The Visual Editor (vi) is the standard text editor used in Linux operating system. You can use the vi editor to write, edit, and save simple text files, programs, shell scripts, Structured Query Language (SQL) queries, and procedures. The vi editor also provides commands that enable you to work with multiple files.
This ReferencePoint introduces the vi editor and discusses the commands to create a text file, insert text, and save text in the file. It also discusses advanced commands used to view the display status, customize settings, and work with multiple files.
Introducing the vi Editor
The vi editor is used to edit ASCII text files in Linux. You can open the vi editor using the vi command. The vi editor opens in the default command mode. The three modes of operation in the vi editor are:
You can switch between these modes by using the keys listed in Table 5-1-1:
Mode | Keys Used |
---|---|
A, a, I, i, O, o, R, r, S, s | |
Escape | |
Colon | |
Enter |
The Command Mode
The command mode is the default mode and is used to execute commands in the vi editor. If the vi editor is in the insert or ex mode, you need to press the ESC key to switch to the command mode. When you press a key in this mode, the Linux shell interprets these keystrokes as commands.
The Insert Mode
To insert text in a file, you need to switch to the insert mode. In this mode, the characters that you type appear on the screen and are stored in the buffer. A buffer is a memory space where data is stored temporarily. Figure 5-1-1 shows the vi editor in the insert mode:
The ex Mode
You use this mode for file-handling operations, such as quitting from and saving a file. You can switch to the ex mode by typing a colon in the command mode. Figure 5-1-2 shows the vi editor in the ex mode with the command to quit the vi editor:
Figure 5-1-2: The vi Editor in the ex Mode
Working with Files
The vi editor provides commands to create, save, modify, and edit a file. In addition, it provides various commands to insert text in a file and access multiple files.
Creating a File
To create a file called abc, in the vi editor, type:
vi abc
If a file called abc already exists, the vi editor opens it; otherwise, the vi editor creates a new file called abc. Figure 5-1-3 shows the vi editor window with a new file, abc:
When you open a file in the vi editor, the cursor is always positioned at the first character of the first line. The tilde character at the beginning of a line indicates that the line is empty.
Note | In the vi editor window, the position where the cursor is placed is the current cursor position and the line at which the cursor is placed is the current line. |
Inserting Text in a File
To insert text in the abc file:
-
Type text in the file. Figure 5-1-4 shows the vi editor window after inserting text:
To insert a word in the abc file:
-
Type the required word.
Figure 5-1-5 shows the inserted word, three, in a file called abc in the vi editor window:
Navigating in a File
In the vi editor, you can navigate horizontally and vertically in a file. Table 5-1-2 lists the commands used to navigate horizontally in a file:
Command | Description |
---|---|
| Moves the cursor |
| Moves the cursor |
Ctrl | Moves the cursor to the first character of the current line |
| Moves the cursor to the |
0 | Moves the cursor to the beginning of the current line |
| Moves the cursor after |
| Moves the cursor before |
| Moves the cursor to the |
| Moves the cursor one input space before the |
| Moves the cursor to the |
| Moves the cursor to one input space after the |
| Moves the cursor |
| Moves the cursor |
| Moves the cursor to the end of the |
Table 5-1-3 lists the commands for navigating vertically in a file:
Command | Description |
---|---|
| Moves the cursor after the |
| Moves the cursor before the |
| Moves the cursor to the end of the |
| Moves the cursor to the first character of |
| Moves the cursor to the first character of the |
| Moves the cursor to the first character of the nth line above the current cursor position |
| Moves the cursor after the |
| Moves the cursor before the |
| Moves the cursor to the |
| Moves the cursor to the |
M | Moves the cursor to the middle line of the window |
: | Moves the cursor to the |
: | Moves the cursor to the |
: | Moves the cursor to the |
Ctrl + f | Scrolls to the next screen |
Ctrl + b | Scroll to the previous screen |
Ctrl + d | Scrolls to half a screen after the current cursor position |
Ctrl + u | Scrolls to half a screen before the current cursor position |
Editing a File
The vi editor provides commands to edit text in a file. It provides commands to undo changes, delete, copy, and paste the text of a file.
Undoing Changes
The vi editor provides commands to undo the changes made to a file. Table 5-1-4 lists the commands used to undo changes:
Command | Description |
---|---|
U | Revokes the changes made to the file |
:u | Revokes only the last change made to the file |
:q! | Quits the editor without saving the file |
Deleting Text
The vi editor provides commands for deleting text in a file. Using the commands in the vi editor, you can delete lines, words, and characters from a file. Table 5-1-5 lists the commands used for deleting text:
Command | Description |
---|---|
| Deletes |
| Deletes |
| Deletes |
D | Deletes the line after the current cursor position |
: | Deletes the lines from line |
Copying and Pasting Text
You can use the Y command to copy text. This process of copying text is also called yanking. The text is copied in a temporary location called a buffer. You can use the P command to paste text from the buffer at the required location. Table 5-1-6 lists the commands for copying and pasting text:
Command | Description |
---|---|
| Copies n lines below the current line. |
Y | Copies the current line. |
: | Copies lines from line |
P | Pastes the copied lines at the current cursor position. |
| Navigates to the position specified by |
For example, to copy and paste three lines from the current cursor position:
-
Type 3yy to copy three lines from the current cursor position. Figure 5-1-6 shows the vi editor window after three lines are copied:
Figure 5-1-6: The vi Editor Window After Copying Three Lines
-
Place the cursor at the required position.
-
Type P to paste the copied lines at the required position. Figure 5-1-7 shows the vi editor window after the text is pasted:
Searching and Replacing Text in a File
You can search and replace text in the vi editor. Table 5-1-7 lists the commands used to search text in the vi editor:
Command | Description |
---|---|
/ | Moves the cursor to the next occurrence of the specified string from the current cursor position |
? | Moves the cursor to the previous occurrence of the specified string from the current cursor position |
/ | Moves the cursor to the |
/ | Moves the cursor to the |
: | Searches from line |
n | Searches for the string below the current occurrence of the string |
N | Searches for the string above the current occurrence of the string |
To search for a string called mode in the abc file:
-
Open file abc in the vi editor, using the command:
vi abc
-
Search for the string in the file using the command:
/mode
Figure 5-1-8 shows the vi editor window with the output of the command to search the string mode in the abc file:
The last line of the figure indicates the command used and the cursor position indicates the first occurrence of the string being searched. Table 5-1-8 lists the commands to replace text in the vi editor:
Command | Description |
---|---|
R | Replaces the specified character at the current cursor position |
R | Replaces text after the current cursor position |
s | Replaces a character at the current cursor position with any number of characters |
S | Replaces the current line |
The syntax for replacing a text with a specified text within a file is:
:1,$s/string1/string2/g
When the above command is executed, string2 replaces string1. 1,$ specifies that all lines of the files will be searched.
The syntax for a command that limits the search area is:
:n1,n2s/string1/string2/g
In the above command, n1 and n2 specify the lines that are searched for string1. When found, string1 is replaced with string2.
The syntax for specifying a command to replace a string after asking for a confirmation from the end user is:
:n1,n2s/string1/string2/gc
In the above command, the character c at the end of command specifies that before replacing string1 with string2 a confirmation from the end user would be taken.
For example, the syntax for a command that searches for the string editor in a file and replaces it with the string window after taking a confirmation from the end user is:
:1,6s/editor/window/gc
Figure 5-1-9 shows the vi editor window when you execute the above command:
In the figure, the first occurrence of the string editor is highlighted. The last line shows the confirmation message.
Saving a File
You need to save a file after you have created it. You should be in the ex mode to save a file. Table 5-1-9 lists the commands for saving a file in the vi editor:
Command | Description |
---|---|
:w | Saves the current file |
:w | Saves the current file with the specified filename |
:wq | |
:.w | Saves the current line of the file to the specified file |
To save the abc file:
-
Type w to save the file.
Figure 5-1-10 shows the vi editor window when you save a file using the :w option:
When you save a file, the Linux operating system saves the contents of the file to the hard disk and shows file information in the last line of the vi editor window. Figure 5-1-11 shows the status of the saved file in the vi editor window:
In the last line, "abc" specifies the file name, 6L specify the number of lines in the file, and 119C specifies the total number of characters in the file.
Advanced Commands
The vi editor provides commands to display file information, customize vi editor, shell commands, and work with multiple files.
Displaying File Information
You use the Ctrl+G command to display the file name, current line status, and cursor position. Figure 5-1-12 shows the vi editor window with the output of the Ctrl+G command:
In the last line, "abc" specifies the file name, line 1 of 6 specifies that the cursor is placed at the first line, 16% specifies the percentage of file used by the current line. col 1 specifies the current cursor position.
Customizing the vi Editor
You can customize the vi editor according to your requirements. The commands to customize the vi editor are:
The set Command
The vi editor provides you with various set options for working with a file, To view all the set options, use the command:
:set all
Table 5-1-10 lists the set options provided in the vi editor:
Option | Description |
---|---|
ai | Applies current indentation to the new line |
ic | Ignores the case of letters while searching for a specific pattern of text |
nu | Displays line numbers in the current file |
sm | Matches ( with ), { with }, and [ with ] |
ts | Sets a value for the tab key |
ws | Search for specified text in the entire file |
The syntax for using the nu, set command is:
:set nu
Figure 5-1-13 shows the result of the :set nu command in the vi editor window:
The Mapping Command
You need to use the map command in the ex mode to associate a function to a key. You can either associate a function with an undefined key or associate a new function with a defined key. A function assigned to a key is executed when you press a key. Table 5-1-11 lists the map commands:
Command | Description |
---|---|
:map | Assigns the function to the specified string |
:map! | Assigns the function to the specified string in the append mode |
:map | |
:map! | |
:unmap | Cancels the mapping assigned to string |
:unmap! | Cancels the mapping assigned to string in the append mode |
The Abbreviate Command
You can use the abbreviate command in the ex mode, to specify abbreviations for words. When you type an abbreviation in the insert mode, the vi editor expands it automatically. Table 5-1-12 lists the abbreviate commands:
Command | Description |
---|---|
:ab <> <> | Abbreviates word as wr |
:ab | |
:unab | Disables ab as an abbreviation |
Using Shell Commands in the vi Editor
The shell acts as a command interpreter. Table 5-1-13 lists the shell commands that you can use in the vi editor:
Command | Description |
---|---|
:sh | Executes a subshell |
Ctrl + D | |
: | Executes the shell command and passes lines |
: | Repeats the previous shell command and appends the option to the shell command |
| Executes the shell command with input as |
| Appends to the previous shell command with the input specified by |
| Specifies |
| Appends to the previous shell command and executes the shell again |
:r! | Moves the output of the shell command to the line above the current line |
:r | Reads the specified file into the buffer |
Working with Multiple Files
You can work with multiple files in the vi editor when in the ex mode. The vi editor provides command to switch between files, split the vi editor window, and move text between files.
Commands to Switch Between Files
You can open multiple files in the vi editor. The syntax to invoke the vi editor with multiple files is:
vi filename1 filename2 filename3
In the above syntax, the vi editor opens files in the order in which they are listed.
The vi editor provides various commands to switch from one file to another without switching to the command prompt. Table 5-1-14 lists the commands to switch between files:
Command | Description |
---|---|
:n | Edits the next file if the vi editor is invoked with multiple file names |
:rew | |
:r | Shows the contents of the specified file below the current line of the current file |
:w >> | Appends buffer content to the specified file |
:e | Switches from the current file to the specified file in the vi editor |
:e! | Discards the changes made to the current file and switches to the specified file |
:e! | Opens the last saved version of the file |
e# | Switches to the recently edited file |
Splitting the vi Editor Window
You can split the vi editor into multiple windows and view different files in these windows. To split the vi editor window into two windows:
-
Press the Enter key. The vi editor window splits into two windows. The first window shows file abc and the second window shows the file called filename.
Figure 5-1-14 shows the vi editor window with multiple windows:
Figure 5-1-14: The vi Editor Window with Multiple WindowsIn the above figure, the highlighted line with text, abc, is the demarcation between the two files.
Moving Text between Files
The vi editor enables you to store blocks of text in a named buffer. The text stored in this buffer can be pasted at the required location. There are twenty-six named buffers provided in the vi editor. The syntax for saving text in a buffer is:
"
In the command,
:e file1
Place the cursor at the required location and paste the text using the command:
"p
In the above command, ch refers to the buffer that contains the text to be pasted.
You need to use an upper case alphabet that specifies buffer name to append text to a buffer. The syntax to append text to a buffer is:
"
In the above command,
For example, if you need to copy three lines of text from the def file and paste them in the abc file:
-
In the def file, type:
"a3yy
-
To append the text in the buffer, type:
"A2yy
-
To switch to the abc file, type:
:e abc
-
Move the cursor to the required location.
-
To paste the text, type:
"ap