Beyond basic modal editing. Using vim's command-line mode.

This blog post was originally published in thoughtbot's blog. You can find the original at thoughtbot.com/blog/beyond-basic-modal-editing-using-vims-command-line-mode .

Vim is famous for its fabulous modal editing. Its normal, insert, and visual modes quickly become magic in the hands of an able user. But many are unaware or ignore the existence of vim’s command-line mode with its Ex commands.

Whereas vim’s normal and insert mode work in the locale of your cursor, vim’s command-line mode can help you deal with things far from your cursor. Or as Drew Neil puts it in his Practical Vim book,

Vim’s Ex commands strike far and wide.

Let’s look at a few of them.

Copy (copy, t), move (m), and delete (d)

We’ll start by copying, moving, and deleting lines. You access vim’s command-line mode with :. Copying, moving, and deleting can be intuitive. What would you say this command did?

:3copy5

If you guessed copy line three to line five, then you got it!

Copying line 3 to 5 with `:3copy5`

t is shorthand for copy (think copy to). So :3t5 does the same thing as the command above.

Do move and delete work the same way? You betcha.

Try moving a line with :3m5.

Moving line 3 to 5 with `:3m5`

Now try deleting a line with :3d.

Deleting line 3 with `:3d`

Acting on ranges

But that’s not all. Ex commands aren’t limited to single lines. They can act on ranges. Want to move lines 3-5 to line 8? You got it:

:3,5m8

Moving lines 3-5 to 8 with `:3,5m8`

Relative ranges

I know what you’re thinking, “But what if I’m on line 125, and I use relative numbers!” Not to worry. Vim has a range for you too:

:-2,+1d

Delete the two previous line and the next line with `:-2,+1d`

The visual range

What about using a visual selection as a range? If you’re like me, you’ve probably used vim to help you sort lines by visually selecting them and typing :sort. Did you ever notice the range vim inserts for us? It’s the special visual selector range:

:'<,'>sort

Visually select lines 3-7 and sort with `:sort`

Want to perform other actions on that same visual block? The visual selector range ('<,'>) continues to operate on the previous visual selection, even when that range is no longer selected! Let’s delete the lines we sorted last time:

:'<,'>d

Delete lines previously visually selected with `:'<,'>d`

Want to learn more about the visual selector range? As it turns out, ranges can be delimited by marks, and '< and '> are just a couple of special marks. That opens ranges to a world of possibilities! Try '{,'} for paragraphs and '(,') for sentences.

Search and replace

Perhaps you’ve never stopped to think about the magical incantation you use to search and replace words in a file. Well, we can finally uncover the mystery: s will replace things (think substitute). Want to replace foo with bar on line 3?

:3s/foo/bar/g

Replace foo with bar on line 3 with `:3s/foo/bar/g`

What if you want to search and replace in the entire file? We can use a range from the first line to the end of the file: 1,$, or even better, we can use vim’s shorthand for the whole file: %. Go ahead. Type it in, and see the magic work:

:%s/foo/bar/g

Replace foo with bar on all lines with `:%s/foo/bar/g`

Repeat a normal command

In addition to modal editing, vim is known for its ability to repeat the last command with .. Maybe you delete a line with dd, then you realize you need to delete the next one. No need to press dd again, just type . and you’re good to go.

But what about repeating the same command across many lines? What would you do if you wanted to append a comma at the end of each line in a list? Ex commands are here to help you:

:2,5normal A,

Append comma on lines 2-5 with `:2,5normal A,`

Repeat the last Ex command

. repeats the last normal command. What if we want to repeat the last Ex command? We can do that too.

@:

Repeat last command with `@:`

If you’ve ever used macros in vim, you might notice this looks like a macro for the : register. Find that interesting? Take a look at your registers (:reg) and see what you find for :.

What next?

I hope you see the power of Ex commands. Instead of typing 2Gdd to go to the second line and delete it, you can delete it from afar with :2d. And instead of going to line 3, visually selecting lines 3-5, yanking them, going down to line 10, and pasting the lines with 3GVjjjy10Gp, you can just do it from anywhere in the file with :3,5t10.

If you want to learn more, I highly recommend Drew Neil’s Practical Vim book, or take a look at the excellent documentation for different vim modes and for the Ex commands copy, move, delete, substitute. And if you want to dig deeper, help is just a :help away.

Until next time!

Want my latest thoughts, posts, and projects in your inbox?

    I will never send you spam. Unsubscribe any time.