The premier text editor for Linux and UNIX is a program called
vi. While there are numerous editors available for Linux that range from the tiny editor
nano to the massive
emacs editor, there are several advantages to the
vieditor is available on every Linux distribution in the world. This is not true of any other editor.
vieditor can be executed both in a CLI (command line interface) and a GUI.
- While new features have been added to the
vieditor, the core functions have been around for decades. This means that if someone learned the
vieditor in the 1970s, they could use a modern version without any problem. While that seems trivial, it may not seem so trivial twenty years from now.
In reality, most Linux systems don’t include the original
vi, but an improved version of it known as
vi improved. This fact may be hidden by most Linux distributions. For the most part,
vim works just like
vi, but has additional features.
To get started using vi, simply type the command followed by the pathname to the file to edit or create:
There are three modes used in
vi: command mode, insert mode, and ex mode.
Command Mode Movement
Initially, the program starts in command mode. Command mode is used to type commands, such as those used to move around a document, manipulate text, and access the other two modes. To return to command mode at any time, press the Esc key.
Once some text has been added into a document, to perform actions like moving the cursor, the Esc key needs to be pressed first to return to command mode. This seems like a lot of work, but remember that
vi works in a terminal environment where a mouse is useless. Movement commands in
vihave two aspects, a motion and an optional number prefix, which indicates how many times to repeat that motion. The general format is as follows:
The following table summarizes the motion keys available:
|h||Left one character|
|j||Down one line|
|k||Up one line|
|l||Right one character|
|w||One word forward|
|b||One word back|
|^||Beginning of line|
|$||End of the line|
These motions can be prefixed with a number to indicate how many times to perform the movement. For example, 5h would move the cursor five characters to the left and 3w would move the cursor three words to the right. To move the cursor to a specific line number, type that line number followed by the G character. For example, to get to the fifth line of the file type 5G. 1G or gg can be used to go to the first line of the file, while a lone G will take you to the last line. To find out which line the cursor is currently on, use CTRL+G.
Command Mode Actions
The standard convention for editing content with word processors is to use copy, cut, and paste. The
vi program has none of these. Instead,
vi uses the following three commands:
|paste||P | p||put|
The motions learned from the previous page are used to specify where the action is to take place, always beginning with the present cursor location. Either of the following general formats for action commands is acceptable:
Delete removes the indicated text from the page and saves it into the buffer, the buffer being the equivalent of the “clipboard” used in Windows or Mac OSX. The following table provides some common usage examples:
|dd||Delete current line|
|3dd||Delete the next three lines|
|dw||Delete the current word|
|3dw||Delete the next three words|
|d4h||Delete four characters to the left|
Change is very similar to delete; the text is removed and saved into the buffer, however, the program is switched to insert mode to allow immediate changes to the text. The following table provides some common usage examples:
|cc||Change current line|
|cw||Change current word|
|c3w||Change the next three words|
|3dw||Delete the next three words|
|c5h||Change five characters to the left|
Yank places content into the buffer without deleting it. The following table provides some common usage examples:
|yy||Yank current line|
|3yy||Yank the next three lines|
|yw||Yank the current word|
|y$||Yank to the end of the line|
Put places the text saved in the buffer either before or after the cursor position. Notice that these are the only two options, put does not use the motions like the previous action commands.
|p||Put (paste) after cursor|
|p||Put before cursor|
Searching in vi
Another standard function that word processors offer is find. Often, people use CTRL+F or look under the edit menu. The
vi program uses search. Search is more powerful than find because it supports both literal text patterns and regular expressions. To search forward from the current position of the cursor, use the / to start the search, type a search term, and then press the Enter key to begin the search. The cursor will move to the first match that is found.
To proceed to the next match using the same pattern, press the n key. To go back to a previous match, press the N key. If the end or the beginning of the document is reached, the search will automatically wrap around to the other side of the document. To start searching backwards from the cursor position, start by typing ?, then type the pattern to search for matches and press the Enter key.
Insert mode is used to add text to the document. There a few ways to enter insert mode from command mode, each differentiated by where the text insertion will begin. The following table covers the most common:
|a||Enter insert mode right after the cursor|
|A||Enter insert mode at the end of the line|
|i||Enter insert mode right before the cursor|
|I||Enter insert mode at the beginning of the line|
|o||Enter insert mode on a blank line after the cursor|
|O||Enter insert mode on a blank line before the cursor|
vi editor was called the
ex editor. The name
vi was the abbreviation of the visual command in the
ex editor which switched the editor to “visual” mode.
In the original normal mode, the
ex editor only allowed users to see and modify one line at a time. In the visual mode, users could see as much of the document that will fit on the screen. Since most users preferred the visual mode to the line editing mode, the
ex program file was linked to a
vi file, so that users could start
ex directly in visual mode when they ran the
vi link. Eventually, the actual program file was renamed
vi and the
ex editor became a link that pointed the
ex mode of the
vi editor is being used, it is possible to view or change settings, as well as carry out file-related commands like opening, saving or aborting changes to a file. In order to get to the ex mode, type a : character in command mode. The following table lists some common actions performed in ex mode:
|:w||Write the current file to the filesystem|
|:w filename||Save a copy of the current file as filename|
|:w!||Force writing to the current file|
|:1||Go to line number 1 or whatever number is given|
|:e filename||Open filename|
|:q||Quit if no changes made to file|
|:q!||Quit without saving changes to file|
A quick analysis of the table above reveals that if an exclamation mark, !, is added to a command, it then attempts to force the operation. For example, imagine you make changes to a file in the
vi editor and then try to quit with :q, only to discover that the command fails. The
vi editor doesn’t want to quit without saving the changes you made to a file, but you can force it to quit with the ex command :q!.