Using vim

These are some basic commands to get you started using vim. For lots more useful information consult "VIM USER MANUAL," http://vimdoc.sourceforge.net/vimum.html, by Bram Moolenaar.

 

A. File creation

  1. At the $ prompt enter
        vim

  2. You will then see an empty workspace
  3. Press the the A key to allow you to enter text. Pressing A puts you into append mode.

  4. Be sure to press Enter at the end of each line
  5. To save a file:
    a. Press the ESC key to switch to command mode
    b. Enter a colon (:). The cursor will move to the command line at the bottom of the screen.
    c. Enter wq filename.cpp (press Enter)
  6. To periodically save your work:
    a. Press the ESC key to switch to command mode
    b. Enter :w filename.cpp (press Enter)

  7. Continue entering and saving text as necessary. When you save a file this way the previous version is replaced.

  8. When you're done press ESC and enter :wq filename.cpp (Press Enter)to save the current workspace and exit vi.

  9. To exit vim work session without saving current workspace press ESC and enter :q!

B. File Modification Using vim

  1. Enter vim by entering the following at the $ prompt: vim file_name

  2. Moving cursor around the screen

    To end of current line use: $
    To start of current line use: 0
    up, down left, right use arrow keys OR k , j, h, l
    To end of file use: G
    To first line of file use :1 ENTER

  3. To advance to next page use pgdown.
    To go back to previous page use pgup

  4. To display line numbers enter:
    ESC colon (:), set number , ENTER

  5. To move cursor to a specific line enter
    ESC colon(:), line_number, ENTER
    e.g. ESC:15 ENTER

  6. To insert text to left of cursor, enter i followed by text to insert.
    To insert text to right of cursor,enter a followed by text to insert.
    Lines may be inserted by following the same sequence and pressing ENTER as appropriate.

  7. To delete character at cursor use x

  8. To delete line containing cursor use dd
    To delete several lines from cursor line on use:
    #_of_linesdd, e.g. 12dd

  9. To replace character at cursor use r newch
    To substitute more than one character for character at cursor use s newchars

  10. To replace all occurrences of a text pattern with another use
    :g/text_pattern/s//replacement_pattern/g

  11. To search the file you are editing:
    Forwards from cursor use /pattern ENTER
    Backwards from cursor use ?pattern ENTER
    To repeat previous search command use /ENTER

  12. To join current line with next line use J

  13. To move a block of text
    -Position cursor at start of block
    -remove block with #_lines_in_blockdd e.g. 10dd
    -reposition cursor
    -press lowercase p

  14. To copy a block of text
    -Position cursor at start of block
    -copy block with #_lines_in_blockyy e.g. 12yy
    -reposition cursor
    -enter lowercase p

  15. Enter % with cursor on a (,),{,},[,] to find its mate.
For lots more useful information consult "VIM USER MANUAL," http://vimdoc.sourceforge.net/vimum.html, by Bram Moolenaar.