The vi survival guide

If, like mine, your colleagues are constantly moaning about vi when installing their machines, have them read this!

Print the essential commands shown below, stick them on top of the coffee machine and have your coworkers recite them before getting a coffee. After a week, they’ll be ready to fix any Linux box with the stock tools, even when lost in the middle of a desert (I must admit I haven’t tried the desert bit, but it should work).

Essential commands

ESC
toggles between insert and command mode
h,j,k,l
cursor left,down,up,right
i
insert before cursor position
A
add at end of line
o
open (insert) a line after current line
dd
delete current line
CTRL-F / CTRL-B
page forward / backward
ZZ
quit and save
:q!
quit without saving

Other useful commands (partial list)

w,b
word forward/backward
cw
change word (end with ESC)
x
erase one character
d5d
delete 5 lines from current position
dw
erase one word
yy
copy current line to buffer (yank)
p
paste buffer after current line
CTRL-G
display file information at bottom of screen
3G
go to line 3
/expr
search forward for a regular expression
n
repeat forward search
N
repeat backwards search
:g/word1/s//word2/gc
replace word1 by word2 in the whole file with user confirmation.
:/word1/s//word2/gc
replace word1 by word2 in current line with user confirmation.

5 Responses to The vi survival guide

  1. Cool, one which I use a lot is “gqap” which word-wrapped the current paragraph. In combination with :set ai , it even works on indented text, great for making things look nice.

  2. David Channon says:

    Its nice to see VI help floating around. When I was lecuring I always forced the first year students to use VI on the Unix servers for their work. Students don’t necessarily understand why your certain items are on the course. Its not always obvious but it is a part of the big picture. I view basic VI knowledge as being very important even if you don’t use it as your primary editor.

  3. I’ve been using these commands, but a bit differently :

    w : save
    q : quit
    wq : quit and save

    dd : delete line
    5dd : delete 5 lines
    cw : change word
    5cw : change 5 words
    .. and so on, nearly every command can be preceded with a number to repeat multiple times, wuite useful for example :
    yy : copy line
    5p : paste line 5 times

    “:5” : go to line number 5
    /expr : search for expression
    / : search again

    It’s nice to see how many ways there are in VI to achieve the same results.

    One very useful command is also :

    set paste

    It will activate the “paste mode”, so that if you are connecting from a terminal you can cut and paste in the terminal and avoid automatic indentation and other interpretation that would clutter your pasted code.

  4. aizatto says:

    your: d5d
    is too long
    try: 5x
    :), save 1 char

  5. amit says:

    @aizatto: 5x deletes 5 characters from current cursor position. d5d deletes 5 lines from current cursor position

%d bloggers like this: