vi Editor Quick Reference

vi editor is the most popular editor in a linux system, used for creating and editing files. Knowing some of its commands will always be handy.

Leave a comment below if you would like some more commands to be included which could be helpful or let me know if you like the article. Any kind of feedback helps.

Here is your quick reference to the vi editor and it’s commands.

The Two modes of vi editor

vi editor works in two modes; Command mode and Insert mode.

Command Mode

vi editor opens in command mode, where it accepts commands to accomplish a task. Tasks like, save, quit, cut, copy, paste, insert etc.

If you are in insert mode, then press [Esc] key to switch back to command mode.

Note : Commands are case-sensitive.

Insert Mode

Insert mode allows user to insert text in a file. When in Insert mode, everything entered by the user will be taken as input to the file.

User can switch to insert mode by pressing i in the command mode. There are other options as well.

  • i – insert text before the cursor.
  • a – insert test after the cursor.
  • A – Move to the end of line and append input.
  • o – insert input below the current line
  • O – insert input above the current line
  • I – insert input before the first non-blank character.

Start, Save and Exit

Start by creating a file using vi editor.

$ vi <fileName>.<fileExtension>
eg : $ vi sample.txt

To insert text press – i

To save your work and/or to exit get back to the command mode by pressing [Esc] key

  • :w – save your work
  • :q – quit without saving
  • :q! – force exit (when you don’t want to save the change(s))
  • :wq – save and quit editor
  • <colon>x – save and quit editor (writes to disk only when changes have been made)

The difference between wq and x is that, if you do not change the content of the file and command :wq , the modification time will be updated. In case of x the modification time will not be updated if you haven’t change anything.

In the following section, all the commands will work in the command mode.

Screen Navigation

  • — one page down scroll – CTRL + F
  • — one page up scroll- CTRL + B
  • — scroll window one line down – CTRL + E
  • — scroll window one line up – CTRL + Y

Cursor Navigation

Using Arrow keys – Works both in command and Insert mode.
h, j, k and l keys – Works only in command mode

below, n – is optional. And n represents a number.

  • — Left movement by n characters – nh
  • — Down movement by n lines – nj
  • — Up movement by n lines – nk
  • — Right movement by n characters – nl

Command to make navigation within file easier

  • G$ – move to the end of the file.
  • GA – move to the end of line and enable insert mode.
  • gg – move to the start of the file
  • $ – move to the last character of the line.
  • n$ – move to the last character of the nth line from the current line.
  • w – Forward to the beginning of the next word.
  • b – move back to the beginning of previous word.
  • 0 – move to the first character of the line.
  • ^ – move to the first non-blank character of the line.
  • G – move to the first non-blank character of the last line.
  • nG – move to the first non-blank character of the nth line.
  • ) – Forward to the next sentence.
  • ( – Move back to the beginning of previous sentence.
  • } – Move forward to the next blank line.
  • { – Move backward to the previous blank line.
  • % – Forward to the matching brackets/braces/comments.

You may also be interested in reading another excellent quick reference

Shell Script Quick Reference

Cut, Copy and Paste Commands

  • yy – copy the current line.
  • dd – cut the current line.
  • p – paste the copied/cut content after the cursor
  • P – paste the copied/cut content before the cursor
  • y[navigation_command] – copy the content from the current pointer to the navigated area.
  • d[navigation_command] – cut the content from the current pointer to the navigated area.

[navigation_command] – can be any command from the above cursor navigation section.
Eg : if you are at the beginning of the file and you want to copy the entire content of the file, use the command yG$

Misc Commands

  • :set number – display the line numbers in your file
  • :set nonumber – remove the line numbers from the file
  • u – undo the previous change
  • cc – replace the current line with a new line.
  • /what – search forward for the word what.
  • ?what – search backward for the word what.

There are many more commands which can be included, specially in relation with regular expressions. Let me know in the comments section if you would like them to be added as part of quick reference.

Subscribe to get notified on the upcoming articles. Share this article with others if you think it will be helpful for others.

Leave a Reply