home vim downloads links

#inserting
#deleting
#moving
#searching
#marks
#copying
#select
#folding
#various
#command line
#options

Just a small reference about vim
Basically vim knows 3 different modes. The normal mode (e.g. navigation, searching ...). This is the mode you typically start with. In command line mode you can enter commands in one line at the bottom of the window It is used for the Ex commands (all commands starting with : ), search commands and the filter command. The last mode is the insert mode. It is used to actually enter text into the buffer currently edited. You can always switch back from any mode to normal mode by pressing ESC twice.
Have you ever wanted to just type one command from inside insert mode? Well you can. Just press CTRL-O. Then you can enter your command (possibly inserting something or deleting or whatever you like :-)).
generell structure of a vi command
"repetition" "command" "range"
repetition is a optional number that may precede the command to multiply or iterate the command. If no number is given, a value of 1 is assumed. Note that I will not explicitly mention it in the explantion.
command is the actual command to be performed.
range states how far the command will work. It is usually a command that moves the cursor.

Special Notation
Everthing that can be entered optionally is shown in a turquois color.
A capital N represents any number indicating how often the command will be performed.
{x} the x within curly braces indicates that the part within the braces must appear within the command, and can be any number or any command that positions the cursor within the text.
Meanings of special characters - normal mode

$ - last character in the current line
% - positions the cursor under the corresponding bracket
Meanings of special characters - command line mode

$ - last line
. - current line
% - whole file, corresponds to 1,$
The column vim command contains the commands (Surprisingly, isn't it?). The other column will explain shortly the command.


Inserting text

vim command Explanation
i insert text before the cursor.
I Inserting text after the cursor
a Append text after the cursor
A Append text before the cursor
o create a new line below the current one and goto insert mode (open)
O create a new line above the current one and goto insert mode
c{N} change the text, in the range N. (actually the changed text will be removed and then vi will go into insert mode)
C Changes from the actual position until end of line
Ncc Changes N lines
r Replaces exactly one character at cursor position with the one that will be pressed next
R Enter replace mode. Typed text will then replace the displayed text.
NrX Replaces N characters with character X.

deleting text

vi command explanation
d {N} Deletes all the text within the range N. The deleted Text will be copied to a register. (delete)
dd Deletes the line of the cursor
D Deletes from actual cursor position until the end of line
Nx Deletes N characters under and after the cursor.
NX Deletes N characters before the cursor

Moving within the text

vi command explanation
h moves the cursor 1 character to the left
SPACE,l 1 character to the right
j 1 line down
k 1 line up
w one word to the right (word)
W one blank separated word to the right
b one word to the left
B one blank separated word to the right
e go to the last character of the actual word (if the cursor is already at the last character of the word, go to the last character of the next right word)
E go to the last character of the blank separated word
f {char} go the first occurence of char to the right.
F {char} go to the first occurence of char to the left.
t {char} go until the first occurence of char to the right. (But stop one char before the occurence. Great for deleting)
F {char} go until the first occurence of char to the left. But stop one char before the occurence. (Great for deleting)
Return go to the beginning of the next line
- go to the beginning of the previous line
( go to the beginning of the sentence
) go to the end of the sentence
{ go to the beginning of the paragraph
} go to the end of the paragraph
NG Go to line N. If N has not been given, go to the end of the text
gg go to the first line
H go to the first line in the window
M go to the middle line in the window
L go to the last line in the window
Ctrl + d scroll window downwards (by half a window)
Ctrl + u scroll window upwards (by half a window)
Ctrl + f scroll 1 window downwards
Ctrl + b scroll 1 window upwards

Searching within the text

vi command explanation
N/{pattern} RETURN search forward from the current cursor position for the Nth occurence of {pattern}
N?{pattern} RETURN search backwards from the current cursor position for the Nth occurence of {pattern}
n repeat last search in the same direction
N repeat last search in the opposite direction
/ RETURN repeat last search in the forward direction
? RETURN repeat last search in the backward direction
* search forward for the word under the cursor
# search backwards for the word under the cursor
g* like “*”, but finds also partial matches
g# like “#”, but finds also partial matches
gd go to local declaration of the word under the cursor
gD go to the global declaration of the word under the cursos
:[range] s/expr1/expr2 /g Searches forward for expr1 and replaces it by expr2.
When /g is given all occurences of expr1 will be replaced. (If it isn't given, only the first occurence will be replaced.) If [range] is not given, vi assumes to search and replace only in the line where expr1 is first found
range consists of 2 comma separated line numbers and defines the range in which to search and replace.

Marks

vi command explanation
m{char} Mark current Position with char
'{char} go to the line where mark char is
`{char} go to the mark char
'' go to the position before the last jump
:marks show active marks
:ju print the jump list

Copying

vi command explanation
Nyy Copy N lines into a register.
Y Copy until end of line into a register
p Paste after the current line
P Paste before the current line
"{char} use register char for next copy, delete, paste

Select

Vim also knows a so called select mode. This is the mode known from MS-Windows systems. You mark anything within the text and whatever you type afterwards will replace your marked text. Vim is usually in marked mode, when you have selected something with the mouse or from the visual mode by typing CTRL-G. Alternatively you can enter select mode by entering gh, gH or g CTRL-H. The usage is as in the select mode. gh enters character select mode, gH enters Line select mode and - guess what? - g CTRL-H enters blockwise select mode.

Folding

Folding mode is really nice. You can hide part of the text so they won't be visible, possibly bothering you. There are several modes. By default it is set to manual. So you have to define what gets folded. You can set the method via :set foldmethod (See also :h foldmethod).

vi command explanation
zf When using manual fold method, create a new fold and close it.
NzF Create a new fold over the next N lines.
zd Delete fold on cursor.
zD Delete fold on cursor recursively.
zE Delete all folds in the current document.
zo Open current fold under cursor.
zO Open current fold under cursor recursively.
zc Close current fold under cursor.
zC Close current fold under cursor recursively.
za When on closed fold open it, when on opened fold close it.
zA When on closed fold open it recursively, when on opened fold close it recursively.
zi Toggle option foldenable. When on, all folds will be closed and when off, all folds will be opened, thus showing all the text.

Various useful things

vi command explanation
v Enter visuell mode characterwise. May be used for copying, deleting, pasting ...
V Enter visuell mode linewise. May be used for copying, deleting, pasting ...
CTRL + v Enter visuell mode blockwise. May be used for copying, deleting, pasting ... .
~ Switch case for the character under the cursor.
u Undo the last change. Can be done several times.
CTRL + r Redo the last undo
J Join the next line with the current one.
. Repeat the last change.
, Repeat the last search in the line (keys f or t).
CTRL-A Increment number under cursor.
CTRL-X Decrement number under cursor.
CTRL-L Redraw screen.
gf Open file under cursor.
K Lookup keyword under cursor with "man"
!! {command} Execute {command} in a shell and place output at current cursor position. Note that any other text at current position will be deleted.

Command line mode

VI command explanation
:{range}co {linenr} Copies the given range after the specified line
:{range}m {linenr} Moves the given range after the specified line
:ab {x} {text} Add Abbreviation x for text. Just enter in insert mode x Space and it will be written as text.
:ab show all abbreviations
:una {x} Remove abbreviation for x
:reg show content of all registers
:w name Save current file (with namename)
:w! Force to write current file (e.g. if it is only readonly).
:e {xyz} Open file xyz. (You can also use it to browse through the filesystem.
:q Close current file (without saving) and close vi (quit).
:qa Close all opened files (without saving) and quit vi.
:q! Quit vi and discard all changes.
:r ?{xyz} Insert contents of file xyz below current cursor position.
:sp file Split window horizontally and in the new window open the specified file. Use CTRL + w to change between the active windows.
:vsp file Splits the current window vertically and opens the specified file Use CTRL + W to change between the windows.
:set show all modified options
:set {option} Toggle between option=true and option=false
:set {option=value} Set option to value
:set {option}? show value of option
:set no{option} Set option off
:sh Start a shell
:! {command} Execute command within a shell

Brief overview of the Options

The following options show only a brief introduction. You can also set those options via the ~/.vimrc file (you can omitt the colon in that file). Notice that you always have to use :set before actually setting the option.
vi command explanation
ai autoindent lines (default off)
bg You can choose between dark and light. This is used by vim to have a pretty nice syntax highlighting according to the right backgroundcolor (default: "")
bs Backspace: Influences how Backspace works. If set to 0 vi compatible backspace is used. If set to 1, allow backspacing over newlines. If set to 2 allow backspacing over the start of insertSet option to value (default: "")
bk Backup: Keep backup file after overwriting (default off)
bin Binary: Set vim to binary mode. This should be done, before editing a binary file (default off)
cin Cindent: Enable automatic C program indenting. (default off)
cp Compatible: Change vim to vi-compatible mode. (default off)
ea Equalalways: When turned on, all windows are resized to the same size after splitting or closing a window. (default off)
eb Errorbell: Rings the bell on errors.(default off)
fdm foldmethod: Switch to any of the 6 folding methods: manual,indent, expr, marker, syntax or diff. (Default: manual) Don't know how exactly each mode works. You got a good explanation by typing: :h fdm.
fen foldenable: When set to off, all folds are shown, thus you can quickly show the complete text. When enabled all folds will be closed. It can also be toggled by typing "zi". (default: off)
hi History: How many : commands and searches are going to be remembered. (default: 20)
hls Highlightsearch: highlight all matching search patterns. (default off)
ic Ignorecase: Ignore case in search patterns. (default off)
kp Keywordprg: Program to use for the "K command. (default: man)
magic Magic: Changes the special characters that can be used in search patterns (default on)
mp Makeprg: Program to use for the :make command. (default: "make")
more More: Listings pause when the whole screen is filled (default: on)
nu Number: Set line numbering (default off)
pastetoggle Have you ever noticed that vim behaves strange, as soon as you insert with a mouse into a window? Well setting pastetoggle to a key helps. By this you can enter a special mode called paste from within pasting with the mouse should work. (e.g. set pastetoggle=<F10> From now on you can switch into paste mode via the F10 key) (default: "")
ru Ruler: Show the line and column numer of the current cursor position (default off)
sh Shell: Defines the shell to be used for the ! and sh commands. (default: $SHELL of "sh")
sw Shiftwidth: Number of spaces to be used for each step of indent (default 8)
sbr Showbreak: String to put at the start of lines which have been wrapped. (default "")
sm Showmatch: When a bracket is inserted, briefly jump to the matching one. Usefull for programming. (default off)
smd Showmode: Show in the last line, in which mode vim currently is (default on)
scs Smartcase: Overrides the 'ignorecase' option during searches, if the search patterns contain upper case characters (default off)
si Smartindent: Do smartindenting when starting a new line. Is similar to the cin option. (default off)
ts Tabstop: Number of spaces that a TAB counts for. (default 8)
tw Textwidth: Maximum of width of text that is being inserted. A longer line will be broken after white spaces to get this width. A zero value disables this option.(default 0)
title Title: When set, the title of the window will be set to "VIM - filename" (default off)
ul Undolevels: Max. number of changes that can be undonde. (default 80)
wrap When on, lines longer than the current window width will be wrap and show on the next line If off, the screen will scroll horizontally (default on)


viewable with a browser Valid HTML 4.01!