trevordmiller

Vim reference

My reference sheet for text editing with Vim.

View documentation

:h some_thing

Start

cd some_project_directory_root
vim

Write

:w

Write and quit

:wq

Switch to a shell

:sh
# Run shell commands
<ctrl d>

Explore directories

:e some_directory

Examples:

:e src/
:e .

Open files

:e some_file

Examples:

:e **/*some_file<tab>
:e src/**/*some_file<tab>
:e **/*<tab>

Search project for a pattern

:vim /some_pattern/ some_path
:cn/p # to jump between quickfix results
:copen # to show all quickfix results

Examples:

:vim /test/ **/*
:vim /fn/ **/*.rs
:vim /#/ **/.*

Page

<ctrl f/b>

Repeat

.

Undo

u

Redo

<ctrl r>

Split windows

:vs

Go to window

<ctrl w h/j/k/l>

Go in/out (forward/back)

<ctrl i/o>

Go to file

# Cursor over import
gf

Complete word

<ctrl n>
<ctrl n/p> # to move through list items

Complete line

<ctrl x><ctrl l>
<ctrl n/p> # to move through list items

Substitute a pattern in a range

:some_range s/some_pattern/some_replacement/g

Examples:

:%s/foo/bar/gc

Apply commands to each quickfix item

:cdo some_command

Examples:

# Populate the quickfix with something like :vim[grep]
:cdo normal d/foo

Apply commands to each quickfix file

:cfdo some_command

Examples:

# Populate the quickfix with something like :vim[grep]
:cfdo %s/foo/bar/g

Apply generic commands across lines in a file (macro)

qq
# Complete generic commands for a line
q
:some_range normal @q

Apply generic commands across files (macro)

qq
# Complete generic commands for a file
q
# Populate the quickfix with something like :vim[grep]
:c[f]do normal @q | :w

Apply ex commands to patterns in a file

:some_range g/some_pattern/some_command

Examples:

:g/delete_me/d

Re-run commands

:some_search
# <up/down> to move through list of items
# <enter> to run

Compose commands

Operators, motions, text objects etc. can be composed like a sentence.

Examples:

# search for 'foo'
/foo

# delete up to 'foo'
d/foo

# delete word
dw

# change to end of line
c$

# change inside braces
ci{

# yank around brackets
ya[

# clipboard register put
"+p

# format to end of file
=G

# etc.

Search file for a pattern

/some_pattern
n # to move to the next

Change

c

Delete

d

Yank (copy)

y

Put (paste)

p

Inside

i

Around

a

Parenthesis

(

Braces

{

Brackets

[

Double quotes

"

Single quotes

'

Backtick

`

Tag

t

Word

w

Start of line

^

End of line

$

File top

gg

File bottom

G

Format

=

Clipboard register

"+