Nano is great for simple edits, vi and emacs have more advanced and powerful features. THere will be a learning curve to use these two editors as they are not exactly intuitive. Learning vi will definitely be harder than Nano. It will require a bit of a time investment to become proficient.

The vi editor

To use the vi editor, simply type vi [filename] to create or edit a file. You can also use vim [filename]. Vim stands for vi improved, which is compatible with all the command found at vi, and has some additional features like syntax highlighting and ability to edit files over the network. On many Linux distributions, when you type vi you are actually running vim or vi improved. If you want to view a file but make sure you don't change any of it's content, you can use the view [file] command.

Commands Meaning
vi [filename] Create or edit file
vim [filename] It is vi improved, has more features.
view [filename] Start vi/vim in read-only mode.

The advantage of using vi are:

  • It is always available on a Linux system.
  • Once you learn the key mappings for vi, you can apply them to other command like man, more, less, or view.

Vi Modes

Vi has the concept of modes. You are always working in one of three modes: Command mode , Insert mode or Line mode . The modes in vi are Command mode which you can enter by hitting the Esc key, Insert mode by using i, I, a, or A. And Line mode by beginning commands with a colon character :.

Mode Key
Command mode Esc
Insert mode i, I, a, or A
Line mode Beginning commands with :

Vi Command mode and Navigation

When vi starts you are placed into Command mode . To get back to Command mode at anytime symply hit the Esc key. Letters typed while in Command mode are not sent to the file, but they are interpreted as commands.

Command mode allows you to navigate the file, perform searches, delete text, copy text, and paste text. Here are some of the commonly used key binding with navigation and Command mode:

Keys Meaning
k Up one line
j Down one line
h Left one character
l Right one character
w Right one word
b Left one word
^ Go to the beginning of the line
$ Go to the end of the line

Vi Insert mode

In order to insert text in a file, enter vi's Insert mode. Do this by pressing lowercase i, uppercase I, lowercase a, or uppercase A. The corresponding meaning is displayed as below:

Keys Meaning
i Insert at the cursor position
I Insert at the beginning of the line
a Append after the cursor position
A Append at the end of the line

Vi Line mode

To use vi as Line mode, begin a command with colon (:). For example, to write the file or save the file, type :w. The commonly used commands are:

Keys Meaning
:w Writes (saves) the file.
:w! Forces the file to be saved.
:q Quit
:q! Quit without saving changes.
:wq! Writes and quit.
:x Same as :wq

Line mode can be used for navigation too. For example type :15 to go to line 15 in a file.

Keys Meaning
:n Positions the cursor at line n.
:$ Positions the cursor on the last line
:set nu Turn on line numbering
:set nonu Turn off line numbering
:help [subcommand] Get help

Vi - Repeating Commands

You can repeat commands in vi by proceding them with a number. For example, to move the cursor up 5 lines, simply type 5k.

To insert a piece of text 80 times, type 80i, and start entering the text, once done hit the Esc key to return to command mode, and the text you typed will be repeated 80 times. For example: 80i_ will insert '_' characters 80 times.

Vi - Deleting Text

In Command mode, you can use x delete a character, dw to delete a word, dd to delete a line,D to delete the remaining text on the line.

Keys Meaning
x Delete a character
dw Delete a word
dd Delete a line
D Delete the remaining text on the line.

Vi - Changing Text

In Command mode, you can change text by:

Keys Meaning
r Replace the current character.
cw Change the current word.
cc Change the current line.
c$ Change the text from the current position to the end of the line.
C Same as c$
~ Reverses the case of a character.

Vi - Copying and Pasting

In Command mode, to yank or copy the current line, type yy. To yank a position, type y and a position character. For example, to yank a word, type yw.

Keys Meaning
yy Yank (copy) the current line.
y<position> Yank the position.
p Paste the most recent deleted or yanked text.

Vi - Undo / Redo

In Command mode, the undo command is u, and the redo command is Ctrl + R

Keys Meaning
u Undo
Ctrl + R Redo

Vi - Searching

In Command mode, to start a forward search, type a forward slash and a search pattern and hit Enter. To go to the next match, type n. To go to the previous match, type the captial N

To start a reverse search, use the question mark followed by a search pattern, and then hit Enter.

Keys Meaning
/<pattern> Start a forward search
?<pattern> Start a reverse search