Vim in 5 Minutes

Vim is a text editor.

Start it from the command line with vim or vim filename. Start the graphical version with gvim.

Vim has several modes of operation. The most commonly used modes are:

To start typing text right away, hit i to enter Insert mode.

To exit Vim (first hit Escape to return to Normal mode if you're not there):

    :wq    Write/save and quit
    :q!    Quit without saving

Get help like:

    :help
    :h topic
    :h quickref
    :h help
    :h options

Help is hyperlinked. Follow a link with Ctl-] and go back with Ctl-t.

Normal Mode

Vim starts in Normal mode.

Commands can be constructed like:

    [operator][count][motions]

Examples:

    d3w   Delete next three words
    yB    Yank/copy whitespaced word to the back/left
    y5j   Yank/copy five lines down
    2p    Past twice

Operations

d      Delete
dd     Delete line
y      Yank
yy     Yank line
c      Change
u      Undo
Ctl-r  Redo
r      Replace character
.      Repeat last operation

Motions

h      Left (like left arrow)
j      Down (like down arrow)
k      Up (like up arrow)
l      Right (like right arrow)
w      Work forward/right
b      Work back/left
W      Whitespace work forward
B      Whitespace work back
0      Start of line
$      End of line
^      Non-whitespace start of line
Ctl-f  Screen forward
Ctl-b  Screen back
gg     Start of file
G      End of file
(      Sentence back
)      Sentence forward
{      Paragraph back
}      Paragraph forward
%      Jump to matching brace
23G    Jumpt to line 23
`.     Jump to last change
*      Jump to next instance of word under cursor
#      Jump to previous instance of word under cursor
fx     Next character x on this line
Fx     Previous character x on this line
;      Repeat fx or Fx

Insert Mode

To leave insert mode, use Escape or Ctrl-[.

Entering Insert Mode

i   Insert before cursor
a   Insert after cursor
I   Insert at start of line
A   Insert at end of line
o   Insert into new line below
O   Insert into new line above

Run one quick Normal command without leaving insert: Ctl-o cmd.

Command Mode

Enter Command mode from Normal mode with : (or / or ? for search). Cancel an in-progress command and return to Normal mode with Escape.

:w      Save file
:wq     Save and quit
:q!     Quit without saving
:q      Close unchanged file
:e      Reload file from disk
:enew   New empty file
:e foo  Edit file foo
:w bar  Save as bar
/foo    Search forward for foo
?foo    Search backwards for foo
:s/Foo/Bar/g     Substitute all occurances of foo with bar
:s/foo/bar/gi    Substitute all and ignore case
:s/foo/bar/c     Substitute but ask for confirmation for each instance
:set hlsearch    Highlight search results
:set nohlsearch  Don't highlight search results
:set nu          Show line numbers
:set nonu        Hide line numbers
:set nonu        Show relative line numbers

The search commands accept regular expressions. See :h regex.

Visual Mode

v     Enter visual mode
V     Enter line-wise visual mode
Ctl-v Enter block-wide visual mode

After entering visual mode, use Normal motions to change the highlight area. Normal actions like yank/copy or delete work on the highlighted area.

Command mode commands, like substitute, will also work on the highlighted are.

Exit Visual mode with Escape. Use Normal command gv to select the last visual region again (i.e. — the /g flag will be global within the highlighted area.).

What Else Is Really Useful to Learn?

Learn about marks, registers, macro repeats, vimrc config file, and buffers.

:h mark-motions
:h registers
:h complex-repeat
:h vimrc
:h buffers

Also, using :norm is powerful, especially with recorded macros.

Links