Skip to content
shinokada edited this page Feb 16, 2014 · 2 revisions

Vim Resources

VIM QUICK REFERENCE CARD

Vim Tutorial

vimtutor

Learnvim

:h learnvim

Reload .vimrc

:so %

To repeat the last change.

. (dot) 

Undo

:edit! # get into the earliest saved state.

Indent

# indent a line 

>> 

# Indent three lines.

>>j.j. or >3> or 3>>

# Indent from the current line to the end of the file
>G

Delete

# Change
c

# Change word/Word to abc
cwabc # cWabc

# To delete the character under the cursor.
x 

# Delete three characters
x... # or 3x

# To delete the current line
dd 

# Delete two lines
dd.  # or d2d or 2dd

# Delete upto the end of line and ready to type.
C # or c$, c stands for change

# Delete one letter and enters Insert mode.
s # or cl

# Delete the line and enters Insert mode.
S # or ^C

# Delete a word
daw

# Delete backward
db

# Delete forward
dw

# Delete up to the second comma
d2t,

Move around

vim vikia: Moving around All the right move

# move to the beginning of the line
0

# move to the end of the line
$

# move to the first non-blank
^

# go to the last non-blank character of line
g_

# word, end and back
w # W
e # E
b # B

# Jump forward/backward one sentence.
) # (

# Jump forward/backward one paragraph
} # {

# Move to the beginning of the line and chnage to the insert mode.
I or ^i

# Move to the end of line and enters Insert mode.
A # or $a

# move backward by word
b

# move forward by word
w

# move to the beginning of the line
0

# find, find backward
f # F

# find till, find till backward
t # T

Move and insert

# Replace mode, enter insert mode with typing over existing characters
R

# Append, move to the end of line and insert mode
A

# Open below. Insert a new line under the current line and enters Insert mode.

o # or A<CR>

# Open above. Insert a new line above the current line and enters Insert mode.

O # or ko

# Undo,  redo and undo all in the line
u
^R
U

To end vim

ZZ, :x, :wq # write and quit
:q! # without write and quit
:e! # return to the original

Saving files

:w! # to overwrite
:w filename # to save to a newfile

Help

:h # or :help
ZZ # to end help
# on a tag 
^] # will move to that tag
# going back
^t
#  get the help on yank
:h yank
# get the help on ^A
:h ^a
# get the help on ^h with the insert mode
:h i_^h

Change

# find +
f+
# s deletes the character and then enters insert mode
# enter changes space + space
s + 
# repeat the last search
;
# repat
.
# repeat search and replace
;.

# Change till ?
ct? # or ct<letter>

# Change and find ?
cf? # or cf<letter>

Search

# scan line for next character 
fx
# scan line for previous charcter
Fx
# scan line for next character and move to the left of the letter x
tx
# scan line for previous character and move to the right of the letter x
Tx
# scan document for next match
/pattern<CR>
# scan document for previous match
?pattern<CR>
# will search for the word under the cursor
* 
# clear word(cw)(and change to insert mode) and type new word
cwnewword<ESC>
# next search
n
# repeat the change
.

Repeat and reverse

# repeat fx, Fx, tx, Tx
;
# reverse fx, Fx, tx, Tx
,
# repeat /pattern or ?pattern
n
# reverse /pattern or ?pattern
N

Substitue

Search and replace

s[ubstitute]
:s/target/replacement
# repeat substitue
&
# reverse it
u
# g-flag for global
:s/foo/bar/g    # Change each 'foo' to 'bar' in the current line.
:%s/foo/bar/g    # Change each 'foo' to 'bar' in all lines.

Spell checking

:set spell spelllang=en_us
# set locally
:setlocal spell spelllang=en_us
# turn off spelling
:set nospell
# find next misspelled word
]s
# find previous misspelled word
[s
# find alternatives
z= # and type number

Adding and subtracting

# add 1 to a number at or after the cursor. This will look ahead for a digit and jumps to it.
<C-a> # ctrl+a
# subtract 1 from a number
<C-x>
# add 10 to a number
10<C-a>
# subtract 180
180<C-x>

Vim tabs

# opening vim with multiple files
vim -p file1 file2
# toggle between files
gt # gT

Adding comment outs

<C-v> # select lines
I#<Esc>

Yank and paste

# yank all lines
:%y+
# paste
p

Using terminal in vim

Hit <kbd>ctrl-z</kbd> to go to terminal. Type <kbd>fg</kbd> to go back to vim.

Remove the first 2 characters of every line:

:%normal 2x

Remove first 2 characters of every line, only if they're spaces:

:%s/^  # Note: there are two spaces after ^. 

# Move indentation to left for every line:

:%normal <<

Vim’s terminology

Vim Normal
put paste
yank copy
delete(and kepp it in a register) cut

Increasing and Decreasing numbers

# to increase one
<C-a>
# to decrease one
<C-x>
Clone this wiki locally