Vim text editor is used for creating, editing and perform other operations in a text file. Common modes in Vim i.e.

  • Normal Mode : Hit the Esc key to enter Normal mode.
  • Command Mode : Type colon ( : ) in the normal mode to go to this mode.
  • Insert Mode : Type i in the normal mode to go to insert mode.

.(dot) repeat last command.

Normal Mode Commands

  1. Show line number
    set number
  2. Copy text
    Select text and press Ctrl + C
  3. Go to line number
    For instance, if you wanted to go to line 14, could press Esc and then enter :14
  4. Search some text
    Enter /pattern, replacing pattern with the text for which you want to search forward. For example, to find every instance of the word “blimp”, enter:
    /blimp

    To search for word, enter /\<word\> in Normal Mode. \< means beginning of a word, and \> means the end of a word. Enter n for next and N for previous search result.
  5. Search for text in the document, going backwards.
    ?pattern
  6. Find text and replace
    :%s/text/replacement text/g
    Search through the entire document for text and replace it with replacement text. To check the text before replacement, use below command
    %s/text/replacement text/gc
  7. Go go end of file
    <ESC>G
  8. Go to end of cursor
    <ESC>A

Command Mode Options

  1. :q! to quit without saving (short for :quit!)
  2. :wq to write and quit
  3. :wq! to write and quit even if file has only read permission (if file does not have write permission: force write)
  4. :x to write and quit (similar to :wq, but only write if there are changes)
  5. :qa to quit all (short for :quitall)
  6. :cq to quit without saving and make Vim return non-zero error (i.e. exit with error)

Navigating In File

  • h moves the cursor one character to the left.
  • j moves the cursor down one line.
  • k moves the cursor up one line.
  • l moves the cursor one character to the right.
  • 0 moves the cursor to the beginning of the line.
  • $ moves the cursor to the end of the line.
  • w move forward one word.
  • b move backward one word.
  • G move to the end of the file.
  • gg move to the beginning of the file.

Prefacing a movement command with a number will execute that movement multiple times. So, if you want to move up six lines, enter 6k and Vim will move the cursor up six lines.

Editing File Content

  • u – Undo last change
  • U – Undo all changes to the entire line
  • o – Open a new line (goes into insert mode)
  • dd – Delete line. 3dd will delete 3 lines.
  • D – Delete contents of line after the cursor
  • C – Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
  • dw – Delete word. 4dw will delete 4 words
  • d$ – Delete to end of line
  • d^ (d caret) – Delete to beginning of line
  • cw – Change word
  • x – Delete character at the cursor
  • r – Replace character
  • R – Overwrite characters from cursor onward
  • s – Substitute one character under cursor continue to insert
  • S – Substitute entire line and begin to insert at the beginning of the line
  • ~ – Change case of individual character

Copying And Pasting

  • v – Select and highlight one character at a time.
  • V – Select highlight one line at a time.
  • Ctrl-v : Highlight by columns.
  • p : paste text after the current line.
  • P : paste text on the current line.

Examples

To create a new file, and save content

  • Type vim filename (create a file named filename)
  • Hit i ( switch to insert mode)
  • Enter text
  • Hit Esc key (switch back to normal mode)
  • Type :wq (write file and exit vim)