Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active February 6, 2022 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ajaxray/776c9507894f4901eaf87bf6f9779249 to your computer and use it in GitHub Desktop.
Save ajaxray/776c9507894f4901eaf87bf6f9779249 to your computer and use it in GitHub Desktop.
Vim Cheat Sheet

Vim Cheat Sheet

My collection of vim tips to make the best editor even better. This is by no means complete or a tutorial on how to use vim, but a set of commands I don't want to forget and need to write them down before they burn into memory.

See the resources section below for a more complete introduction and a set of in-depth tutorials.

Navigation

:nn             " Jump to line nn
nn|             " Jump to column nn
H M L           " Jump dorectly to Highest, Middle, Lowest of screen
( ) { }         " Beginning, End of current sentence, paragraph
0 ^ $ g_        " First, Last of current column, line 
f F t T         " Follow, Till forward, backward 
, ;             " Repeat last Follow/Till forward, backward 

Navigation Marks

ma              " mark spot current file label it a
mA              " mark spot globally label it A
'a              " jump to spot
'A              " jump to spot (cross file)
''              " jump to last spot you were
'.              " jump to last modification
:marks          " show all marks   

Start Insert Mode

i I             " Insert before cursor, BOL
a A             " Append after cursor, EOL
o O             " Open new line after, before current line
c{motion} C|cc  " Change (delete and Insert mode) character, up to EOL
s S             " Substitute (delete and Insert mode) char, line

When v(isual), V(isual line) or Ctrl+v(isual block) mode

J               " join all the lines together.
< >             " indent to the left, right
=               " auto indent
y d ~           " yank, delete, change case of marked text

I, A, $ etc. will work too.

Edting

u Ctrl+r        " undo, redo
J               " join line below to the current one

Copy, Paste and Registers

y[movement]     " copy 
Y|yy            " copy line
"ad             " cut something to register a
p P             " Paste before, after 
"ap             " paste something from register a
"0-9p           " paste from delete history
:reg            " list registers

Few Special Registers

""             " Default register for any yank or delete
".             " Last inserted text
"+             " System clipboard
"/             " Search content
"=             " Expression result

More about registers: https://www.brianstorti.com/vim-registers/

Deleting Lines

dd             " delete line
D              " delete up to EOL    
:g/regexp/d    " delete all lines that match regexp
:v/regexp/d    " delete all lines that do NOT regexp
:v/w{3,}/d   " delete all lines with less than 3-chars
:15,20d        " delete lines 10-20

Buffer Management

:ls             " list open buffers
:b [num|name]   " switch to buffer 
:b#             " switch to last buffer
:bd #           " delete buffer
:*e*dit [file]  " open file in new buffer

Splits

:sp file        " open a file in a new split window
:vs file        " open a file in a new vertical split window
Ctrl + wq       " quit a window
Ctrl + w[dir]   " move to direction (hjkl←↓↑→) window
Ctrl + ww       " switch window
Ctrl + [+|-]    " Grow, shrink split    

Tabs

:tabnew | :tabnew file  " open a file in a new tab
Ctrl + wT               " move the current split window into its own tab
gt | :tabn              " move to the next tab
gT | :tabp              " move to the previous tab
#gt                     " move to tab number #
:tabclose | :tabc       " close the current tab and all its windows

Record Macro

qa              " start recording macro in buffer a
[do stuff]
q               " end recording

Playback Macro

@a
50@a  (50 times)

Multiple file search

:vimgrep /search-pattern/[jg] file-pattern
:vimgrep /kateb/j **/*.go
:cn[ext] | :cp[revious]         " jump to next and previous match
:cnf[ile] | :cpf[ile]           " jump to next and previous file
:cw[indow]                      " open quickfix window (list of matches)                                     

More options on vimgrep matches : https://stackoverflow.com/a/7880681/228648

Other

<C-n> | <C-p>           " Autocomplete from next|prev matches
<C-x><C-o>              " Language aware autocomplete 

Map System Command to Key Stroke

Map ctrl-j d to run system command /tmp/x.py

:imap <C-j>d <C-r>=system('/tmp/x.py')<CR>

Toggle Spellcheck

:map <F5> :setlocal spell! spelllang=en_us<CR>

Customizations

Here are a set of short cuts I have in my vimrc file that simplify some common operations. If I notice myself doing the same thing over and over, I try to add a shortcut when possible.

" Add spaces inside parentheses, WordPress coding style
map <Leader>o ci(hp

" Surround word with quote
map <Leader>' ysiw'
map <Leader>" ysiw"

" Add Trailing Semi-colon
map <Leader>; g_a;<Esc>

" Use :w!! to save with sudo
ca w!! w !sudo tee >/dev/null "%"

Vim Plug-ins

One of the best things about vim is all of the available plug-ins. Here is the set of plug-ins I use that make vim even easier and better.

Tabular

Tabular.vim is a very useful plugin to lineup characters amongst several lines, for example lining up the equal sign in a set of variable assignments. See vimcasts on tabular

Ctrl-P

CtrlP.vim is a fuzzy file matching finder for vim. It works similar to the Command-T plugin, a copy of Textmate’s file search. CtrlP is easier to setup and works real nice.

Additional Resources

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