win

I love keyboard shortcuts. Naturally, VIM would be my best friend but it was really hard to get into. While pair programming at Pivotal with expert VIMists (is that a word?) I got to learn a few tricks. Below I’ve gathered a subset of vim commands that have converted me. Think of it as “just enough vim” to get very comfortable.

I assume you understand the difference between insert, visual and normal mode.

Basics

dd deletes the line your cursor is on dd

x deletes the character you cursor is on x

D deletes all the characters from your cursor to the end of the line D

r + <some character> will replace the character your cursor is on with <some character> r

p will paste what you’ve copied in the line below your cursor p

P will paste what you’ve copied in the line above your cursor P

y will copy the highlighted text

y

yy will copy the line your cursor is on yy

Movement

j, k, h and l will move your cursor down, up, left and right respectively j k h l

gg will jump you to the top of the file gg

G will jump you to the bottom of the file G

0 will move you to the front of the line 0

$ will move you to the end of the line dollar

“Scrolling”

I use these two often to “scroll” through a file. { will move your cursor to the next empty new line above your cursor

previousempty

} will move your cursor to the next empty new line below your cursor nextempty

Alternatively you can use ctrl+f or cftrl+b to scroll to the previous or next “page” where page is the amount of lines that your screen can fit. I find i don’t use this as often. page f page b

Text manipulation

ciw will delete the word you’re cursor is on and put you in insert mode. I remember this as “change in word”

ciw

viw is similar to ciw but only highlights the word and puts you in visual mode. I remember this as “view in word”

viw

d/ + <some characters> + enter will delete characters from your cursor to <some characters>. This is equivalent to highlighting and deleting part of a line in say sublime.

dslash

J will delete and append the line below your cursor the end of your current line. It’s handy when you want to “glue” together some lines of text.

shiftj

Switching to insert mode

a puts your cursor in insert mode after the character the cursor was on. i puts your cursor in insert mode before the character the cursor was on. a i

A puts your cursor in insert mode at the end of the current line. I puts your cursor in insert mode at the beginning of the line.

shifta shifti

To remember which is which I tell myself that a stands for append and i … is the other one.

o puts you in insert mode on a new line below your current line. O puts you in insert mode on a new line above your current line.

o shifto

Commands

As a quick summary for complete beginners to VIM, you can enter commands while in normal mode by type : followed by the command and enter. Here are some basic ones everyone should know.

:w saves the current file. I remember it as “writes” the file.

:q quits the file.

:wq saves the file and quits the file. :x is equivalent. Not sure why it exists but I use it often.

:qa quits all the buffers.

:set cursorcolumn creates a highlight bar on the column your cursor is on. It’s just a visual aid which is helpful to check your tabbing is okay.

cursorcolumn

:set nu turns on line numbers. It’s actually short for :set number which is easier to remember.

nu

:set nonu turns off line numbers. There is also an equivalent :set nonumber.

nonu

:e <filename> will open up a file you type in.

colone

Use ctrl+o to move to the previously opened file or ctrl+i to move to the next opened file.

io

:set wrap turns on wordwrapper. Handy when you’re editing paragraphs of text and not code. :set nowrap will undo the wordwrapper.

wrap

When you’re pasting blocks of text from outside of vim, you should run :set paste to prevent vim auto inserting tabs in your text.

/ following by a word searches your file for that word. slash

Type n to move your cursor to the next instance of that word. N will move your cursor to the previous one.

n shiftn

A fun fact I later learnt about this is that even after you start editing or move your cursor to another line, n and N will still work for the last word you searched for.

moren

When you do a find/search vim will highlight that text. Use :noh to remove this highlight. I remember it as “no highlight”.

noh

Multiple windows

:sp is a handy command that will split your window into two horizontally. (I remember it as “splitting” the window) This is convenient for when you want to look at two different files at the same time. I often follow :sp with :e to open up new files.

sp

I more commonly use :vs which I remember as a vertical split. This splits the screen side by side. vs

To move around windows you can use ctrl+w ctrl+w to toggle your cursor between open windows. To be more exact you can use ctrl+w (j/k/h/l) to move in a specific window to the bottom, top, left or right.

ww wjk

Quickly highlight blocks of texts

V will highlight the entire line your cursor is on. Follow through with movements like j, k, { or } to highlight large blocks of text.

shiftv shiftvplus

Fancy stuff

So there are a few handy moves that I don’t know where to put. They are definitely beyond the basics.

:%s/existing-string/new-string/g Will find all instances of “existing-string” and replace it with “new-string”

percents

A variation of the above is :%s/existing-string/new-string/gc which replaces instances of “existing-string” one by one but asks you to confirm first. Hit y to accept, hit n to skip. percentsc

There is this multicursor command: ctrl+v (j/k/h/l). This allows you to select the current column among multiple lines.

ctrlv

More interestingly once you’ve selected a set of lines, by typing I <some chars> ESC, you’ll apply <some chars> to all the lines that were selecto confirm ted.

ctrlvplus

Markers

If you’re editing a large file and are frequently jumping back and forward between two sections you can create markers to quickly jump between lines. m<somechar> will allow `<somechar> to jump to that line. (That’s a backtick follow by some character).

marker

Run :marks to see the markers you’ve set.

Plugins

There are tons of plugins available for VIM that does very specific things. The one I suggest as a must have is NERDTree.

You can use | (pipe) to jump from your current file to the tree. Or you can use the same keys for window navigation such as ctrl+w h to move to the window to the left since NERDTree is just another window.

pipe

Conclusion

If you memorize this subset of commands I’m confident you’ll begin to navigate VIM with ease…

And when I step into the building everybody’s hands go … on the keyboard hands