Friday, February 22, 2013

Vim Editor Commands

Vim is a very powerful tool for editing text. It is widely used by programmers for source code editing and browsing. It is an improved version of the vi editor, hence stands for Vi IMproved.. It is freely distributed with most of the linux and unix systems. It can be downloaded from below link.

Graphical version of Vim is also available which is known as gVim, and it supports all of the Vim commands. It is also available for Windows.

Once you get used to Vim, you will definitely say like every programmer, "Vim is a life saviour".

Below are some of the widely used vim commands, every programmer/vim user must know:

Editing
r
replace a single character (does not use insert mode)
J
join line below to the current one
cc
change (replace) an entire line
cw
change (replace) to the end of word
c$
change (replace) to the end of line
s
delete character at cursor and subsitute text
S
delete line at cursor and substitute text (same as cc)
xp
transpose two letters (delete and paste, technically)
u
undo
.
repeat last command


Marking Text (visual mode)
v
start visual mode, mark lines, then do command (such as y-yank)
V
start Linewise visual mode
O
move to other end of marked area
Ctrl+v
start visual block mode
O
move to Other corner of block
Aw
mark a word
Ab
a () block (with braces)
aB
a {} block (with brackets)
Ib
inner () block
Ib
inner {} block
Esc
exit visual mode


Visual Commands
> 
shift right
< 
shift left
Y
yank (copy) marked text
D
delete marked text
~
switch alphabet’s case (lower to upper or upper to lower)


Cut and Paste
yy
yank (copy) a line
2yy
yank 2 lines
yw
yank word
y$
yank to end of line
P
put (paste) the clipboard after cursor
P
put (paste) before cursor
dd
delete (cut) a line
dw
delete (cut) the current word
X
delete (cut) current character


Exiting
:w
write (save) the file, but don't exit
:wq
write (save) and quit
:q
quit (fails if anything has changed)
:q!
quit and throw away changes


Search / Replace
/pattern
search for pattern
?pattern
search backward for pattern
n
repeat search in same direction
N
repeat search in opposite direction
:%s/old/new/g
replace all old with new throughout file
:%s/old/new/gc
replace all old with new throughout file with confirmations


Working with multiple files
:e filename
edit a file in a new buffer
:bnext (or :bn)
go to next buffer
:bprev (or :bp)
go to prev buffer
:bd
deletes the buffer currently active
:sp filename
open a file in a new buffer and split window
ctrl + ws
split windows
ctrl + ww
switch between windows
ctrl + wq
quit a window
ctrl + wv
split windows vertically

No comments:

Post a Comment