Operating systems

Use vi to edit files in a UNIX environment

Here are the basic tips and tricks you need to know to use the vi text editor in Linux/UNIX.

Windows network administrators have long used Edit, Notepad, or Wordpad to edit large text and script files. Most UNIX systems have an editor of some sort; the actual utility varies depending on the flavor of UNIX you're running. Some, like Sun Microsystems, even have full-blown competitors to Microsoft Word. But, all UNIX machines have vi—the visual editor. Once you know your way around vi, you can edit files on any flavor of UNIX, including Linux.

What's vi?
At first glance, vi appears to be anything but visual. It is, however, a very powerful editor, and it can do anything you need. It allows you to edit and see your entire document as you work. Many prior UNIX editors, such as ed, were line-oriented editors, where you only saw one line of text at a time. So, vi is considerably more visual than past UNIX editor options.

Combined with several other command line utilities available on most UNIX systems, such as nroff and troff, it is possible to produce documents with as many formatting changes and styles as any Microsoft Word document. In fact, I actually produced many of my graduate school papers using these utilities.

You don't have to worry about going through any special installation routines to get vi on your system. It should be there when the system is installed, because system administrators need vi to do most of their work, including creating system scripts. Redhat Linux does give you the option of installing or not installing particular editors on a system, but the chances of finding a UNIX system without vi is still small.

Author's note
This article is based on the vi version found in Solaris 7. Even though I'll be discussing a specific version of vi, everything discussed here should work fine in any flavor of vi, including the version that comes with the MKS Toolkit for Windows.

Using vi
Before I get into how to use vi, it's important to understand that there are two basic modes for vi: insert mode and command mode. Insert mode is the mode where you type and things appear on the screen. Command mode is the mode where you type and things happen, instead of text appearing. The easiest way to think of command mode is to think of what happens when you cut, copy, and paste data.

By default, vi starts up in command mode, so no text will be entered when you type, unless you choose one of the commands to take you into insert mode. Most vi commands don't show anything on the screen—you just have to know how they work. To switch between insert and command modes, use the [Esc] key. If you're already in command mode, using [Esc] will indicate to vi that you have finished your command and it will attempt to execute the command. If you have not typed a command, pressing [Esc] in command mode will simply ring the terminal bell.

Now that I've discussed the modes vi operates in, you can start to use it. To start, open a terminal window on your workstation. You can start the editor by simply typing vi at the $ prompt and pressing [Enter].

You'll then see a blank document appear, filled with ~ characters down the left side of the page. It doesn't look terribly visual, but those ~ characters you see are just the indication of the bottom of the file. They will not show up in the file when you save it.

To edit a specific document or to give a blank document a name, you can also type vi filename, where filename is an existing file or the name you want the new file to have.

Common insert commands
Once in a document, there are many ways to tell vi to go into insert mode other than just pressing [Esc] (remember that, by default, vi starts in command mode). Most of these ways involve using a vi command. These commands cause vi to go into insert mode right away, so the next characters typed after the command will appear as text in the document. Some of the most common commands are:
  • i
    This is the insert command, which begins inserting characters at the cursor. In the case of a blank document, characters will be inserted at the upper left corner of the document.
  • I
    This is the insert at beginning of line command, which begins inserting characters at the beginning of the current line. Think of pressing <HOME> and then typing in Word.
  • a
    This is the append command, which begins inserting characters after the cursor.
  • A
    This is the append to line command, which begins inserting characters at the end of the current line, after the last character. Think of pressing <END> and then typing in Word.
  • o
    This is the open line below command, which begins typing at the left of the line below the line the cursor is on.
  • O
    This is the open line above command, which begins typing at the left of the line above the line the cursor is on.

Pressing [Esc] after editing using any of these commands will return the session to command mode.

Movement commands
The next thing to learn is the navigation keys in vi. To move around the document while in command mode, use the following keys:
  • h: move one character to the left
  • j: move one character down
  • k: move one character up
  • l: move one character to the right

In the world of mice and GUI applications, these sorts of movement keys seem a bit old fashioned, but they work in every version of vi. Many times the cursor keys, up arrow, down arrow, left arrow, and right arrow also work, but only if someone has set up the termcap or terminfo properties to include those keys. Do yourself a favor though—learn to use hjkl instead of the arrow keys, as hjkl works on all versions of vi and in all situations.

Some other movement commands that are helpful are as follows:
  • [Ctrl]f
    Forward one page command. This will cause the cursor to move forward one page in the text. A page is the length of the screen
  • [Ctrl]b
    Backward one page command. This will cause the cursor to move backward one page in the text.
  • [Ctrl]u
    Forward one half page command. The cursor will move forward one half page in the text.
  • ^
    Beginning of line command. The cursor will move to the beginning of the line. The ^, or carrot key, is [Shiftl]6 on a US English keyboard.
  • $
    End of line command. The cursor will move to the end of the line
  • w
    Beginning of next word command. The cursor will move to the beginning of the next word.
  • b
    Beginning of previous word command. The cursor will move to the beginning of the previous word.
  • e
    End of next word command. The cursor will move to the end of the next word
  • G
    Go to end of file. The cursor will move to the end of the file

The regular functions
All the functions you're used to in regular text editors—such as delete, change, cut, copy, paste, and search/replace—are available in vi. Many of these commands put the session into insert mode, so they must be terminated with an [Esc]. Some common editing commands include:
  • x
    Delete character command. The character under the cursor is deleted
  • dw
    Delete word command. When d and w are pressed in sequence, the word the cursor is in will be deleted.
  • dd
    Delete line command. When d is pressed twice, the line the cursor is on will be deleted.
  • D
    Delete to end of line. When D is pressed, all text from the cursor to the end of the line will be deleted.
  • cw
    Change word command. When c and w are pressed in sequence, the word the cursor is in will be changed with the text typed until [Esc] is pressed.
  • r
    Replace character command. The character the cursor is on will be replaced by the next single character typed. This places the session into insert mode for the single character typed, and there is no need to press [Esc].
  • R
    Replace multiple characters command. Characters from the cursor to the right are replaced with characters that are typed until [Esc] is pressed.

Most of these commands can be enhanced by entering a number before the command. When a number is entered before the command, the command is executed that number of times. For example if you type 2dd, vi will delete two lines of text.

Cut and paste and more
Like most editors, you can cut and paste text in your document. The simplest cut and pastes can be accomplished with the following commands:
  • yy
    The yank line command. The line where the cursor is located will be ”yanked,” or copied, into a buffer, like the clipboard. Note that the yank command can be modified using a number to yank that number of lines into the buffer.
  • yl
    The yank character command. The character where the cursor is located will be yanked, or copied, into a buffer, like the clipboard. This yank command can also be modified with a number to yank that many characters into the buffer.
  • p
    The put after command. The contents of the buffer is "put," or pasted, after the cursor. In the case of a yank line command, the buffer will be pasted below the current line. In the case of a yank character command, the buffer will be pasted to the right of the cursor.
  • P
    The put before command. The contents of the buffer are "put," or pasted, before the cursor. In the case of a yank line command, the buffer will be pasted above the current line. In the case of a yank character command, the buffer will be pasted to the left of the cursor.

You can have more than one memory buffer, or clipboard, by preceding the yank and putting commands with the “character command. The characters can be a-z, and only lower case. For example “ayy will yank the contents of the current line into Buffer A. The “aP command will put the contents of Buffer A above the current line

Probably the most important command to learn for vi is the undo command. This command is extremely simple: Just press u. Pressing the u key will undo the last command issued.

Similar to the undo command is the redo command. Pressing the period key (.) will redo the last command you entered. For example, if you issued a cw command to change a word, you can look for another occurrence of a word and press . to change it to the same word.

Two other important commands are the search commands. To search forward in the text, enter /text, where text is the pattern you are looking for. It is important to note that only exact matches to what you enter will be found; vi does not do any mixed case or fuzzy searching. To search backward in the text, enter ?text, where text is what you're looking for.

Editing commands
In command mode there are a group of commands that are preceded by a :. Unlike the other commands described above, once a colon is entered, the entire command is displayed in the lower left corner of the screen and can be edited before being executed. Some of the more important of the : commands are:
  • :w
    The write file command. If you type :w and press [Enter], the current file written to disk. If a blank document was started, the message No Current Filename will be displayed. It is possible to write a blank document or to write the current document to another file using :w filename.
  • :w! or :w! filename
    The overwrite file command. If you type :w! and press [Enter], the file named will be overwritten with the contents of the session. This is useful for overwriting another file, or for writing a file that has improper permissions.
  • :q
    The quit command. The session is terminated if there are changes since the last save; otherwise, an error message is displayed.
  • :q!
    The quit without saving changes command. The session is terminated without checking for changes.
  • :%s/find_text/replace_text/
    The search and replace command

As I mentioned above, it is possible to modify the search command to be a search and replace command. Using the :%s command tells vi to search all lines of the document, the /find_text command is just like the search command above. When followed by /replace_text/, the first instance of the find_text on each line is replaced by replace_text. In order to have all instances of find_text replaced, it is necessary to add a g (for global) to the end of the command, such as :%s/find_text/replace_text/g.

You don't have to enter commands singly. You can combine commands together. For example, the :wq! (write/quit) command saves the file to disk and exits vi back to the command prompt.

vi= Very involved
This is only a very small portion of the capabilities of vi. It is possible to execute strings of commands together, mark blocks of text for copy and paste, or use the full set of sed commands within vi. It is even possible to have the output of a shell script or other command inserted into your document.

The examples and commands shown above will get you started in using vi. Most vi users have a set of commands they use all the time. It is possible to learn a little more about vi from each person who uses it, and you will be surprised how powerful the editor becomes for you. However, there's one thing you should never forget. Always remember to issue a :w command periodically, because vi does not have an automatic background save feature.
0 comments

Editor's Picks