Edit and save
Let's continue our rapid command-covering pace. In command-mode, you can jump to a particular line by typing <linenumber>G. To jump to the first line of a file, type 1G. Note that G is capitalized.
If you want to jump to the next occurrence of a particular text pattern, type /regexp and hit Enter. Replace regexp with the regular expression you're looking for. If you don't know how to use regular expressions, don't fret -- typing /foo will move to the next occurrence of foo. The only thing you'll need to watch out for is when you want to refer to the literal ^, ., $ or \ characters. Prefix these characters with a backslash (\), and you'll be set. For example, /foo\.gif will search for the next occurrence of "foo.gif".
To repeat the search forwards, hit n. To repeat the search backwards, type N. As always, test these commands out in your very own vi editor.
We've covered how you can use the ex command :q to quit from vi. If you want to save your changes, type :w. If you want to save your changes to another file, type :w filename.txt to save as filename.txt. If you want to save and quit, type :x or :wq.
In vim (and other advanced vi editors, like elvis), you can have multiple buffers open at once. To open a file into a new window, type :sp filename.txt. filename.txt will appear open for editing in a new split window. To switch between windows, type ^w^w (control-w twice). Any :q, :q!, :w and :x commands that you enter will only be applied to the currently-active window.
Now, it's time to start learning some of the simple editing commands. The commands that we'll cover here are considered "simple" because the commands keep you in command mode. The more complex editing commands automatically put you into insert mode -- a mode that allows you to enter literal data from the keyboard. We'll cover those in a bit.
For now, try moving over some characters and hitting x repeatedly. You'll see that x will delete the current character under the cursor. Now, move to the middle of the paragraph somewhere in your text file, and hit J (capitalized). You'll see that the J command tells vi to join the previous line to the end of the current line. Now, move over a character and hit r, and then type in a new character; you'll see that the original character has been replaced. Finally, move to any line in the file and type dd. You'll see that dd deletes the current line of text.
You can repeat any editing command by hitting the . key. If you experiment, you'll see that typing dd... will delete 4 lines, and J... will join 4 lines. As usual, vi provides another handy shortcut.
To delete text, you can also use the d command combined with any movement command. For example, dw will delete from the current position to the beginning of the next word; d) will delete up until the end of the next sentence, and d} will delete the remainder of the paragraph. Experiment with the d command and the other editing commands until you're comfortable with them.
Now that we're experimenting with deletion, it would be a good time to learn how to undo any changes. By pressing u, the original version of vi allowed you to undo the last edit only. However, modern versions of vi like vim will allow you to repeatedly press u to continue to undo changes to your file. Try combining some d and u commands together.
Time to update the cheat sheet! After adding all the commands we've covered so far, your cheat sheet should look like Figure 4:
Figure 4. Cheat sheet with vi editing commands