Skip to main content

Vim

Movement

gd
Move to the closest definition of the selected variable.
gf
Open the file path currently under the cursor. This will work with plugins such as vim-node to support require('package') and other language-specific file paths.
zt
Centre the current line to the top of the screen.
zz
Centre the current line to the middle of the screen.
zb
Centre the current line to the bottom of the screen.

Formatting

gg=G
Automatically indent the entire file.
gq
Word wrap the target line. Can be combined with movement operators like gqj, and visual selections like C-v + jjj + gq.
gq{
Word wrap the previous paragraph.
gq}
Word wrap the next paragraph.

Editing

:Nt.
Copies the Nth line to below the position of the cursor.
:tN.
Copies the line the cursor is on to below the Nth line.
ciN
Changes the content between the identifier N that the cursor is currently on. E.g. ci" with the cursor on e in "hello" will remove hello and start Insert mode between the two ".
diN
Deletes the content between the identifier N that the cursor is currently on. E.g. di" with the cursor on e in "hello" will remove hello.
yiN
Yanks the content between the identifier N that the cursor is currently on. E.g. yi" with the cursor on e in "hello" will yank the text hello.
:Nd
Deletes line N.
C-a
Increments the number under the cursor.
C-x
Decrements the number under the cursor.
C-v + gC-a
In a visual block selection, sequentially increment every number in the selection.

Searching

:g/N/<command>
Applies the specified <command> against all lines matching with N (see examples).

Run the macro stored on q on every line that contains foo:

:g/foo/norm @q

Folding

zo
Open one fold level under the cursor.
zO
Expand all folds under the cursor.
zc
Close one fold level under the cursor.
zC
Close all folds under the cursor.
za
Toggle (open or close) the current fold under the cursor.
zA
Toggle (open or close) all folds in the file.
zr
Open one level of folds in the file.
zR
Expand all folds in the file.
zm
Close one level of folds in the file.
zM
Close all folds in the file.

Registers

"Ny
Yanks the selected text into register N.
"Np
Pastes the text form register N.

Marks

Marks are a means of tracking important or frequently accessed lines of code. Both local and global marks can be set, offering the potential to make navigation across files substantially easier.

See :help mark-motions for more information.

Creating global marks for files like .vimrc might be useful.

ma
Set a local mark on the current line/column of the file under the guise 'a'.
mA
Set a global mark on the current line/column of the file under the guise 'A'.
'a
Jump to the beginning of the line for mark 'a'.
`a
Jump to the line and column for mark 'a'.
]`
Jump to the next mark.
[`
Jump to the previous mark.
]'
Jump to the next mark (at the beginning of the line).
['
Jump to the previous mark (at the beginning of the line).
:marks
List all current marks.
:marks a
Show the mark registered as 'a'.
:delm!
Delete all lowercase marks.

Buffers

A buffer is an instance of a file loaded in Vim.

:ls
List all buffers in the buffer list.
:bN
Load buffer N in the current window.
:e!
Reload the current buffer, discarding any changes.
:new
Open an empty buffer, splitting the window in the process.
:enew
Open an empty buffer in the current window.
:bufdo <command>
Perform <command> across all buffers.
:%bd
Close all buffers.

Windows

A window is an area displaying the contents of a buffer.

C-W + s
Split the current buffer into two windows horizontally.
C-W + v
Split the current buffer into two windows vertically.
C-W + T
Move the current window into a new tab.
C-W + =
Make all windows in the current tab equally sized.
C-W + o
Makes the current window the only visible window.
C-W + R
Rotate all windows upwards.
C-W + r
Rotate all windows downwards.
C-W + x + N
Exchanges the current window with window N. Defaults to next window if window N is unspecified.
:windo <command>
Perform <command> across all windows.

Tabs

A tab is a group of windows displaying buffers.

:tabmN
Moves the current tab to the tab after tab N.
:tabm0
Moves the current tab to the beginning of the tab list.
:tabm+N
Moves the current tab N positions to the right.
:tabm-N
Moves the current tab N positions to the left.
:tabdo <command>
Perform <command> across all tabs.

Jump list

Vim remembers actions such as searching, substituting and marking in what it calls its "jump list". The Vim documentation has a list of commands considered "jumps". Jumps can be navigated between back and forth and span across files. Up to 100 jumps are stored.

The +jumplist feature must be available in Vim to use this. See the documentation on jumplist for more information.

See :help jump-motions for more information.

:ju
Display the jump list.
:cle
Clear the jump list.
C-I
Navigate to the next jump in the list.
C-O
Navigate to the previous jump in the list.

Miscellaneous

Random and cool commands to speed up productivity.

:so %
Reload the .vimrc file if the current buffer is .vimrc.
:so $MYVIMRC
Reload the .vimrc file if current buffer is not .vimrc.
:! <command>
Run <command> in the shell and return to Vim.
:! git blame %:p
Run git blame on the current file in the current buffer. % refers to the file in current buffer and :p grabs the full path of the file in the current buffer.
:shell
Open a full screen, dedicated shell to run commands. Use exit to return to Vim.
:redraw
Refresh the display in Vim; useful if the display in the terminal is accidentally cleared.
:terminal
Open a shell directly inside of Vim as a window, respecting all existing splits.
:mksession <file>
Create a Vim session file of all open buffers, including all windows and tabs.
:tabdo windo N
Run command N across all windows in all tabs.

Opening Vim

Neat things to remember when opening files.

vim $(<command>)
Opens up the files that are output by <command>.
vim -S <file>
Restores a Vim session file.