DEV Community

loading...
Cover image for Vim Tips and Tricks

Vim Tips and Tricks

Harvey
・2 min read

The following are my favorite tips and tricks for working with Vim. I use Vim as my primary editor, and try to avoid the use of other IDEs and editors.

If you're new to vim, or looking to learn more about the features already available, here's a sample of my tips and tricks.

Vim is often used by Linux but it works on every system. It's included by default on Mac OS X.

Searching for Words

If you type / then a search term, Vim will start searching forwards from the current cursor location for that word.

To search for the next occurrence of that search term, press n (for next); to search backwards press N . After pressing n or N , Vim will highlight all matching words, which you can then press on to select.

search

ex commands

One of the most powerful features of Vim is its ability to use ex commands. First, in order to run ex commands, you must be in insert mode: that is to say, not at the beginning of a line.

It’s important to note that in order to enter insert mode, you must first exit normal mode. To do this, press “i” or “a” – these keys will start you off in insert mode at the beginning of a new line or at the end of your current line.

Now that you’re in insert mode, you can type “:” and then an ex command.

Some things you can do are:

:5p print the fifth line
:100p print line 100
:%s/hello/world/ search and replace
:!bash return to bash, type exit to open vim

repeat action

This saves you quit a few keystrokes. Press the number of times you want to repeat, then press the key. To move 100 lines down, press 100j. To move up 50 lines, press 50k. To delete 5 lines press 5dd etc.

awesome

macros

Vim supports keyboard macros. They let you record and replay your keystrokes. This is really great if you have some tedious typing to do.

To start recording press q followed by a. To stop recording press the ESC key. Then press @ followed by a to replay the keystrokes.

If you are new to vim, I recommend this course

Discussion (2)

Collapse
moopet profile image
Ben Sinclair • Edited

Now that you’re in insert mode, you can type “:” and then an ex command

That's backwards - you get to command mode by typing : from normal mode.

Collapse
harvey profile image
Harvey Author

yes you're right