Vim Guide

ยท 2480 words ยท 12 minute read

Vim Guide based on vim.rtorr.com

Global ๐Ÿ”—

colon help keyword

open help for keyword

colon saveas file

save file as

colon close

close current pane

uppercase K

open man page for word under the cursor

Tip ๐Ÿ”—

Run vimtutor in a terminal to learn the first Vim commands.

Cursor movement ๐Ÿ”—

h

move cursor left

j

move cursor down

k

move cursor up

l

move cursor right

g j

move cursor down (multi-line text)

g k

move cursor up (multi-line text)

uppercase H

move to top of screen

uppercase M

move to middle of screen

uppercase L

move to bottom of screen

w

jump forwards to the start of a word

uppercase W

jump forwards to the start of a word (words can contain punctuation)

e

jump forwards to the end of a word

uppercase E

jump forwards to the end of a word (words can contain punctuation)

b

jump backwards to the start of a word

uppercase B

jump backwards to the start of a word (words can contain punctuation)

g e

jump backwards to the end of a word

g uppercase E

jump backwards to the end of a word (words can contain punctuation)

%

move to matching character (default supported pairs: open parenthesis closed parenthesis, open curly bracket closing curly bracket, open square bracket closing square bracket - use colon h match pairs in vim for more info)

0

jump to the start of the line

Caret

jump to the first non-blank character of the line

$

jump to the end of the line

g_

jump to the last non-blank character of the line

g g

go to the first line of the document

uppercase G

go to the last line of the document

5gg or 5 uppercase G

go to line 5

g d

move to local declaration

g uppercase D

move to global declaration

f x

jump to next occurrence of character x

t x

jump to before next occurrence of character x

uppercase F x

jump to the previous occurrence of character x

uppercase T x

jump to after previous occurrence of character x

semicolon

repeat previous f, t, uppercase F or uppercase T movement

comma

repeat previous f, t, uppercase F or uppercase T movement, backwards

closing curly bracket

jump to next paragraph (or function/block, when editing code)

open curly bracket

jump to previous paragraph (or function/block, when editing code)

z z

center cursor on screen

Ctrl e

move screen down one line (without moving cursor)

Ctrl y

move screen up one line (without moving cursor)

Ctrl b

move back one full screen

Ctrl f

move forward one full screen

Ctrl d

move forward 1/2 a screen

Ctrl u

move back 1/2 a screen

Tip ๐Ÿ”—

Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert mode - inserting/appending ๐Ÿ”—

i

insert before the cursor

uppercase I

insert at the beginning of the line

a

insert (append) after the cursor

uppercase A

insert (append) at the end of the line

o

append (open) a new line below the current line

uppercase O

append (open) a new line above the current line

e a

insert (append) at the end of the word

Ctrl h

delete the character before the cursor during insert mode

Ctrl w

delete word before the cursor during insert mode

Ctrl j

begin new line during insert mode

Ctrl t

indent (move right) line one shiftwidth during insert mode

Ctrl d

de-indent (move left) line one shiftwidth during insert mode

Ctrl n

insert (auto-complete) next match before the cursor during insert mode

Ctrl p

insert (auto-complete) previous match before the cursor during insert mode

Ctrl r x

insert the contents of register x

Ctrl o x

Temporarily enter normal mode to issue one normal-mode command x.

Escape key

exit insert mode

Editing ๐Ÿ”—

r

replace a single character.

uppercase R

replace more than one character, until Escape key is pressed.

uppercase J

join line below to the current one with one space in between

g uppercase J

join line below to the current one without space in between

g w i p

reflow paragraph

g Tilde

switch case up to motion

g u

change to lowercase up to motion

g uppercase U

change to uppercase up to motion

c c

change (replace) entire line

c $ or uppercase C

change (replace) to the end of the line

c i w

change (replace) entire word

c w or c e

change (replace) to the end of the word

s

delete character and substitute text

uppercase S

delete line and substitute text (same as cc)

x p

transpose two letters (delete and paste)

u

undo

uppercase U

restore (undo) last changed line

Ctrl r

redo

period

repeat last command

Marking text (visual mode) ๐Ÿ”—

v

start visual mode, mark lines, then do a command (like y-yank)

uppercase V

start linewise visual mode

o

move to other end of marked area

Ctrl v

start visual block mode

uppercase O

move to other corner of block

a w

mark a word

a b

a block with open parenthesis closed parenthesis

a uppercase B

a block with open curly bracket closing curly bracket

a t

a block with less than greater than tags

i b

inner block with open parenthesis closed parenthesis

i uppsercase B

inner block with open curly bracket closing curly bracket

i t

inner block with less than greater than tags

Escape key

exit visual mode

Tip ๐Ÿ”—

Instead of b or uppercase B one can also use open parenthesis or open curly bracket respectively.

Visual commands ๐Ÿ”—

greater than

shift text right

less than

shift text left

y

yank (copy) marked text

d

delete marked text

tilde

switch case

u

change marked text to lowercase

uppercase U

change marked text to uppercase

Registers ๐Ÿ”—

colon registers or colon reg

show registers content

double quote x y

yank into register x

double quote x p

paste contents of register x

double quote + y

yank into the system clipboard register

double quote + p

paste from the system clipboard register

Tip ๐Ÿ”—

Registers are being stored in tilde slash period vim info, and will be loaded again on next restart of vim.

Tip Special registers ๐Ÿ”—

0

last yank

double quote

unnamed register, last delete or yank

%

current file name

hash sign

alternate file name

star

clipboard contents (X11 primary)

plus

clipboard contents (X11 clipboard)

slash

last search pattern

colon

last command-line

period

last inserted text

minus sign

last small (less than a line) delete

equal sign

expression register

underscore

black hole register

Marks and positions ๐Ÿ”—

colon marks

list of marks

m a

set current position for mark A

backtick a

jump to position of mark A

y backtick a

yank text to position of mark A

backtick 0

go to the position where Vim was previously exited

backtick double quote

go to the position when last editing this file

backtick period

go to the position of the last change in this file

backtick backtick

go to the position before the last jump

colon jumps or colon j u

list of jumps

Ctrl i

go to newer position in jump list

Ctrl o

go to older position in jump list

colon changes

list of changes

g comma

go to newer position in change list

g semicolon

go to older position in change list

Ctrl closing square bracket

jump to the tag under cursor

Tip ๐Ÿ”—

To jump to a mark you can either use a backtick or an apostrophe. Using an apostrophe jumps to the beginning (first non-blank) of the line holding the mark.

Macros ๐Ÿ”—

q a

record macro a

q

stop recording macro

@ a

run macro a

@ @

rerun last run macro

Cut and paste ๐Ÿ”—

y y

yank (copy) a line

2 y y

yank (copy) 2 lines

y w

yank (copy) the characters of the word from the cursor position to the start of the next word

y i w

yank (copy) word under the cursor

y a w

yank (copy) word under the cursor and the space after or before it

y $ or uppercase Y

yank (copy) to end of line

p

put (paste) the clipboard after cursor

uppercase P

put (paste) before cursor

g p

put (paste) the clipboard after cursor and leave cursor after the new text

g P

put (paste) before cursor and leave cursor after the new text

d d

delete (cut) a line

2 d d

delete (cut) 2 lines

d w

delete (cut) the characters of the word from the cursor position to the start of the next word

d i w

delete (cut) word under the cursor

d a w

delete (cut) word under the cursor and the space after or before it

d $ or uppercase D

delete (cut) to the end of the line

x

delete (cut) character

Indent text ๐Ÿ”—

greater than greater than

indent (move right) line one shiftwidth

less than less than

de-indent (move left) line one shiftwidth

greater than %

indent a block with open parenthesis closed parenthesis or open curly bracket closing curly bracket (cursor on brace)

greater than i b

indent inner block with open parenthesis closed parenthesis

greater than a t

indent a block with less than greater than tags

3 equals equals

re-indent 3 lines

equals %

re-indent a block with open parenthesis closed parenthesis or open curly bracket closing curly bracket (cursor on brace)

equals i uppercase B

re-indent inner block with open curly bracket closing curly bracket

g g equals uppercase G

re-indent entire buffer

closing square bracket p

paste and adjust indent to current line

Exiting ๐Ÿ”—

colon w

write (save) the file, but don’t exit

colon w exclamation point sudo tee %

write out the current file using sudo

colon w q or colon x or uppercase Z uppercase Z

write (save) and quit

colon q

quit (fails if there are unsaved changes)

colon q exclamation point or uppercase Z uppercase Q

quit and throw away unsaved changes

colon w q a

write (save) and quit on all tabs

Search and replace ๐Ÿ”—

slash pattern

search for pattern

question mark pattern

search backward for pattern

back slash v pattern

‘very magic’ pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)

n

repeat search in same direction

uppercase N

repeat search in opposite direction

colon % s slash old slash new slash g

replace all old with new throughout file

colon % s slash old slash new slash g c

replace all old with new throughout file with confirmations

colon n o h

remove highlighting of search matches

Search in multiple files ๐Ÿ”—

colon vim or colon vim grep slash pattern slash files

search for pattern in multiple files

example

colon vimgrep slash foo slash wildcard wildcared slahs wildcard

colon c n or colon c next

jump to the next match

colon c p or c previous

jump to the previous match

colon cope or colon c open

open a window containing the list of matches

colon c cl or colon c close

close the quickfix window

Tabs ๐Ÿ”—

colon tab new or colon tab new {page.words.file}

open a file in a new tab

Ctrl w uppercase T

move the current split window into its own tab

g t or colon tab n or colon tab next

move to the next tab

g uppercase T or colon tab p or colon tab previous

move to the previous tab

9 g t

move to tab number 9

colon tab m or colon tab move 5

move current tab to the 5th position (indexed from 0)

colon tab c or colon tab close

close the current tab and all its windows

colon tab o or colon tab only

close all tabs except for the current one

colon tab d o command

run the command on all tabs (example colon tab d o q - closes all opened tabs)

Working with multiple files ๐Ÿ”—

colon e or clone edite file

edit a file in a new buffer

colon b n or colon b next

go to the next buffer

colon b p or colon b previous

go to the previous buffer

colon b d or colon b delete

delete a buffer (close a file)

colon b 9 or colon buffer 9

go to a buffer by index 9

colon b or colon buffer file

go to a buffer by file

colon ls or colon buffers

list all open buffers

colon s p file or colon split file

open a file in a new buffer and split window

colon v s file or colon v split file

open a file in a new buffer and vertically split window

colon v e r t ba or colon vertical ball

edit all buffers as vertical windows

colon tab space ba or colon tab space b all

edit all buffers as tabs

Ctrl w s

split window

Ctrl w v

split window vertically

Ctrl w w

switch windows

Ctrl w q

quit a window

Ctrl w x

exchange current window with next one

Ctrl w =

make all windows equal height & width

Ctrl w h

move cursor to the left window (vertical split)

Ctrl w l

move cursor to the right window (vertical split)

Ctrl w j

move cursor to the window below (horizontal split)

Ctrl w k

move cursor to the window above (horizontal split)

Ctrl w uppercase H

make current window full height at far left (leftmost vertical window)

Ctrl w uppercase L

make current window full height at far right (rightmost vertical window)

Ctrl w uppercase J

make current window full width at the very bottom (bottommost horizontal window)

Ctrl w uppercase K

make current window full width at the very top (topmost horizontal window)

Diff ๐Ÿ”—

z f

manually define a fold up to motion

z d

delete fold under the cursor

z a

toggle fold under the cursor

z o

open fold under the cursor

z c

close fold under the cursor

z r

reduce (open) all folds by one level

z m

fold more (close) all folds by one level

z i

toggle folding functionality

closing square bracket c

jump to start of next change

open square bracket c

jump to start of previous change

d o or colon diffg or colon diff get

obtain (get) difference (from other buffer)

d p or colon diff p u or colon diff put

put difference (to other buffer)

colon diff this

make current window part of diff

colon dif or colon diff update

update differences

colon diff o or colon diff off

switch off diff mode for current window

Tip ๐Ÿ”—

The commands for folding (example z a) operate on one level. To operate on all levels, use uppercase letters (example z A).

Tip ๐Ÿ”—

To view the differences of files, one can directly start Vim in diff mode by running vim diff in a terminal. One can even set this as git difftool.

Source: Vim Cheat Sheet