Taking vim to the next level

I love vim. It’s not for everyone, but vim is heaven for people who do a lot of text manipulation. I love introducing people to vim and spitting out shortcuts that save a keystroke here and there. For a while, the cognitive cycles needed definitely outweigh the keystroke saved but over time, you’ll find your fingers moving without thinking.

The following are some vim tips I find useful that I’d like to share, assuming you have the basics down: h, j, k, l, w, e, b, 0, $, %, g, G, i, I, a, A, o, O, v, y, d, c, r, p, P, and so on.

Use f to move to a specific place in a line
var quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/;

To move to the second ‘w’ in this line (with your cursor at the start of the line), type fwfw
[vimdoc] moving to a character

Use tabs and splits

As important as learning to create splits and tabs is getting comfortable navigating between them.
[vimdoc] splitting windows
[vimdoc] tabpage

Mark your place

Let’s say you are working on one place of a long file and need to navigate to another place in the file, but eventually need to come back to the current location. Mark the current location by typing mm (m followed by any character) and move around the file at will. Once you’ve done your thing, type ‘m (replace m here with the character you chose before) to move your cursor back to where you marked before. This saves a lot of time as well as cognitive cycles.
[vimdoc] using marks

Get rid of the suck by recording commands

Ever find yourself repeating the same keystrokes over and over? Type qq. Do your keystrokes. Type q. Move to where you want the keystrokes repeated and type @q. Go ahead and try 10@q or even 100@q. If you choose your keystrokes carefully, your recorded commands can be quite adaptive.
[vimdoc] record and playback commands

:mksession

If you like gvim but get frustrated that screen doesn’t play nice, :mksession is your answer! Also, :mksession is great for reloading all the files you have open.
[vimdoc] sessions

Move the view with z

If you find your cursor at the top or bottom of the screen, just type zz or zt to scroll so that your cursor is at the middle and top of the screen respectively. This is useful when you search for a function name but the function ends up at the bottom of your viewport.
[vimdoc] scrolling around

Complete your word with ctrl-p

While in insert mode, press ctrl-p to complete a word that already appears in your open files. If there are multiple possibilities, vim lets you select the correct one from a dropdown.
[vimdoc] completing keywords from different sources

Commentaires
  • provolot

    Brilliant – the word completion made my jaw drop. Aaamazing!.

    One fun thing – recorded commands/macros can contain other commands, which means that you can stitch multiple sets of actions together and combine them with a single command. I like to do this when I’m doing a complicated operation on a text file, say, converting a file from a csv to some other format, so that you can work on it step by step. (I use vim for things like that when it’s complicated enough to be a pain to do by hand, yet simple enough to not warrant the time to write a script.)

    This also means that commands can be recursive — vim supports that pretty well. A quick loop to say ‘do this until you can’t do it anymore would be’: qa (then do actions) @a q. Just make sure that macro a is initially empty before you start defining a…

  • Pingback: What are some favorite Vim commands? - Quora

  • http://www.facebook.com/jay.adkisson Jay Adkisson

    Don’t for get ctrl-n, the forward-looking version of ctrl-p.

  • Anonymous

    Good stuff. I didn’t know about zz and zt!