NOTES on the vi editor

This is not a complete list of commands. It is more like a cheat sheet and is much more than most people need. Almost everything can be done if you know the right 10 commands or so. But the others make everything easier and faster, especially if you spend much of your time using vi.

Why use vi at all? A few reasons are that 1) it's powerful 2) it is available on many platforms 3) it is NOT a microsoft product 4) the resulting document remains normal text - so you can open it with other editors 5) it has some features that facilitate programming 6) it is relatively small (it will easily fit on a floppy) and 7) it is not GUI based... to name a few. Why is it good to not have a GUI? Well, you can't run Word or Notepad on a machine that you are connected to via Telnet, for example.

Vi is not a word processor. You'll probably want to keep your word processor around and use it to format nice letters, resumes etc.


most important notes

when in doubt - hit ESC
press ESC to get out of insert mode.

type u to undo your last change (after an ESC)
type :q! to exit the editor without saving the changes you made

type :wq to save your changes and exit
when in doubt - hit ESC


  • command mode: move the cursor around, edit or go into insert or last line modes
  • insert mode: insert text above or below the current line, or before/after the cursor, etc.
  • last line mode: enter a command that may affect a line, a range of lines or the file.
  • Press the ":" key.
    this brings you down to the last line of the
    screen and readies the editor for a command.
    enter the command followed by a CR or hit
    ESC to get out of this mode. The commands
    themselves appear later in this document.

  • search forward or backward in the current file.

    From command mode press "/" and enter a text string to search forward in the file. When the search pass the end it will wrap around to the beginning. Example:


    From command mode press "?" and enter a text string to search backward in the file. When the search pass the end it will wrap around to the end.

    From command mode press "n" or "N" to repeat the last search (find next) forwards (n) or backwards (N).

    From the command mode press `` (back-tick, not double quote) to go back to were you were before the last search. This applies to all navigational commands.

basic movement and navigation

  • h, j, k, l = left, down, up, right (left/right one character or up/down one line)
  • w, b = forward and backward one word
  • ^F, ^B = full-screen, jump forward/backward
  • ^U, ^D = half-screen, scroll up/down

less basic movement

  • :5 = goto line 5
  • w = move forward a word
  • b = move backward a word
  • e = move to the end (rather than beginning) of the next word
  • $ = go to end of current line
  • 0 = (zero) = go to beginning of current line

advanced movement

  • `` (two back-ticks) = Goto previous location (before last move, search, etc.)
  • % = go from any brace, paren, bracket {,(,[ ,],),} to it's corresponding mate
  • ]] = go to next function (opening '{' ) in a program
  • [[ = go to previous function (opening '{' ) in a program
  • m<letter> = mark this (current cursor location) place for future reference.
  • `a = goto place marked 'a' (i.e., mark your place before doing a search then find your way back to the original location easily)

command modifiers

Add a numeric modifier to repeat the command n times.

  • 5h = move 5 characters to the left
  • 3j = move down 3 lines
  • 5w = move 5 words to the right

inserting text

  • o = open the next line and start inserting text
  • O = open the previous line and start inserting text
  • i = open before the cursor and insert text
  • I = open at the beginning of the line and insert text
  • a = open after the cursor and insert text
  • A = open at the end of the line and insert text
  • ESC = end the insertion of text (back to command mode)


  • yy = copy whole line ("yank" into un-named buffer)
  • p = paste from buffer
  • dd = delete line (into buffer)
  • yw = yank a word
  • 3yw = yank 3 words
  • 3yy = yank 3 lines
  • dw = delete word
  • 5dw = delete 5 words
  • 5dd = delete 5 lines
  • x = delete the character under the cursor
  • 3x = delete 3 characters
  • r = replace character under the cursor
  • R = change the letters under the cursor until ESC pressed
  • cw = change the whole word (ESC to terminate)
  • C = change the whole line from the cursor forward until ESC key pressed
  • D = delete everything from here to the end of the line
  • J = "join" the next line to this current line.
  • u = undo last change
  • . = repeat last command

named buffers

there are 26 named buffers, named a thru z

  • "ayy = yank current line into buffer 'a'
  • "c5yy = yank 5 lines into buffer 'c'
  • "b6dd delete 6 lines into buffer 'b'
  • "e8yw= yank 8 words into buffer 'e'
  • "z7dw= delete 7 words into buffer 'z'
  • "xp = put the contents of buffer 'x' here
  • "ap = put the contents of buffer 'a' here
  • "cp = put the contents of buffer 'c' here

un-named buffers

deleted stuff is put into a stack of un-named buffers

  • "5p = undo (paste here) from 5 deletes ago

block cut-n-paste

  • ma = mark this spot as place 'a'

go to another place (i.e., search for end of block or say move down 10 lines)

  • d`a = delete/cut from here to the place marked 'a'


  • "bd`a = delete (cut) everything from this place to the place
    marked 'a' and put it all in buffer 'b'

go somewhere else, then

  • "bp = put the stuff in buffer 'b' here

other stuff/common usage

  • vi *.txt = edit all files ending with .txt
  • :n = edit next file in list
  • :n# = edit previous file (repeating will toggle files - not go farther back in list)
  • :e <new name> = edit a different file (requires saving changes)
  • :e! <new name> = edit new file without saving changes in current file
  • :e# = edit previous file
  • :q! = quit without saving change (exit)
  • :w = write the file out to disk (save)
  • :w <new name> = write to a new name (save as)
  • :wq = write and quit
  • :r <file> = read the file in and put it here
  • :!<cmd> = execute shell command 'cmd' example :!ls
  • :r !<cmd> = execute the command and put the results here
  • :6 = goto line 6
  • :6d = delete line 6
  • :$ = goto last line (same a G in command mode)
  • :u undo last change
  • :f = give me info about the current file (size and current line)

When writing documentation it is sometimes helpful to do these

  • :r !date = run the UNIX date command and puts the results right into the file.
  • :r !ls = insert a directory listing of the current directory here.
  • :r !ps -ef = insert the current process status here.

operating on a range of lines

  • :1,. = from line 1 to the current line
  • :1,.d = delete line from the beginning of the file to here
  • :.,$ = from the current line to the last line
  • :.,$d = delete line from here to the end of the file
  • :5,10 = from lines 5 to 10
  • :5,10d = delete lines 5 through 10
  • :1,$s/old/new/g = In all lines replace the old string with the new string. The g at the end indicates that if there is more than one old sting in a line you want them all replaced.
  • :5,$10/old/new = In all lines 5 through 10 replace the first occurance of old string with the new string in each.
  • :g/abc/d = remove all lines in the file containing the string "abc". Nice for cleaning up logs.

Did I make any mistakes? Did I leave something out? Let me know. - a personal web site for Mike Fried
Copyright © 2002-10, Mike Fried. All rights reserved