Moving Through the File
Before you can do any real editing, you must be able to move through the file. The following is a small subset of the commands with which you can move through the file. Remember once again you must be in command mode to run these commands. You can move from insert mode to command mode with the Esc key.
l (lower case L)
Cursor one character right
h
Cursor one character left
j
Cursor one line down, staying in the current column. In other words, if you were on the 14th character of line 5 when you pressed j, the cursor would end up on the 14th character of line 6. If line 6 has less than 14 characters, the cursor will be on the last character in line 6.
k
Cursor up one line. The k command works the same as j only up instead of down.
w
Cursor one word right. This command wraps at the end of a line.
b
Cursor one word left. This command wraps at the beginning of a line.
G
Cursor to end of file
gg
Cursor to start of file. On VI implementations without the gg command, use 1G
100j
Cursor down 100 lines. Naturally, that number can be any number you choose. You can also precede k with a number to move up that many lines. This same technique can be used to move several characters (100l or 100h), or several words (100w or 100b).
Ctrl+G
Tells you your current line number, so you can estimate how far you need to jump or move.
52G
Cursors to line 52 from anywhere in the file. Naturally, that number can be any desired destination line. Note that on some implementations you can do this as 52gg, which might be easier for some keyboarders.
Ctrl+F
Move down (forward) one screenful of lines.
Ctrl+B
Move up (backward) one screenful of lines.
Ctrl+D
Move down 1/2 screenful of lines.
Ctrl+U
Move up 1/2 screenful of lines.
The preceding is a list of basic, essential moves. There are many more. In Vim, use the :h motion.txt command to learn the rich variety of motion commands. And there are moves based on searches. The next section of this article covers basic searches.
Basic Searches
This section covers VERY basic searches. VI searches can be incredibly powerful and complex, because they incorporate regular expressions. This article does not cover search and replace, because in VI search and replace is extremely powerful, and is implemented differently from plain searches.
:set ic
Sets VI to ignore case in all searches and replaces. All subsequent searches are case insensitive.
:set noic
Sets VI to consider case in all searches and replaces. All subsequent searches are case sensitive.
/Steve
Search for the next instance of Steve in the file. In other words, search down from the cursor.
?Steve
Search for the previous instance of Steve in the file. In other words, search up from the cursor.
/web *master
Search for webmaster, web master, or web master. This is a very basic regular expression that means “look for web, followed by zero or more spaces, followed by master. Such regular expressions also can be used with the ? to search backwards. The asterisk means “zero or more of the preceding character”, and the preceding character in this case is a space.
/
Search down for the next occurrence of whatever you searched for last.
?
Search up for the next occurrence of whatever you searched for last.
n
Repeat the previous search, in the direction of the previous search.
N
Repeat the previous search, in the opposite direction of the previous search.
As an added benefit, many VI implementations including the Vim VI implementation give you a search history. Press the slash key (/) followed by the up arrow, and you’ll see the history of all your searches, both forward and backward. If you want to search backwards for something in your history, press the question mark (?) and then the up arrow. If you go too far with the up arrow, you can return with the down arrow. If you’re familiar with the command history provided by the bash shell, it’s very similar.
This section has provided you with a minimal set of search commands with which you can maneuver the file.
Inserting Text
There are probably 20 commands to insert text, and the truly efficient VI user knows and regularly uses most of them. However, only a few are necessary. Strictly speaking, you could do everything with the i command (Insert before the current character). You could navigate to the character before which you want to insert, and the press i key. Use of only the i key would preclude easily inserting at the end of a line. What you’d need to do is navigate to the end of the line, press the i key, insert the character previously at the end of the line, insert text you want to insert, return from insert mode, and then delete the extra end character. Ugh!
That’s why VI has the insert after (a) command, which inserts after the current key. So you’d maneuver to the end of the line, press the a key, and type what you want at the end of the line.
Here is a summary of enough insert commands to make your life easy:
Esc
Return from insert mode to command mode.
i
Insert before the current character. Observe that this gives you no convenient way to insert at the end of the line.
a
Insert after the current character. Observe that this gives you no convenient way to insert at the beginning of the line.
I
Insert at the beginning of the line.
A
Insert at the end of the line. This is needed often and is a real time saver.
o
Open a brand new empty line below the current one, and position the cursor at the start of that line, in insert mode.
O
Open a brand new empty line above the current one, and position the cursor at the start of that line, in insert mode.
The preceding set of commands is by no means exhaustive, but it’s enough to work with some efficiency in VI.
Deleting Text
VI gives you two ways to delete — You Asked For It, You Got It (YAFIYGI) and What You See Is What You Get (WYSIWYG). Each has its benefits. In general, go YAFIYGI for small deletions, and WYSIWYG for large or complex ones. Obviously, WYSIWYG is safer, and YAFIYGI is often faster. I use both regularly. Remember also that almost all VI implementations have at least one level of undo, so it usually isn’t the end of the world if you blow a delete.
You Asked For It, You Got It
Here are some of the most used YAFIYGI commands:
u
Undo the previous command. Very handy to correct a blown delete. Many VI implementations have multiple levels of undo. Some such implementations use a succession of u commands to do more undos, and Ctrl+R commands for redos in case you undo too far. Other implementations use u for the first undo, and Ctrl+R commands for subsequent undos. The latter, although bizarre, is the “true VI way”. Your best bet is to be alert enough that you never need more than a single level of undo.
Note that if you save often, you can always revert to the last saved version.
:e!
Revert to the last saved version of the current file.
x
Delete the character under the cursor. The cursor comes to rest on the character following the deleted one, or if the deleted character was the final character on the line, the character before the deleted one.
X
Delete the character to the left of the cursor. The cursor stays on the current character.
dd Delete the current line so that the cursor is placed on the line following the one deleted.
D Delete current character and everything to the end of the current line.
d^
Delete everything to the left of the cursor.
dG
Delete the current line and everything below. (Careful!)
dgg
Delete the current line and everything above. (Careful!)
What You See Is What You Get
WYSIWYG deletes are safer because you can review what you’ll do before you do it. Here are the commands to perform WYSIWYG deletion:
V
Begin highlighting lines of text. Use j, k, or other move commands discussed previously to extend the highlighting.
d
Delete the highlighted text
Summary
VI is an incredibly rich and powerful editor. There’s no way to learn the whole thing at once. But by mastering how to start VI, how to quit it, how to load, save and quit files, and a few commands to move around, insert text and delete text, you’ll be able to get into the game. By mastering the contents of this article, you’ll be able to walk into any UNIX shop and use the editor without looking like a total newbie.
That being said, VI offers MUCH more. Its power is almost magical. Read on…
Steve Litt is the author of the Universal Troubleshooting Process courseware. Steve can be reached at Steve Litt’s email address.
Search and Replace: Substitute and Global Commands
By Steve Litt
This article reviews the mechanics of the search and replace commands: Substitute commands, Global commands, and combinations of the two. To keep it simple, this article contains no regular expressions, so naturally all its examples are academic rather than real-world. An article later in this magazine details regular expressions, which, when combined with the information in this article, gives you complete power over a file.
The simplest search and replace occurs on a single line. For instance, to replace the first instance of “Windows” with “Linux”, use the following command:
:s/Windows/Linux/
The colon puts you in EX mode, which is what you use for search and replace, as well as global commands and many other commands.
NOTE
In some VI implementations, the EX mode has a history feature similar to the bash shell. After pressing the colon character, you can use the up arrow to recall previous commands, and the down arrow to go back to later commands if you go too far back.
The s stands for “substitute”, because that’s what you’re doing. The first forward slash signifies the beginning of the search expression. The second backslash signifies the end of the search expression and the beginning of the replace expression. The third backslash signifies the end of the replace expression.
NOTE
In many VI implementations you don’t need to use the slash character as the expression delimiter. You can use most non-alphanumeric characters (but not , ” or |). This is very handy when working with UNIX filenames, as in the following example:
:s+/usr/local/+/opt/+
Whatever character follows the :s is defined to be the delimiter character. If your implementation doesn’t support this, you can represent slashes in search and replace expressions by escaping them with backslashes, as follows:
:s//usr/local///opt//
As you can see, the escaping method is much less readable, so if you can use alternative delimiter characters, it’s a good idea.
Recalling the substitute command:
:s/Windows/Linux/
The preceding simple command replaces only the first instance on the line. Sometimes that’s what you want, and sometimes it isn’t. Often you want to replace all occurrences on the line. In that case, append the letter g (stands for Global) after the close of the replace expression, as follows:
:s/Windows/Linux/g
Perhaps you’re replacing ten or so instances, and you want to make sure you really want to replace each one. In most VI implementations you can use the letter c (stands for Confirm) at the end, in which case before each replacements you’ll be asked yes or no:
:s/Windows/Linux/gc
