vim tips

replace foo with bar from line 10 to 20:

:10,20s/foo/bar/g

Using !cmd to apply a command to a visual selection or a whole file. (e.g. “!sort -nr“)

'gd' (go to definition)

:earlier 5m takes you back 5 minutes in the editing session (ignoring undos/redos)

in cmd mode ctrl-r ctrl-w pastes the word that’s currently under the
cursor in your active buffer. So if you want to replace aVeryLongWord
you’d do :%s/ctrl-r ctrl-w/ctrl-r ctrl-w edit a bit/g.

^a and ^x to incr/decr a number in the text.

Sometimes after making many edits to an open file, you end up wondering what changes you made since it was last saved. In that case, use

:w !diff – %

qz ” record keystrokes into “z register;
” hit ‘q’ again to stop
@z ” play back macro in “z
10@z” play back macro in “z 10 times
@@ ” play back last macro

:registers z ” show macro/register contents

gt ” go to next tab
gT ” go to previous tab

Vim cheatsheet

Leave a Reply