13.  Vim Reference Card

13.1.  Vi states

Vi has 3 modes:

  1. command mode - Normal and initial state; others return here (use ESC to abort a partially typed command)

  2. input mode - entered by specific commands a i A I o O c C s S R and ended by ESC or abnormally with interrupt

  3. line mode - i.e. waiting for input after a : , / , ? or a ! command (end with CR , abort with CTRL-c ). CTRL is the control key: CTRL-c means "control c"

13.2.  Shell Commands

  1. TERM= code Puts a code name for your terminal into the variable TERM

  2. export TERM Conveys the value of TERM (the terminal code) to any UNIX system program that is terminal dependant.

  3. tput init Initializes the terminal so that it will function properly with various UNIX system programs.

  4. vi filename Accesses the vi screen editor so that you can edit a specified file.

  5. vi file1 file2 file3 Enters three files into the vi buffer to be edited. Those files are file1, file2, and file3 .

  6. view file Invoke vi editor on file in read-only mode

  7. vi -R file Invoke vi editor on file in read-only mode

  8. vi -r file Recover file and recent edits after system crash

13.3.  Setting Options

  1. :set option Activate option

  2. :set option=value Assign value to option

  3. :set no option Deactivate option

  4. :set Display options set by user

  5. :set all Display list of all current options, both default and those set by the user

  6. :set option ? Display values of option

13.4.  Notations used

Notations:

  1. CTRL-c CTRL is the control key: CTRL-c means "control c"

  2. CR is Carriage return (ENTER key)

13.5.  Interrupting, cancelling

  1. ESC end insert or incomplete command

  2. CTRL-? CTRL is the control key: CTRL-? means "control ?" delete or rubout interrupts

  3. CTRL-l reprint/refresh screen if CTRL-? scrambles it

13.6.  File Manipulation

  1. ZZ Save the file and exit vi

  2. :wq Save the file and exit vi

  3. :w Write the current file

  4. :w! Force write the current file, if file is read-only

  5. :w name Write to file name

  6. :q Exit from vi

  7. :q! Force exit from vi (discarding changes)

  8. :e name Edit file name

  9. :e! reedit, discard changes

  10. :e + name edit file name , starting at end

  11. :e + n edit starting at line n

  12. :e # edit alternate file

  13. :n edit next file in arglist

  14. :args list files in current filelist

  15. :rew rewind current filelist and edit first file

  16. :n args specify new arglist

  17. :f show current file and line

  18. CTRL-G synonym for :f , show current file and line

  19. :ta tag to tag file entry tag

  20. CTRL-] :ta, following word is tag

13.7.  Movement

  1. Arrows Move the cursor

  2. CTRL-d Scroll half page down

  3. CTRL-u Scroll half page up

  4. CTRL-f Scroll a full page down

  5. CTRL-b Scroll a full page up

  6. :0 Move to start of file

  7. :n Move to line number n

  8. :$ Move to end of file

  9. 0 Move to start of line

  10. ^ Move to first non-blank character

  11. $ Move to end of line

  12. CR Move to the start of next line

  13. - Move to the start of previous line

  14. % Find matching bracket

  15. G goto line (last line default)

  16. ]] next section/function

  17. [[ previous section/function

13.8.  Line Positioning

  1. H Home window line

  2. L Last window line

  3. M Middle window line

  4. + Next line, at first non-white

  5. - Previous line, at first non-white

  6. CR return, same as +

  7. j next line, same column

  8. k previous line, same column

13.9.  Character positioning

  1. 0 beginning of line

  2. $ end of line

  3. h forward

  4. l backwards

  5. SPACE same as l

  6. fx find x forward

  7. Fx find x backward

  8. ; repeat last f F

  9. , inverse of ;

  10. | to specified column

  11. % find matching { or }

13.10.  Words, sentences, paragraphs

  1. w Word forward

  2. b Word backward

  3. e End of word

  4. ) To next sentence

  5. ( Back sentence

  6. } To next paragraph

  7. { Back paragraph

  8. W Blank delimited word

  9. B Back W

  10. E To end of W

13.11.  Marking and returning

  1. `` (press twice the back-quote ` key) Previous context

  2. '' (press twice the single-quote ` key) Previous context at first non-white in line

  3. mx mark position with letter x

  4. `x (back quote key and letter x) goto mark x

  5. 'x goto mark x at first non-white in line

13.12.  Corrections during insert

  1. CTRL-h Erase last character

  2. CTRL-w Erase last word

  3. erase Press DELETE key, same as CTRL-h

  4. kill Your kill key, erase input this line

  5. \ Escapes CTRL-h, DELETE and kill

  6. ESC Ends insertion, back to command

  7. CTRL-? Interrupt, terminates insert

  8. CTRL-d Backtab over autoindent

  9. CTRL-v Quote non-printing character

13.13.  Adjusting the screen

  1. CTRL-l Clear and redraw

  2. CTRL-r retype, eliminate @lines

  3. z-CR redraw, current line at window top

  4. z- redraw, current line at window bottom

  5. z. redraw, current line at window center

  6. /pat/z- pat line bottom

  7. tn Use n line window

  8. CTRL-e Scroll window down 1 line

  9. CTRL-y Scroll window up 1 line

13.14.  Delete

  1. x Delete the character under the cursor

  2. X Delete the charater before the cursor

  3. D Delete to the end of line

  4. d^ Delete back to start of line

  5. dd Delete the current line

  6. ndd Delete n lines starting with the current one

  7. dnw Delete n words starting from cursor

13.15.  Insert, change

  1. i Enter input mode inserting before the cursor

  2. I Enter input mode inserting before the first non-blank character

  3. a Enter input mode inserting after the cursor

  4. A Enter input mode inserting after the end of the line

  5. o Open a new line below current line and enter input mode

  6. O Open a new line above current line and enter input mode

  7. r Replace the character under the cursor (does NOT enter input mode)

  8. R Enter input mode replacing characters

  9. C shift-c. Change rest of line

  10. D shift-d. Delete rest of line

  11. s Substitute chars

  12. S Substitute lines

  13. J Join lines

  14. J Join lines

13.16.  Copy and Paste

The "yank buffer" is filled by EVERY delete command, or explicitely by Y and yy .

  1. Y Copy the current line to the yank buffer

  2. n yy Copy n lines starting from the current to the yank buffer

  3. p Paste the yank buffer after the cursor (or below the current line)

  4. P Paste the yank buffer before the cursor (or above the current line)

  5. " x p Put from buffer x

  6. " x y Yank to buffer x

  7. " x d Delete into buffer x

13.17.  Operators (use double to affect lines)

  1. d delete

  2. c change

  3. < left shift

  4. > right shift

  5. ! filter through command

  6. = indent for LISP

  7. y yank text to buffer

13.18.  Search and replace

  1. / text Search forward for text

  2. ? text Search backward for text

  3. n Repeat the last search in the same direction

  4. N Repeat the last search in the reverse direction

  5. / Repeat the last search forward

  6. ? Repeat the last search backward

  7. [ addr ] s/from/to/ [ g ] Search for the occurence of from and replace it with to in the current line, or in the range addr (two line numbers seperated by command; 1,$ is the whole file). Replaces one occurrence per line, or all occurrences if g is specified. For example, :3,20s/someword/anotherword/g Will replace "someword" with "anotherword" starting from line 3 to line 20. 'g' is global means replace all occurrences of "someword".

13.19.  General

  1. :sh Forks a shell (to be exited with CTRL-d)

  2. :! command Forks a shell to execute command

  3. :set number Switch on line numbering

  4. :set nonumber Switch off line numbering

13.20.  Line Editor Commands

  1. : Tells vi that the next commands you issue will be line editor commands.

  2. :sh Temporarily returns to the shell to perform some shell commands without leaving vi .

  3. CTRL-d Escapes the temporary return to the shell and returns to vi so you can edit the current window.

  4. : n Goes to the n th line of the buffer.

  5. : x,z w filename Writes lines from the numbers x through the number z into a new file called filename .

  6. :$ Moves the cursor to the beginning of the last line in the buffer.

  7. :.,$d Deletes all the lines from the current line to the last line

  8. :r filename Inserts the contents of the file filename under the current line of the buffer.

  9. :s /text/new_text/ Replaces the first instance of text on the current line with new_text

  10. :s /text/new_text/g Replaces the every occurrence of text on the current line with new_text

  11. :g /text/s//new_text/g Changes every occurrence of text on the buffer to new_text .

13.21.  Other commands

  1. u Undo the last change

  2. U Restore the current line

  3. ~ Change case

  4. J Join the currentline with the next line

  5. . Repeat last text changing command

  6. CTRL-g Show file name and line number