Skip to content

Instantly share code, notes, and snippets.

@rosswd
Last active October 29, 2021 17:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rosswd/7477004 to your computer and use it in GitHub Desktop.
Save rosswd/7477004 to your computer and use it in GitHub Desktop.
Vim cheatsheets

Administrative Commands

Runtime Commands

Path to runtime executable

:echo $VIMRUNTIME

Example Output

/usr/local/share/vim/vim74

Show path order

:set runtimepath?

Use :set rtp as a shorter alternative to same command.

Example Output

~/.vim, /usr/local/share/vim/vimfiles, /usr/local/share/vim/vim74, /usr/local/share/vim/vimfiles/after, ~/.vim/after

Version

Show advanced version info

:version

Settings

Check if compatible with vi mode is on or off

:set compatible?

Plugins

Check if plugins are set to load

:set loadplugins?

List all sourced plugin scripts (pathogen plugin mgr should appear before other plugins)

:scriptnames

Troubleshooting Tips

  • Backup up your .vimrc and create a new one with minimal config.
    • If testing pathogen plugin just put execute pathogen#infect(), syntax on, filetype plugin indent on in .vimrc

Buffers and Windows

Buffers

Open an empty buffer in the current window.

:new

Buffers will become hidden when they are abandoned.

:set hidden

List open buffers.

:ls
:buffers

Next buffer, Previous buffer

:bn, :bp

Go to third buffer, for example

:b3
:buf 3

Windows

Open a file in current window

:e filename.txt

Open a file, horizontal split

:split filename.txt
:sp filename.txt

Open a file, vertical split

:vsplit filename.txt
:vsp filename.txt

Move cursor to previous window.

C-w-w

Close this window.

C-w-c

Make this the only window.

C-w-o

Move cursor to hjkl arrow window.

C-w-(h,j,k,l)

Combining commands

Operation + Motion

Operations:

  • d (delete)
  • y (yank)
  • c (change)

Motions:

  • w (until the start of the next word, EXCLUDING its first character)
  • e (to the end of the current word, INCLUDING the last character)
  • $ (to the end of the line, INCLUDING the last character)
  • 0 (from cursor to bol)

Delete the word vans from trucks vans cars

dw while on the v

=> trucks_(c)ars

note a space is left intact and the cursor, () ends up on the c.

Delete the word vans from trucks vans cars

de while on v

=> trucks_(_)cars

note two spaces are left this time and the cursor ends up one space left.

() = cursor, _ = space

Some possible operation+motion combinations:

dw, de, d$, d0
yw, ye, y$, y0
cw, ce, c$, c0

Quantity + Motion

motions: w, e, $, 0

quantity: 1 to n

0 (move to the start of the line)

2w (move cursor two words forward)

4b (move cursor two words back)

Move cursor to the end of the third word forward. If at start of word, this word is included. If at end, it will move three whole words.

3e

Quantity + Operation)

operations: dd, d

quantity: 1 to n

5dd (delete the next 5 lines)

d2w (delete two words)

Command Mode - Copy/Paste/Select

Text must be selected first for most (if not all) of these commands. V selects a line, v selects a character etc.

Cut(yank), Paste(put)

Yank to buffer current line:

yy or Y

Yank from cursor to bol, yank from cursor to eol:

y0, y$

Put (paste) after position or after line, put before position or before line:

p , P

Yanking characters or words. Enter visual mode, select text, yank then put:

v then hjkl then y then p

Yanking lines. Enter visual mode, select text, yank then put:

V then hjkl then y then p

Changing/Replacing/Joining

Changing characters or words. Enter visual mode, select text, yank then put:

c then hjkl then y then change text.

At the end of this line, join the beginning of the next line:

J

Change word. change command is a delete command that remains in insert mode:

cw

Change to end of line:

C

Change line:

cc

With cursor on f of function selects whole function block:

v$%

Inserting

Insert text from external file foo.txt:

:r foo.txt

Indenting

Indent one or more lines:

> + h,j,k,l

Un-indent one or more lines:

< + h,j,k,l

Indent n following lines, confirm with enter:

2> <enter>

Unindent n following lines, confirm with enter:

2< <enter>

Comments

Comment a block of code:

  • C-v one space to left of block
  • Use movement keys to select a vertical row
  • I
  • Enter //
  • Esc
  • Wait
  • Done

Uncomment a block of code:

  • C-v on first forward slash (comment)
  • Use movement keys go down and right (select all the //)
  • x to delete
  • Esc
  • Done

Command Mode - Deleting

Delete character to right, left.

dx , dX

Delete word at cursor.

dw

Delete the preceding word.

db

Delete from cursor to eol.

D, d$

Delete entire line.

dd , :d

Delete this number # of lines from cursor. e.g. 4dd would delete the next 4 lines:

#dd

Delete to this number # this character x, e.g. d2f. delete to 2nd .

d#fx

Delete character.

x

Delete this number of characters. e.g. 10x deletes 10 characters.

#x

Undo last action, undo changes on current line.

u , U

Redo. i.e. undo the undo's.

C-r

Repeat last command.

.

Delete everything on line to the left of the cursor.

d0

Delete everything on line to the right of the cursor.

d$

Deletes the character under the cursor, clears current line. Both enter insert mode after.

s, S

Text deleted with d, c or x is also copied.

Executing files and using the command line from within vim

Launch an external command.

:! [command]

Print working directory.

:pwd

List buffers. :ls

Short Directory listing.

:! ls

Long Directory listing.

:! ls -l

Long alphabetical listing.

:! ls -la

Delete File.

:! rm newfile.txt

Exit vim Shell:

esc

Connect to mysql on Mac OS 10.

:cd /Applications/MAMP/Library

! bin/mysql --host=127.0.0.1 --port=8889 -u[user] -p[password] ! bin/mysql --host=127.0.0.1 --port=8889 -uroot -p12345

Overview: Select Text to write (output) to a file.

  1. V (enters visual mode)
  2. h,j,k,l (select text)
  3. :
  4. Vim adds '<,'> to command, :'<,'>
  5. w (to write file)
  6. (name of file)

v + h,j,k,l + : (+'<,'>) + w + <filename>

:!'<,'>w filename.txt

Read (input) file contents into current file:

:r filename.txt (file contents are placed below cursor line)

Read shell command output into file:

:r! echo $PATH

Read shell long listing output into file:

:r! ls -l

Read network configuration into file:

:r! ifconfig

Markers and Macros

Named markers may be set on any line of a file. Any lower case letter may be a marker name. Markers may also be used as the limits for ranges.

mc (set marker c on this line)

`c\ (go to marker c)

'c (go to first non-blank in that line)

A-Z markers are global, a-z markers are pre-buffer.

`. refers to the position of the last modification.

qa (start recording macro)

q (stop recording)

@a (replay macro)

@@ (replay last macro played)

Command Mode - Movement

h,j,k,l (left,down,up,right)

- , + (previous line, next line)

w , W (next word, next blank delimited word)

b , B (beginning of word, beginning of blank delimited word)

e , E (end of word, end of blank delimited word)

( , ) (sentence back, sentence forward)

{ , } (paragraph back, paragraph forward)

0 , $ (beginning of line, end of line)

gg (beginning of file)

G (end of file)

nG , :n (line n)

H , M , L (top, middle, bottom of screen)

C-f (move forwards one screen or page, in a file)

C-b (move backwards one screen or page, in a file)

C-d (scroll down in the file)

C-o (last cursor position)

C-i (next cursor position)

spacebar (advance the cursor one position)

f+x (search forward for a character, where x is the character)

f+x then ; (same as above but to find the next instance on the line use repeated ;)

F+x (search backwards for a character, where x is the character)

t+x (search forward for a character, where x is the character, but stop before the character)

T+x (same above but backwards)

* (with cursor on a word, move to next word)

# (with cursor on a word, move to previous word)

% (on {}, (), [] will find next matching one)

Ranges

Ranges may precede most 'colon' commands and cause them to be executed on a line or lines. For example, :3,7d would delete lines 3 - 7.

Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.

:n,m (lines n-m)

:. (current line)

:$ (last line)

:'c (marker c)

:% (all lines)

:g/pattern/ (all matching patterns in file)

:w <file> (write file, current file if no name given)

:w >> <file> (append file, current file if no name given)

:r <file> (read file after line)

:r !program (read program output)

:n, :p (next file, previous file)

:e (edit new file)

:.!program (replace line with program output)

~/.vimrc (location of global config file)

RegExp

. (any single character except newline)

* (zero or more repeats)

[...] (any character in the set)

[^ ...] (any charactoer not in the set)

^ , $ (beginning, end of line)

\< , \> (beginning, end of word)

\(...\) (grouping)

\n (contents of nth grouping)

Command Mode - Registers

This command may be specified before any change, deletion, yank or put command.

adw (delete a word into register a)

ap (put the contents of the register back into the file)

kyy (yank a line into register k)

kp (put the line back into the file)

" and an a-z character before any yank/delete/put command chooses a register.

An A-Z register before yank/delete means append-copy.

"* or "+ selects the system clipboard.

Command Mode - Replacing

:s/old/new (replaces text on a single line. e.g. s:/house/cabin)

:s/old/new/g (replace all occurences on a single line, g=global)

:%s/old/new/g (replace all occurences in the whole file)

:%s/old/new/gc (same as above with confirmation, c=confirm)

:#,#s/old/new/gc (# are line number ranges: e.g. :2,:8s/foo/bar replaces all occurances of 'foo' with 'bar' only within lines 2 to 8)

& (repeat last :s command)

:g/old/new (replaces all occurance of text. e.g. :g/goat/cowboy)

~ (toggle case)

Command Mode - Searching

/"string" (search forward, for a word in the file, from the start)

/"string"/+n (search forward, for a word and include next n lines. e.g. /loopers/+10)

?"string" (search for a word in the file, from the end, i.e backwards. e.g. ?cars searches for cars)

n , N (searches for next instance of the word, previous instance)

C-o (go back to last position)

C-i (go forward)

Search is interpreted as a regex, so a*b means zero or more a's followed by a b, ^abc means abc at the beginning of a line, [0-9] looks for the next digit, etc.

Vim Modes - Switching

Insert before cursor, before line.

i , I

Append after cursor, after line.

a , A

Open new line after the current line, before current line.

o , O

Replace one character, replace one word.

r , R re (turns hen into han, h[e]n => han)

Visual mode (characters), visual mode (lines)

v , V

Change.

c

Change word.

cw

Change word from position until end of word.

ce

Exit insert mode.

esc

Opening, Saving and Quitting Vim

Start vim. (note: vim starts in command mode)

vim

Open this readme.md. Create if file doesn't exist.

vim readme.md

Edit readme.md:

:e readme.md

Open readme.md for viewing only (read-only):

:view readme.md

Exit, saving changes if any.

:x

Quit, also gets you out of help.

:q

Quit without saving. No warning.

:q!

Save and quit.

:wq

Save readme.md but don't close it.

:w readme.md

Show status.

C-g

Show list.

C-d
:e <C-d>

Tab complete

<tab>
:e read <tab>

Help related commands for vim

Open help system

:h, :help

Close help system

:q

Jump from one window to another

C-w C-w

Place cursor on tag and jump to a subject

C-]

Jump Back

C-o

Looking for normal mode help: (no prepend)

shell

:help w
:help user-manual
:help c_ C-d
:help insert-index

Looking for visual mode help: (prepend with v_)

:help v_u

Looking for insert mode help: (prepend with i_)

:help i_<Esc>

Looking for command line help: (prepend with :)

:help :quit

Looking for command line editing help: (prepend with c_)

:help c_<Del>

Looking for vim command argument help: (prepend with -)

:help -r

Looking for option help: (enclose in '')

:help 'textwidth'

Searching for help:

:help word, then C-d to see matching entries for word

:help grep

Snippets used in command mode

  • :set shiftwidth=4 - useful for when indenting code and you don't want extra spaces
  • :u - undo last action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment