In this article, you will get a brief introduction to vi editor and its basic commands.

There are many command line text editors in Ubuntu but three of them are very popular – ed, vi, emacs.
ed is simple to use but is not as powerful as vi and emacs editors.
vi and emacs are most powerful interactive editors. Some people consider emacs is an OS (Operating System) itself, while some treat it as a shell.
There are many versions of vi editor available. The most common version of vi is a clone called vimVi IMproved, which comes with Unix/Linux.

vi Editor Modes

There are three modes in vi editor:

  1. Command mode
  2. Insert mode
  3. Last Line or Escape mode
vi Editor - switch between different modes

All vi editor modes are explained with basic commands to manipulate text files.

Command Mode

Command mode is the central mode or default mode of vi editor. When the editor starts up, it’s in Command mode.

  • This lets you move around the text and delete words or lines of text.
  • In this mode, the status bar at the bottom of the screen shows information such as the percentage progress through the document.
  • Although you cannot insert text in this mode, you can delete and otherwise manipulate words and lines within the file.
  • You can also move through the text using the cursor keys and the Page Up and Page Down keys.

Command Mode

Delete Text

dd – Delete current line.
ndd – Delete n number of lines, for instance, 4dd will delete four lines.
x – Delete single character under the cursor.
nx – Delete n number of characters, starting from under the cursor.
dw – Delete the current word under the cursor.
dnw – Delete n words beginning with character under the cursor.
db – Delete the word before the cursor.
D – Delete everything from the cursor to the end of the line.

Search Text

/string – Search text in forward direction (type searching text after the slash).
?string – Search text in backward direction.
n – Move to next occurrence (forward search).
N – Move to previous occurrence (backward search).

Cut and Paste Text

yy – Copy (yank, cut) the current line into buffer.
nyy – Copy n number of lines into buffer from the cursor downwards.
p – Paste the contents of buffer/clipboard after the current line.

Changing Text

r – replace single character under the cursor.
R – replace characters, starting with current cursor position, until Esc hit.

Insert Mode

To type your own text or edit text, you need to switch to Insert mode.

  • To get in insert mode, press i or a or A.
  • You can also type O or o to change to Insert mode.
  • In Insert mode, you can still move around the text using the cursor keys. Anything you type will appear the point of the cursor.
  • To quit this mode, press the Esc key. This will return you to Command mode.


Insert or Add Text

i – Switch to insert mode at the cursor.
I – Insert text at the beginning of current line.
o – Switch to insert mode, placing the cursor below the current line.
O – Switch to insert mode, placing the cursor above the current line.
a – Append text to the end of line.
A – Delete the current word under the cursor.

Last-Line Mode

This mode is also known as Last-line or Escape mode.

  • Command-Line Mode, the third mode you should be aware of is Command-Line mode (note that, irritatingly, this is not the same as the Command mode).
  • As its name suggests, this is the mode in which you can enter commands to save and load files, as well as perform other fundamental tasks to control vim or to quit the program.
  • You can enter Command-Line mode by typing a colon (:), although if you’re in Insert mode, you’ll first need to leave it by pressing the Esc key.


To Exit and Find-Replace

:w – Save the file.
:w! – Save the file and ignore the errors.
:q – Quit vim.
:q! – Quit vim and ignore errors such as an unsaved file.
:wq – Save the file and quit vim.
:s/word/replacement/ – Search downwards and replace with new word.
:help – View help docs.

Above are the 3 modes of vi editor and the basic commands to manipulate the text files.

For Linux Commands related short tutorials, click here. 😉

Categorized in: