From Wikibooks, open books for an open world
Jump to: navigation, search

vim xxx yyy zzz - open one or more files (-R: open as read-only; +n: skip to line n; +/xxx: skip to string xxx;)

vim . - browse current directory


esc - goto command mode

i,I,a,A,o,O - goto insert mode

v,V,Ctrl+v - goto visual mode

Colon Commands

w - save

w myfile - save as myfile

q - quit

r myfile - paste myfile at the cursor of the current file

e myfile - edit myfile, close current file

f - show file properties

n - open next vi file in arguments list

!xxx - run xxx in the shell

!!xxx - replace current line with result of shell command xxx

!}xxx - replace current paragraph with the result shell command xxx

map - show key mappings

map key cmd - map vi cmd to key

. - show line numbers

set nonumber - turn off line numbering

s/old/new/ - search and replace

%s/search/&/g - search and count matches

1,$ s/old/new/g - global replace

g/^$/d - delete all blank lines

/xxx - search down for xxx

?xxx - search up for xxx

Search Commands

/ - RegExp search

n - repeat last search(find next)

/↑ - edit last search

Control Commands

Ctrl-b - page up

Ctrl-f - page down

Ctrl-u - half page up

Ctrl-d - half page down


a - append after cursor

A - append after current line

i - insert before cursor

r - replace at cursor

~ - transpose case of text at cursor

R - replace from cursor to end of new text

o - open newline after cursor

O - open newline before cursor

u - undo last action

Ctrl+R - redo last action

U - undo current line

yy - copy current line

nyy - copy n lines from current line

y/string/ copy from the cursor positon to the string's position, searching forward

y?string? copy from the string's position to the cursor, searchingbackward

p - paste

ddp - cut and paste current line

dd - delete current line

dw - delete current word

d$ - delete from cursor to end of line

D - delete from cursor to end of line

cc - change current line

cw - change current word

c$ - change from cursor to end of line

C - change from cursor to end of line

s - change current symbol

S - change current line

Cursor Movement

h/j/k/l - move left/down/up/right

w - move forward word-by-word

b - move backward word-by-word

e - move to end of current word

W - same as w but only uses space as delimiter

B - same as b but only uses space as delimiter

E - same as e but only uses space as delimiter

} - move forward one paragraph

{ - move backward one paragraph

) - move forward one line

( - move backward one line

^ - first non-blank character

0 - start of current line

$ - end of current line

gg - top of the file

G - bottom of the file

ngg - go to line n

nG - same as ngg

% - find match of current brace in the cursor

To store a session:


To load a session while starting vim:

   $ vim -S Session.vim

To load a session while in vim:

   :so Session.vim

Retab one or more files from the command line

   $ vim -c retab -c wq <filename(s)>

Wscript Avoid setting the file type to python by including these lines in ~/.vimrc

   1 autocmd BufRead,BufNewFile wscript set filetype = python

Clear highlighted search terms

   :let @/=""

Paste indented code into vim from another application

   :set paste
   <Terminal paste command>
   :set nopaste

Redirect Terminal output to Vim

   repoquery  -lq *gstreamer* |vim -

Write and quit a file

   Shift + zz

Read blocks of text from other file

   :r! sed -n 12,40p filename

This will paste line contents from line number 12 to 40 in current file

Save a file which requires sudo permission without exiting the file

   :w !sudo tee %
     You can add following line to your .vimrc to make this trick easy-to-use: just type :w!!.
   cmap w!! %!sudo tee > /dev/null %

Word wrap

   set wrap

Auto Indentation

   Select the block(visual mode) you want to auto indent and just press "=" button.

Copy a file to current position

   eg: 44t.

Move a line to another position

   eg: 4m5

Indent a line

   shift + >>
   shift + <<

Delete a block

   eg: 1,10 d

Set/Unset Line numbers

   :set nu
   :set nonu

See/Hide special characters in vim

   :set list
   :set nolist

Must use Vim Plugins


Open and edit your files more efficiently. Nerd Commenter

   Comment or un-comment multiple lines with keys.This plugins detects the file type and apply's the correct comment syntax.It even supports Vim's visual mode.


   List and jump to functions quickly.This requires that you have ctags installed .