1 Basic Editing
This chapter teaches you the basic set of 10 Vim commands you need to get started editing. In this chapter, you learn the following:
- The four basic movement commands
- How to insert and delete text
- How to get help (very important)
- Exiting the editor
-
Moving Around
h(left), j (down), k (up), and l (right) -
Deleting Characters
To delete a character, move the cursor over it and type x -
Undo and Redo
Type u to undo the last delete;CTRL-R (redo) -
Getting Out
To exit, use the ZZ command.This command writes the file and exits.
Unlike many other editors, Vim does not automatically make a backup file. If you
type ZZ, your changes are committed and there’s no turning back. (You can configure the Vim editor to produce backup files, as discussed in Chapter 14,“File Recovery and Command-Line Arguments.”) -
Discarding Changes
Sometimes you will make a set of changes and suddenly realize you were better off before you started. Don’t worry; Vim has a “quit-and-throw-things-away” command. It is :q!.
If you were to just type :q, Vim would display an error message and refuse to exit: No write since last change (use ! to override) -
Other Editing Commands
-
Inserting Characters at the End of a Line
The i command inserts a character before the character under the cursor.This is done with the a (append) command. -
Deleting a Line
To delete a line, use the dd command -
Opening Up New Lines
To add a new line, use the o command to open up a new linebelow
the cursor.The editor is then placed in insert mode.
If you want to open a line above the cursor, use the O (uppercase) command. -
Help
:help
Remember the impliedfor command-mode commands.
To get out of the help system, use the same command you use to get out of the editor: ZZ.
As you read the help text, you will notice some text enclosed in vertical bars (for example, |:help|).
press CTRL+] (jump to tag)
press CTRL+T (pop tag)
When you need to get help for a control character command (for example, CTRL-A, you need to spell it with the prefix CTRL-.
:help CTRL-A
The Vim editor has many different modes. By default, the help system displays the normal-mode commands. For example, the following command displays help for the normal-mode CTRL-H command:
:help CTRL-H
To identify other modes, use a mode prefix.
If you want the help for the insert-mode version of this command, prefix the key
with i_.This gives you the following command:
:help i_CTRL-H -
Using a Count to Edit Faster
Suppose you want to move up nine lines.You can type kkkkkkkkk or you can enter the command 9k.
you added three exclamation points to the end of a line by typ- ing a!!!. Another way to do this is to use the command 3a! .The count of
3 tells the a command to insert what follows (!) three times.
Similarly, to delete three characters, use the command 3x. -
The Vim Tutorial
To invoke the tutorial on UNIX, use the following command:$ vimtutor
-
Summary
You now know enough to edit with Vim. Not well or fast, but you can edit.Take
some time to practice with these commands before moving on to the next chapter. After you absorb these commands, you can move on to the more advanced commands that enable you to edit faster and easier.