Using the vi Text Editor in Solaris 9
In the world of Solaris, most files are plain text without fancy typographic material, whether text, shell scripts, Web pages, or even C programs. No bold, no multiple font colors, no included graphics. As a result, most Solaris users use vi, a powerful (albeit tricky to learn) text-only editor that enables you enter and modify text quickly. With vi, you must do everything — from cursor motion to search and replace — from the keyboard.
Perhaps the most puzzling aspect of vi is that it is a modal editor. These are the two modes of operation:
- Insert mode: If you’re in insert mode and type an x, the letter is added to the document at the current cursor point.
- Command mode: If you’re in command mode, the x command causes the letter under the cursor to be deleted, not added.
Fortunately, there’s a trick to starting up vi that enables a mode display function on the bottom line of the screen. This display quickly tells you whether you’re in insert or command mode. Rather than specify the show mode feature manually each time you start vi, open a terminal window and type the following:
echo “set showmode” >> ~/.exrc
Do that once, and you’ve created a custom preferences file for vi (yes, it should be called .virc, but that’s a long story). You don’t have to ever think about it again.
You can start the vi editor from the command line several ways:
- Type vi on the command line:
- Specify the name of an existing file to edit or a new file to create:
$ vi my.new.file
- You can also specify a list of filenames if you want. You can finish editing the first file, and then move to the second, and so on to modify a batch of files in sequence.
Start vi by specifying the name you want to create:
$ vi ashley.travels.txt
The File, Edit, View, and other menus are for the terminal application, not vi. The vi program has no fancy interface elements, just whatever you type at the keyboard.
There’s no menu at the bottom, just a cursor at the top-left corner and a bunch of tilde symbols (~) running down the left side. The lines prefaced with tilde symbols are placeholders, not part of the file. They’re beyond the end of the file being edited.
By default, the editor starts out in command mode: Type an x, and you’ll hear a beep as the editor tells you that there’s nothing to delete. You can move into insert mode several ways, depending on where you want to insert text. In fact, vi has dozens and dozens of commands.
Following are a few some basic commands:
- To insert just before the current cursor location (where the flashing block is sitting), press i.
- To insert just after the current cursor location, use a to append text.
- To insert just above the current line by creating a new blank line, use O (capital o).
- To insert into a new blank line just below the current line, use o (lowercase o).
Jump into this by pressing i and typing the following:
Gulliver’s Travels is the most well-known work of Irish writer Jonathan Swift, famous for his work as a novelist, essayist and satirist. You Betcha!
To make this interesting, add a few random nonsense characters at the end of what you type in.
Notice that the bottom-right corner of your screen says INSERT MODE. That’s the show mode feature giving a visual clue of what mode you’re in.
To leave insert mode and get back to command mode, press the magic key, Esc. Intelligently, the Esc key has no function in command mode. You can press it any time you want to be in command mode and aren’t sure what mode you’re in. It just beeps.
Moving in the file
Because vi has no scroll bars and no mouse support, it has a set of keys that you can use in command mode to move around.
On Solaris, you can also use the arrow keys on your keyboard unless you’re connected remotely, in which case they may or may not work.
The four key movement keys are h, j, k, and l:
- h moves one character to the left.
- j moves one line down.
- k moves one line up.
- l moves one character to the right.
Try using these four keys to move around. If these letters start to appear in your document, you’re still in insert mode and need to press the Esc key.
- You can move one word at a time with w or b, depending on whether you want to move forward one word or backward one word.
- You can jump to the beginning of the line with 0 (zero) and to the end of the line with $.
- To move page by page, when the file is sufficiently large to have pages of text, use
• ^F to move forward a page
• ^B to move back a page
• ^D to move down half a page
• ^U to move up half a page
- You can jump to the first line of the file with 0G (zero followed by G) and to the end of the file with G by itself.
Use motion keys to move to the first letter of the extraneous stuff you added to the file. Now press the x key a few times. Each time you press it, you should see the letter under the cursor deleted and the text slide left to fill the now open hole.
Finish up the delete process so that there are no stray characters. Then use the a append function to add a few carriage returns immediately after the period ending the sentence. The cursor should now be on the last line, with at least one blank line separating it from the text in the file.
Including other files
To include the contents of another file, you need to jump to the vi command line.
- You do this by typing : , at which point the cursor immediately jumps to the bottom-left corner of the screen.
- Type in the following command:
- After pressing Enter, the contents of the file are injected into the buffer, exactly as you saw in the GNOME Text Editor and CDE Text Editor.
The final step on this journey is to save the new file. You started vi with the new filename. To save the file with that name, type one of these commands:
- :w writes the file and stays in vi.
- :wq writes the file and quits vi.
ZZ writes out the file if it has changed and then quits, without the command line (:) sequences.
If you entered more than one file, use :w to write out this file and then :n to move to the next file on your list.
To quit and discard the changes that you have made, add a ! to the end of the command, like this: