Wednesday, April 14, 2010

Vi/Vim Survival Guide

Vi is a command line editor in linux. I have prepared a "vi" survival guide for my own reference and thought to share it with others. This guide is derived from Vi's official survival guide and some other sources.

Vi/Vim SURVIVAL GUIDE
  1. The cursor is moved by using either the arrow keys or the hjkl keys.

    • h (left)

    • j (down)

    • k (up)

    • l(right)

  2. To exit vim :q!

  3. Type x to delete the character below the cursor, X deletes the character to the left of the cursor.

  4. To insert or append text type:

    • i type inserted text insert before the cursor

    • I type inserted text insert at the beginning of the current line

    • A type appended text append after the line

    • a type appended text append immediately after the cursor

    Note: a(A),i(I) all go to the same insert mode, the only difference is where the characters are inserted.

  5. To insert an empty line below the cursor type and go to insert mode: o (lowercase o)

  6. To insert an empty line above the cursor type and go to insert mode: O (capital O)

  7. To delete from the cursor up to the next word type: dw

  8. To delete from the cursor up to the end of a line type: d$

  9. To delete a whole line type: dd

  10. To repeat an action prepend it with a number: 2w

  11. To move to the start of the line use a zero: 0

  12. To move to the end of the line use dollar sign: $

  13. To undo previous actions, type: u (lowercase u)

  14. To undo all the changes on a line type: U (capital U)

  15. To redo (undo the undo's) type: CTRL-R

  16. To put back a text line that has just been deleted, move the cursor on the line above the deleted line and type p.

  17. To replace a character,
    move the cursor on that character, type r and then then the new character.
    type R to go Replace mode and to replace more than one characters.

  18. Type ce to change the characters in a word, ce deletes the word and places you in Insert mode. cne changes n number of words. c$ changes uptil the end of the line.

  19. CTRL-G displays your location in the file and the file status(in terms of percentage). :f provides the same result as CTRL-G.
    G moves to the end of the file.
    number G moves to that line number.
    gg moves to the first line.

  20. Typing / followed by a phrase searches FORWARD for the phrase.
    Typing ? followed by a phrase searches BACKWARD for the phrase.
    After a search type n to find the next occurrence in the same direction
    or N to search in the opposite direction.
    CTRL-O takes you back to older positions, CTRL-I(Capital "EYE") to newer positions.

  21. Typing % while the cursor is on a (,),[,],{, or } goes to its match.

  22. To substitute new for the first old in a line type :s/old/new
    To substitute new for all 'old's on a line type :s/old/new/g
    To substitute phrases between two line #'s type :#,#s/old/new/g
    To substitute all occurrences in the file type :%s/old/new/g
    To ask for confirmation each time add 'c' :%s/old/new/gc

  23. :!command executes an external command.
    for example :!ls or :!rm FILENAME

  24. :w FILENAME writes the current Vim file to disk with name FILENAME

  25. Type v to go to visual mode,to select some text, while in visual mode, type :w FILENAME saves the visually selected text in file FILENAME.

  26. :r FILENAME retrieves disk file FILENAME and puts it below the cursor position

  27. :r !dir reads the output of the dir command and puts it below the cursor position

  28. In visual mode, type y to copy selected text and p to paste it to a desired location.

  29. Typing :set 'xxx' sets the option xxx. Some options are:
    ic (ignore case) ignores upper/lower case while searching
    hls (highlight search) highlight all matching phrases.
    cp (Compatible mode) :set [no]cp
    Prepend "no" to switch an option off: :set noic

  30. Type :help(within Vim) to open a help window, (:q to exit the help window)

  31. Type CTRL-W CTRL-W to jump to another window