VIM



Introduction

VIM (http://www.vim.org) is an enhanced version of the terminal-based text editor vi. VIM is especially useful when you are logged into a remote device via a terminal session and want to edit source code. The user interface is geared toward key combinations, which the user needs to memorize.

Basic Usage

You start VIM with the vi or vim command. To edit an existing file simply pass the path of the file as argument. A short interactive tutorial at http://www.openvim.com

Useful shortcuts

Key(s) Description
:help Open help.
:shell (also :sh) opens a shell (or drops you into the terminal session from which vim was started) from which you return via Ctrl+D
:q quits VIM if there are no unsaved changes
:q! quits VIM without saving changes
:w writes changes to file.
ZZ saves file and quit, same as :wq
i switches to insert mode where text is inserted before the character under the cursor (type a number before pressing i, then type the string, then exit insert mode with ESC to insert the string multiple times)
a switches to insert mode where text is appended after the character under the cursor
ESC returns from insert mode to normal mode
h, j, k, l moves the cursor left, down, up, right.
e moves cursor to the end of word
b moves cursor to the beginning of the word
w moves cursor to first character of next word (type a number before w to apply multiple times)
f finds the next occurrence of the string that is entered after f
* finds the next occurrence of the word under the cursor
# finds the previous occurrence of the word under the cursor
/ finds next occurrence of string that matches the given string or regular expression
n finds the next occurrence of the string searched with /
N finds the previous occurrence of the string searched with /
% jumps to matching parenthesis (the cursor needs to be over a (, {, or [ character)
0 moves the cursor to the beginning of the current line
$ moves the cursor to the end of the current line
gg moves the cursor to the beginning of the file
G moves the cursor to the end of the file
:n moves the cursor to the line number n (entering nG or ng works, too)
o inserts a new line before the current line
O inserts a new line after the current line
x deletes the character under the cursor
X deletes the character left of the cursor
r replaces the character under the cursor
d Generic delete. Combine with movement commands to delete desired text. Deleted strings are copied to the pasteboard. dw deletes the current word, d0 deletes to the beginning of the line, d$ deletes to the end of the line, dgg deletes to the beginning of the file, dG deletes to the end of the file.
y copies selection (yy or Y for copying the whole line, y<n>y for copying n lines)
p pastes string from pasteboard
. repeats the previous command
v enters selection (visual) mode, which highlights the selected text (finish by pressing d to delete, "y" to copy)
u undo
CTRL-r redo
Multiview Editing Commands
:e filename (also :o) opens the given file for editing
:split filename splits the screen horizontally and open the given file in the new window.
:sview filename same as :split but file is read-only.
:vsplit filename' splits the screen vertically and open the given file in the new window
CTRL-w CTRL-w moves cursor to next window.
n CTRL-w+ increases the vertical size of the current window by n lines.
n CTRL-w- reduces the vertical size of the current window by n lines.
n CTRL-w> increases the horizontal size of the current window by n lines.
n CTRL-w< reduces the horizontal size of the current window by n lines.
CTRL-w= makes all windows equal size
:hide closes the current window.
:only closes all windows except the current one.

Additional shortcuts are listed at vim.rtorr.com.

Search & Replace

Type

:%s/old/new/g

to search for the string old in file scope (because of %) and replace occurrences with new. See this article for more options.

Syntax Highlighting

In normal mode, you can type

:syntax on

to enable syntax highlighting and

:syntax off

to disable it. For permanent syntax highlighting you can add the following line to .vimrc in your home directory:

syntax on


Line Numbers

Turn line numbering on with the command

:set number

and off with

:set nonumber

Make line numbering turned on by default by adding

set number

to .vimrc.

Recovery

For each file that is being edited, VIM maintains a recovery file with the extension swp. If the VIM process dies unexpectedly before the edited file could be saved, editing can be continued from the recovery file. You can instruct VIM to continue editing from the recovery file by starting VIM with the -r option:

vim -r MyLastEditedFile.txt


Alternatively, when confronted with the message that a recovery file exists for the file that you opened for editing, you can open the file with the command

:recover

or the shorter :rec.