Bookmark this page

Editing with vim

  • Fast cursor commands: wb(){}.

  • c enter change mode.

  • d and y to cut and copy, p to paste.

Objectives

After completing this section, students should be able to:

  • Use movement shortcuts.

  • Copy and paste text.

  • Use search and replace.

  • Undo (and redo) their actions.

Advanced editing with vim

Movement

In addition to the boring old single character/line cursor movements one can perform in command mode, there are also quite a few advanced movement commands to help users navigate documents more efficiently. These shortcuts allow the cursor to be moved per word, sentence, or paragraph. Keep in mind that unlike regular cursor movements, these commands only work in command mode, and not in insert mode.

KeyResult
w Move cursor to beginning of next word (W includes punctuation).
b Move cursor to beginning of previous word (B includes punctuation).
( Move cursor to beginning of current or previous sentence.
) Move cursor to beginning of next sentence.
{ Move to beginning of current or previous paragraph.
} Move cursor to beginning of next paragraph.

All movement commands can be prefixed by typing a number, e.g., 5w to move the cursor five words, or 12j to move the cursor 12 lines down. In fact, every single command (including switching to insert mode) can be repeated a fixed number of times by typing the number of repeats before the actual command. In vim terminology, this is referred to as the count.

Replacing text

vim allows users to easily replace large (and small) amounts of text using a change command. The change command works by pressing the c key, followed by a cursor movement; for example, cw to change from the current cursor position to the end of the current word. The text to be replaced is deleted (and put on the unnamed register), and the vim switches to insert mode.

There are a few shortcuts available to make editing even more efficient:

  • Pressing c twice (cc) will start replace in a line-wise fashion, replacing the entire line (or multiple lines when prefixed with a number). This same trick applies to a number of other commands (such as delete) as well.

  • Most movement commands can be prefixed with i and a to select the inner or a version of the movement. For example, ciw will replace the entire current word, not just from the current cursor position, and caw will do the same, but including any surrounding white space.

  • To replace to the end of the line, one can use c$, but C does the same. (This trick also applies to various other commands, such as deleting.)

  • To just replace the character under the cursor, press r followed by the new character.

  • To change the case of the character under the cursor, press ~.

Deleting text

Deleting text works the same as replacing text. The command for deleting text is d, and all the same movements that are valid for changing text apply to deleting as well, including D to delete from the cursor to the end of the line.

To just delete the character under the cursor, use x.

Copy and paste

vim uses slightly different terminology to describe copy and paste operations than most people are used to currently. A copy operation is called yank, and paste is called put. This is reflected in the keyboard commands assigned to these operations: yank is y followed by a movement, and put operations are performed with p and P.

Yank operations follow the same general schema as replace and delete operations: A user optionally types the number of times to repeat an operation, followed by y, followed by a movement. For example, 5yaw will copy the current word and the next four (for a total of five). Pressing yy will yank the entire line, etc.

Putting (pasting) is done with the p and P commands; lowercase p will put after the current cursor (or below the current line when line-wise data is being pasted), while uppercase P puts before the current cursor position, or above the current line. Like all other commands, a put command can be prefixed with the number of times to paste the register.

Multiple registers

Instead of just one clipboard for copy and paste, vim has 26 named registers, and a number of special purpose registers as well. Having multiple registers available allows users to more efficiently cut and paste, without having to worry about losing data or moving the cursor around too much. If a register to use is not specified, the unnamed register will be used. Normal registers are called a to z, and are selected by putting "registername between the count for a command and the actual command; for example, to copy the current line and the next two into the t register, one can use the command 3"tyy.

To put out of a named register, simply put "registername in front of the put command; for example, "sp will put after the cursor out of the s register.

Note

Whenever a named register is used, the unnamed register will be updated as well.

Delete and change operations can be prefixed with a register selection as well. When no register is specified, only the unnamed register will be used. When the uppercase version of a register is used, the text that is being cut or yanked is appended to that register instead of overwriting it.

Special registers

There are 10 numbered registers, "0 through "9. Register "0 will always have a copy of the most recent yanked text, while register "1 will have a copy of the most recent deleted text. When new text is changed or deleted, the contents of "1 will shift into "2, "2 into "3, etc.

Note

Unlike the named registers, the content of the numbered registers is not saved between sessions.

Visual mode

To avoid having to constantly count the number of lines, words, or characters to specify for commands, vim also comes with a Visual (select) mode. After entering visual mode (indicated by --VISUAL-- in the ruler), any cursor movements will start selecting text. Any change, delete, or yank commands issued in visual mode do not need a cursor movement part, but will instead work on the selected text.

Visual mode comes in three flavors: character-based (started with v), line-based (started with V), and block-based (started with Ctrl+V). When using gvim, the mouse can also be used to select text.

Any ex commands issued in visual mode will by default work on the selected text as well.

Searching

Searching in the current document can be started in two ways: by pressing / to search forward from the cursor position, or by pressing ? to search backward from the current cursor position. After entering search mode, a regular expression can be typed to search for, and pressing Enter will jump to the first match (if any).

To search for the next or previous match, use n and N respectively.

Bonus shortcut: * will instantly search forward for the word under the cursor.

Search and Replace

Search and replace in vim is implemented in ex mode, and uses the same syntax as one would use with sed for search and replace, including the capability to search using regular expressions:

ranges/pattern/string/flags

range can be a line number (42), a range of line numbers (1,7 for lines 1-7), a search term (/README\.txt/), % for all the lines in the current document (search and replace normally only works on the current line), or '<,'> for the current visual selection.

Two of the most common flags are g, to enable replacing more than one occurrence of pattern per line, and i, to make the current search case-insensitive.

Search and Replace example

For example, to search for every occurrence of the word cat and replace it with dog in all lines, regardless of case, but only if it's a full word, and not in something like catalog, one could use the following command:

:%s/\<cat\>/dog/gi

Undo and redo

To allow for human imperfection, vim is fitted with an undo/redo mechanism. Simply pressing u in command mode will undo the last action. If too much has been undone, pressing Ctrl+r will redo the last undo.

Bonus awesomeness: Pressing . (period) from command mode will redo the last edit action, but on the current line. This can be used to easily perform the same edit action multiple times.

References

vim(1) man page

vim built-in help

Revision: rh134-7-63a207e