Last month's column explained why text files are so important to Linux users and described how to use filters to process text. This month's column will dive a little deeper into text processing by explaining the basics of using the vi editor. If you think that knowing how to use pico or notepad is all you need to know about editors and editing, read on. You'll soon discover that vi has capabilities as wide ranging as those of a Swiss army knife, providing functions you won't find in most other editors.
|Figure One: Switching from command mode to insert mode in vi.|
Last month’s column explained why text files are so important to Linux users and described how to use filters to process text. This month’s column will dive a little deeper into text processing by explaining the basics of using the vi editor. If you think that knowing how to use pico or notepad is all you need to know about editors and editing, read on. You’ll soon discover that vi has capabilities as wide ranging as those of a Swiss army knife, providing functions you won’t find in most other editors.
Ask a group of Linux sysadmins to name their favorite tools and you’ll often find that the vi editor is at or near the top of the list. The vi editor is a compact and efficient tool that is found on the installation and rescue media of several Linux distributions. Consequently, it’s apt to be available even when bulkier editors, such as those based on X, are not. If you’re trying to repair a broken system, you’ll likely find knowledge of vi helpful.
However, wide-spread availability due to a small disk footprint is not vi‘s only asset. On the contrary, vi lets you perform sophisticated pattern matching and replacement operations not found in most text editors. Maybe you have heard the other side of the story. It’s true that things aren’t all roses with vi. For example, the user interface dates from the days of Teletype terminals and 100-baud modems. Learning to use vi can be frustrating. But with the help of this article, you’ll get off on the right foot. Soon, you can have a working knowledge of vi.
Getting Started with vi
Most Linux distributions include the vi editor and several install it by default. You’ll actually find several versions of vi, including elvis, nvi, vile, and vim. Perhaps the most popular of these is vim, which stands for “Vi IMproved” and is distributed as part of Red Hat Linux and other Linux distributions. If you can’t find vi on your system, see the sidebar Getting vi.
To start vi, issue the command:
If you want to edit a particular file, issue the command:
where file is the name of the file you want to edit. If you want to start vi and immediately jump to a particular line in a file, issue the command:
where num is the line number of the line you want to jump to. If you omit the line number and include only the +, vi immediately jumps to the end of the file. If you want to immediately jump to the first line containing a particular word, issue the command:
where word is the word you want.
The vi editor has two modes, command mode and insert mode. vi starts in command mode by default. In command mode you can navigate through the file by using the cursor keys. You can also enter commands, as will be explained in the next section.
The other vi editor mode is insert mode. While you’re in insert mode, any characters you type are added to the file, beginning immediately to the right of the cursor. When you’re finished typing, you press the <escape> key to re-enter command mode.
Figure One highlights vi‘s modes. For now, all you need to remember is:
- To move from command mode to insert mode, issue either an append command, which consists of the letter a or A, or an insert command, which consists of the letter i or I. You’ll learn the details of these commands later in the article.
- To move from insert mode to command mode, press the <escape> key.
- To exit vi, hit <escape> to enter command mode and type a colon (:) followed by a q. Then hit <return>.
vi provides three basic groups of commands: navigation commands, editing commands, and special commands. Tables One, Two, and Three summarize the most important commands in each group. If you inspect Table Three you’ll notice that most of the special commands begin with a colon (:). The colon is vi’s cue to invoke the ex editor, which is built into most modern versions of vi.
Today, most people consider ex a relic, comparable to the MS-DOS editor ed. However, back in the days of paper tape Unix, ex was a popular standalone text editor. The chief drawback of ex is that it compels the user to edit a file one line at a time. But, when teamed with vi, ex can execute commands over ranges of lines rather than one line at a time. The ex command set is quite powerful, so once ex is freed from operating on only a single line at a time it is extremely useful. The vi editor relies heavily on ex; you can’t save a file or exit vi without using an ex command.
Figure Two contains a simple example of a vi session that involves changing the inetd.conf file to activate the FTP service. This is done by deleting the hash mark (#) at the beginning of the line that refers to the FTP service. The text at the right of each command explains the function of the command.
Figure Two: Sample vi Session
|Invoke the vi editor|
|/ftp||Go to the first line containing the word ftp|
|0||Go to the beginning of the current line|
|x||Delete the current character|
|:w||Write the file to disk|
| :q||Exit the editor|
The / (slash) command is a navigation command that searches the file for a specified word of text. On the second line of Figure Two the slash command is used to find the line containing the text ftp. Sometimes it’s convenient to search backwards (that is, moving toward the top rather than the bottom of the file). The ? command serves to do this. Other than moving backwards, it works just like the slash command.
The 0 command moves the cursor to the beginning of the current line. It’s another of vi’s navigation commands (shown in Table One). If you want to move to the end of a line rather than the beginning, you can use the $ command. Another popular navigation command is w, which moves the cursor one word to the right. The related b command moves the cursor one word to the left. It’s important to remember that you’ve got to be in command mode for these navigation commands to work. If you see the commands appearing on the screen as characters rather than performing their intended action, you’re in insert mode and need to hit the <escape> key to enter command mode.
Table One: Basic Navigation Commands
|$||Move to end of the current line|
|/<string>||Go forward to next line containing string|
|?<string>||Go backward to next line containing string|
|0||Move to beginning of the current line|
|b||Move one word backward|
|G||Go to final line|
|nG||Go to line n|
|n||Repeat previous search|
|w||Move one word forward|
You may want to move immediately to a particular line sometimes. To do so, use the G command; while in command mode, simply type the number of the destination line and then type an uppercase G. Sometimes vi is configured to omit line numbers from its display. In that case, issue the following command to turn on line numbering:
Like other ex commands beginning with a colon, you must press Enter after typing this command.
In the example in Figure Two, the x command deletes the character under the cursor. Table Two shows a variety of other editing commands you’ll find handy. Perhaps the most important is a or i, which places vi in insert mode. Characters typed in insert mode are inserted into the file. As mentioned before, you can exit insert mode by pressing <escape>.
Table Two: Basic Editing Commands
|$||Move to end of the current line|
|<<||Shift line left|
|>>||Shift line right|
|a||Append text after cursor|
|A||Append text at end of line|
|d$||Delete from cursor position through end of line|
|dd||Delete current line|
|dw||Delete word after cursor|
|Esc||Exit insert mode|
|i||Insert text before cursor|
|I||Insert text at beginning of line|
|J||Join current line and following line|
|p||Insert text from buffer|
|u||Undo last change|
|U||Undo changes to current line|
|x||Delete current character|
|yy||Copy current line to buffer|
When you’re done editing a file in vi, you must save it. The :w (write) command performs this function. Be sure to press the Enter key after issuing this ex command. To exit vi, simply issue the :q (quit) command. Again, be sure to press the Enter key after typing the command.
If you’ve changed anything in the file, vi won’t let you just quit without first saving those changes. You will first need to issue a write command and then a quit command. If you’ve altered a file but do not want to save the changes and overwrite your original file, you will need to type :q!. You add the ! (exclamation mark) to the quit command.
If you plan to work with vi often, you will certainly benefit from learning at least some of the many shortcuts it provides. For example, rather than entering separate commands to write the current file to disk and exit vi, you can issue the following single command that performs both functions:
Table Three: Some Special Commands
|:!<command>||Execute the specified shell command|
|:1,$s/x/y/g||Substitute y for every first instance of x in lines 1 to last (the entire file)|
|:q!||Quit vi, discarding edit bufferv|
|:r file||Insert text of file at current cursor position|
|:s/x/y/||Substitute y for the first instance of x in the current line|
|:s/x/y/g||Substitute y for every instance of x in the current line|
|:set nonu||Suppress display of line numbers|
|:set nu||Display line numbers|
|:w||Write current file to disk|
|:w filename||Write edit buffer to specified file|
|:w!||Write current file to disk, overriding write protection|
|:w! filename||Write edit buffer to specified file, overriding write protection|
|ZZ||Write current file to disk and quit|
But Wait — There’s More…
This column hasn’t even scratched the surface of what’s possible with the help of vi. For instance, you can use vi’s pattern matching and substitution facility to interchange columns of text — a feat few other editors permit. Next month’s column will address this and other capabilities of vi that derive from ex. Meanwhile, to learn more about vi see Ellen Siever’s invaluable Linux in a Nutshell, 3rd. ed., O’Reilly (2000), which devotes 30 pages to summarizing vi’s features and syntax. Or, if your vi appetite is greater still, consider Linda Lamb’s 173-page book on vi,Learning the vi Editor, O’Reilly (1990). In any case, spend some time getting better acquainted with vi. Once you get over the initial speed bumps you’ll find that a knowledge of vi pays daily dividends. You may soon find yourself reluctant to use any other editor. Really!
You can obtain the popular vim variant of vi from its homepage, http://www.vim.org/. The homepage provides access to the binary and source distributions of vim as well as a plethora of information about vim and text editing.
In addition to the Linux implementation of vim, you’ll find implementations for a variety of other operating systems, including MacOS, MS Windows, and many flavors of Unix besides Linux. If you choose to use vim as your standard editor, you’ll be able to use the same editor on virtually any computing platform. This alone is a good reason to choose vim.
Bill McCarty is an associate professor at Azusa Pacific University. He can be reached at email@example.com.