Normal mode commands (Editing)

Basic Undo and Redo

Undo

Command:Description
uu,undoUndo the most recent change
5uUndo the five most recent changes (use any number)

Please be aware that in Vim, the 'most recent change' varies according to the mode you are in. If you enter Insert Mode (i) and type out an entire paragraph before dropping back to Normal Mode (Esc), that entire paragraph is considered the most recent change.

Redo

Command:Description
Ctrl-Rred,redoRedo the most recent undone change
2Ctrl-RRedo the two most recent undone changes (use any number)

There is one other way to undo and redo changes in Vim that is handled a bit differently. When you undo a change with u, you traverse back up the nodes on a 'tree' of your changes, and pressing Ctrl-R walks back down those nodes in order. (The undo tree is a separate topic and is too complex to cover here.)

You can also use U (that is, uppercase) to remove all the latest changes on a single line (the line where your last changes were made). This does not traverse the nodes of the tree in the same way as u. Using U actually counts as a change itself - another node forward on the tree - so that if you press U a second time immediately after the first it will act as a Redo command.

Each has its uses, but u / :undo should cover most simple cases.

Completion

Completion can be used to match words used in a document. When typing a word, Ctrlp or Ctrln will match previous or next similar words in the document.

This can even be combined with Ctrl-X mode to complete entire lines. For instance type something like:

This is an example sentence.

then go to the next line and begin typing the same sentence:

Thi

and then hit Ctrlp which will result in:

This

Now still in insert mode, hit Ctrlx Ctrlp and then next word will be completed resulting in:

This is

Continue hitting Ctrlx Ctrlp until the entire line is completed.

If you know you want to complete an entire line type this:

This is an example sentence.

then on the next line type:

Thi

and hit x Ctrll to complete the line.

If the completion being done is a filename Ctrlx Ctrlf can be used to complete that directory. Type:

~/Deskt

then hit Ctrlx Ctrlf and:

~/Desktop

will be completed (if at that location). Ctrlx Ctrlf can then be repeatedly used to list the files in the Desktop.

Copy, Cut and Paste

In Vim, these operations are handled differently from what you might be used to in almost any other modern editor or word processor (Ctrl-C, Ctrl-X, Ctrl-V). To understand, you need to know a little about registers and motions.

Note: this section will not cover Visual Mode copying and cutting or range yanking as these are beyond the scope of both Normal Mode and basic editing.

Registers

Vim uses the concept of registers to handle moving text around within the program itself. Windows has a single clipboard for this purpose, which is analogous to a single register in Vim. When copying, cutting, and pasting in Vim, there are ways to use a similarly simple editing workflow (where you don't have to think about registers), but there are also much more complex possibilities.

A register is targeted for the input/output of a command by prefixing the command with " and a lowercase letter name.

Motions

A motion in Vim is any command that moves the cursor position elsewhere. When copying, cutting, and pasting in Normal Mode, the possibilities of text selection for movement are only limited by your knowledge of motions. A few will be illustrated below.

Copying and Cutting

The basic commands copy and cut operations are built on are y ('yank', for copy) and d ('delete', for cut). You'll see the similarities in the following table.

Command:Description
y{motion}Copy ('yank') text indicated by the motion into the default register
yyCopy the current line into the default register, linewise
YCopy the current line into the default register (synonym for yy)
"ayiwCopy the word the cursor is on into register 'a'
20"byyCopy twenty lines, beginning from the cursor, into register 'b'
d{motion}Cut ('delete') text indicated by the motion into the default register
ddCut the current line into the default register, linewise
DCut from the cursor to end of line into the default register (NOT a synonym for dd)
"adiwCut the word the cursor is on into register 'a'
20"bddCut twenty lines, beginning from the cursor, into register 'b'

Note: when something is copied or cut linewise, the paste behavior shown below will place text either before or after the current line (rather than the cursor). Examples follow to clarify.

Pasting

There are several ways to paste in Vim, depending on what you are trying to accomplish.

Command:Description
pPaste whatever is in the default register after the cursor
PPaste whatever is in the default register before the cursor
"apPaste the contents of register 'a' after the cursor
"cPPaste the contents of register 'c' before the cursor

So, How Do I Perform A Really Simple Cut and Paste?

If I have the following text:

1 This line should be second
2 This line should be first

I can do the simplest cut-and-paste by placing my cursor somewhere on line 1 and typing ddp. Here are the results:

1 This line should be first
2 This line should be second

What happened? dd 'Cuts' the first line (linewise) into the default register - which will only contain one thing at a time, like the Windows clipboard - and p pastes the line after the current one, which has just changed due to the dd command.

Here's a not-quite-as-simple example. I need to move a couple of words around. (This is contrived and unnecessary, but you can apply this principle to larger chunks of code.)

1 These words order out are of

I can repeat w to get to the 'o' at the front of 'order' (or b if I just typed it and realized my mistake).

Then "adaw to put 'order ' in register 'a'.

Then w to get to the 'a' in 'are'.

Following this, I would type "bdaw to put 'are ' into register 'b'. Now I have this displayed:

1 These words out of

To be clear, now 'order ' is in register 'a' and 'are ' is in register 'b', like two separate clipboards.

To arrange the words correctly, I type b to get to the 'o' in 'out', and then "bP to put 'are ' from register 'b' in front of 'out':

1 These words are out of

Now I type A to get to the end of the line, followed by SpaceEsc (assuming there was no space after 'of') and "ap to put 'order' where it belongs.

1 These words are out of order

Introduction - Quick Note on Normal Mode

In Normal Mode, commands can be entered by direct key combinations (typing u to undo the last change, for example). These commands often have equivalents in 'ex' mode, accessed by typing a colon :, which drops you into a single-line buffer at the bottom of the Vim window.

In 'ex' mode, after typing the colon you type a command name or its abbreviation followed by Enter to execute the command. So, :undoEnter accomplishes the same thing as directly typing u in Normal Mode.

You can see that the direct commands will often be faster (once learned) than the 'ex' commands for simple editing, but for completeness, wherever possible in the documentation that follows, if both are available for use then both will be shown.

Most of these commands can also be preceded with a count by prefixing or interspersing a number - typing 3dd in Normal Mode, for example, deletes three lines (beginning from the current cursor position).

Repeat the Last Change

The Repeat command, executed with the dot or period key (.), is more useful than it first appears. Once learned, you will find yourself using it often.

Command:Description
.Repeat the last change
10.Repeat the last change 10 times

So then, for a very simple example, if you make a change to line 1 by typing iIEsc, with the following result:

1 I made a mistake
2  made a mistake
3  made a mistake

Your cursor will be at position 1 of line 1, and all you need to do to fix the next two lines is press j. twice - that is, j to move down a line and . to repeat the last change, which was the addition of the I. No need to jump back into Insert Mode twice to fix those lines.

It becomes much more powerful when used to repeat macros.