Introduction

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.

CommandsMeaning
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 :.

ModeKey
Command mode Esc
Insert mode i, I, a, or A
Line modeBeginning 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:

KeysMeaning
kUp one line
jDown one line
hLeft one character
lRight one character
wRight one word
bLeft 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:

KeysMeaning
iInsert at the cursor position
IInsert at the beginning of the line
aAppend after the cursor position
AAppend 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:

KeysMeaning
:wWrites (saves) the file.
:w!Forces the file to be saved.
:qQuit
:q!Quit without saving changes.
:wq!Writes and quit.
:xSame as :wq

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

KeysMeaning
:nPositions the cursor at line n.
:$Positions the cursor on the last line
:set nuTurn on line numbering
:set nonuTurn 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.

KeysMeaning
xDelete a character
dwDelete a word
ddDelete a line
DDelete the remaining text on the line.

Vi - Changing Text

In Command mode, you can change text by:

KeysMeaning
rReplace the current character.
cwChange the current word.
ccChange the current line.
c$Change the text from the current position to the end of the line.
CSame 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.

KeysMeaning
yyYank (copy) the current line.
y<position>Yank the position.
pPaste 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

KeysMeaning
uUndo
Ctrl + RRedo

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.

KeysMeaning
/<pattern>Start a forward search
?<pattern>Start a reverse search