Skip to content

Instantly share code, notes, and snippets.

@karakays
Last active November 17, 2021 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karakays/acedd66febc191674cd5783680d7a92a to your computer and use it in GitHub Desktop.
Save karakays/acedd66febc191674cd5783680d7a92a to your computer and use it in GitHub Desktop.
vim cheat sheet

Windows

MOD Ctrl-w

" move current window to far right
L
" move current window to far left
H
" move current window to very bottom
J
" move current window to very top
K

" close all windows except current
O

" create new window with empty buffer
n

Buffers

" discard changes in buffer and reload from disk
:edit! or :e!

" read from disk into active buffer
:edit or :e

" open file explorer from cwd 
:e .

" open file explorer from directory of active buffer  
:e %%

" open advanced explorer for current buffer
:Explore or :Ex

" list buffers
:ls

" cat the file below the cursor
:read <file>

Ex

Ex commands are executed in the command-line e. While normal-mode commands act on current line, ex commands act in multiple lines with the help of ranges.

command
[range]p[rint] print lines
e[dit] read file
w[rite] write file
[range]n[ormal] {commands} execute normal-mode commands
[range]s[ubstitute]/{pattern}/string substitute
pwd print working directory
tabedit
bpref/bnext

Special characters in command line mode

char meaning
% path of active file (current buffer)
%% path of active directory (head of current buffer)
:e foo/bar/baz
:w
foo/bar/baz E212: Can't open file for writing
:!mkdir -p %%
:w

Address and range

[address] is a line number whereas [range] is set of contiguous lines starts from {address} and ends {address}.

address = line number
range = {address},{address}

special symbols

symbol address
. current line
0 top virtual line
1 first line
$ last line

% is a range symbol that resolves to 1,$ = all lines.

" go to address
:13
" print address
:2p
" print range
:2,5p
" print all lines
:%p
" a range by patterns
:/<html>/,/<\/html>/p
" copy line 6 to current line
:6t.

run normal mode commands

:{range}normal {normal-mode-cmd}

" run last movement command on all lines
:%normal .  
" append semicolon all lines  
:%normal A;  

repeat last ex command

:@:

Execute shell

:!{cmd} {args}

:!ls
:!python %

" exec cmd and output to active buffer
:read !{cmd}

" exec cmd with input of active buffer
:write !{cmd}

Navigate

Retrace with jumps

" Go back to previous active file
C-o
" Go to next file in list
<Tab> or C-i

:e, [no]G and /{pattern} count as a jump.

" jump to filename under cursor (goto file)
gf

Mark

key action mnemonic
mm set mark at cursor mark
`m go to mark m
` list marks
mM set a global mark M (persisted)

Scroll

key action mnemonic
C-j scroll lines downwards extra lines
C-k scroll lines upwards
C-d scroll window downwards downwards
C-u scroll window upwards upwards
scroll relative to the cursor
z. scroll cursor line to center
zt scroll cursor line to top top
zb scroll cursor line to bottom bottom

Quickfix/Location lists

Quickfix is global.

Open quick fix list

:copen

Display next/previous item in list

:]q
:[q

Display first/last item in list

:]Q
:[Q

Location list is window-local quickfix list. Every window has a separate location list.

Open location list

:lopen

Display next/previous item in list

:]l
:[l

Folding

MOD z

Open fold on one level at the cursor

:zo

Close fold on one level at the cursor

:zc

Open all folds

:zR

Close all folds

:zC

Various tricks

display EOL chars (\r\n)

$ vim -b
:e ++ff=unix

Inspect options and variables

Inspect option

:set <option>?

Inspect variable

:echo g:ctrlp_max_files

disable option

:set no<option>

toggle option

:set <option>!

NERDTree

locate current file

:NERDTreeFind

Move tree root up one directory

u

Change CWD in NERDTree to the dir in selected node

CD

Change CWD in vim to the dir in selected node

cd

paste from register when in insert mode

:<C-r>a
" from clipboard
:<C-r>*

paste current word under cursor to command mode

:<C-r><c-w>

change case with gu{motion}

" lower case of visualised
Vu
" upper case
VU

using vim help

:help topic
" Press <Tab> to scroll through topics
:h topic

follow links in vim help

" follow the link
Ctrl-]
" Go back to previous topic
Ctrl-T

vim-gitgutter

Go to next hunk

]c

Go to previous hunk

[c

stage hunk under cursor

\hs

Undo hunk under cursor (discard unstaged hunk)

\hu

autocomplete filenames in insert mode

Cursor over filepath in insert mode and press C-xC-f

Navigate in the list with C-n and C-p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment