Skip to content

Instantly share code, notes, and snippets.

@crittelmeyer
Last active January 25, 2024 20:15
Show Gist options
  • Save crittelmeyer/206caffcb860e2ef4052 to your computer and use it in GitHub Desktop.
Save crittelmeyer/206caffcb860e2ef4052 to your computer and use it in GitHub Desktop.
vim cheatsheet

Vim Cheatsheet

As configured in my dotfiles

Intro Resources


And last but not least:

$ vimtutor

To install a new plugin with pathogen:

$ cd ~/.vim/bundle
$ git clone repo
$ source ~/.vimrc       (my .vimrc includes a line that reloads plugins w pathogen)

Managing .vimrc Shortcuts:

<Leader>ev     ==> edit .vimrc
<Leader>sv     ==> source .vimrc

Some terminology

* a WORD is any group of chars delimited by white space
* a word is any group of chars delimited by white space OR special characters
* "Motions" move the cursor (h, j, k, l, w, b, /)
* "Operators" act on a region of text (d, ~, gU, >)
* "Text Objects" act on the surrounding area, opposed to motions that act into one direction (daw, ci(, vip)

Navigation

Window/Tab Navigation

* Ctrl-w <Arrow>   ==> switch windows
* Ctrl-w Ctrl-w    ==> toggle last window
* gt               ==> go to next tab
* gT               ==> go to previous tab
* <Count>gt        ==> go to tab number <Count>
* Ctrl-w H         ==> make current window full height and move to far left
* Ctrl-w K         ==> make current window full width and move to top
* Ctrl-w =         ==> make all windows equal size
* Ctrl-w <Count>>  ==> resize <Count> columns bigger
* Ctrl-w <Count><  ==> resize <Count> columns smaller
* Ctrl-w <Count>+  ==> resize <Count> rows bigger
* Ctrl-w <Count>-  ==> resize <Count> rows smaller

Buffer Navigation

* <Leader>bl  ==> (buffergator) open buffer list
* <Leader>jj  ==> (buffergator) open previous buffer
* <Leader>kk  ==> (buffergator) open next buffer
* <Leader>bq  ==> (buffergator) close current buffer and open previous
* <Leader>T   ==> (buffergator) open new empty buffer

Line Navigation

* [hjkl]          ==> go left, down, up, right
* [count] [hjkl]  ==> go [count] chars left, down, up, right
* 0               ==> go to the starting of the current line
* $               ==> go to the end of the current line
* ^               ==> go to the first non blank character of the line
* g_              ==> go to the last non blank character of the line
* g               ==> go to first line
* G               ==> go to last line
* [count] G       ==> go to [count] line number

Screen Navigation

* H               ==> Go to the first line of current screen.
* M               ==> Go to the middle line of current screen.
* L               ==> Go to the last line of current screen.
* Ctrl-u, Ctrl-d  ==> up half page, down half page
* Ctrl-b, Ctrl-f  ==> up (back) full page, down (forward) full page

Word/Paragraph Navigation

* w or W          ==> beginning of next word or WORD
* b or B          ==> beginning of previous word or WORD (current word if not already at beginning of word)
* e or E          ==> end of current word or WORD (next word if already at end of current word)
* { or }          ==> beginning/end of current paragraph (a paragaph is delimited by empty lines)

Search Navigation

* / [pattern]       ==> search for next occurrence of [pattern]
* ? [pattern]       ==> search for previous occurrence of [pattern]
* n                 ==> go to next matching search item (will search in same direction as search)
*                 ==> go to next occurrence of WORD under cursor
* #                 ==> go to previous occurrence of WORD under cursor
* Ctrl-n            ==> (vim-multiple-cursors) multi-cursor select word (or next match if already in multi-cursor mode)
* Ctrl-p            ==> (vim-multiple-cursors) multi-cursor select previous word
* Ctrl-x            ==> (vim-multiple-cursors) multi-cursor skip currently selected word

Jump/Change Navigation

* Ctrl-i or Ctrl-o  ==> go to next/prev jump made (can travel between buffers)
* g, or g;          ==> go to next/prev change made

Mark Navigation (use capital for cross-file scoping)

* m <Letter>        ==> mark current location with <Letter>
* ' <Letter>        ==> go to line of the <Letter> mark (if exists)
* ` <Letter>        ==> go to exact cursor location of the <Letter> mark (if exists)
* :marks            ==> display all marks

File/Code Navigation

* <Leader>p   ==> CtrlP find files
* <Leader>bb  ==> CtrlP find buffers
* <Leader>bs  ==> CtrlP find most recently used files
* <Leader>bm  ==> CtrlP find mixed results
* % or <Tab>  ==> find nearest/toggle current open/close structure (methods, html blocks, do/end blocks, etc)
* :A          ==> (vim-rails & projectionist) go to alternate file (i.e. controller test if in controller file)
* :R          ==> (vim-rails) go to related file
* <Leader>Rc  ==> (vim-rails) go to related controller file (i.e. controller if in related view or model)
* <Leader>Rv  ==> (vim-rails) go to related view file (i.e. view if in related controller or model)
* <Leader>Rm  ==> (vim-rails) go to related model file (i.e. model if in related controller or view)
* <Leader>A   ==> (ack.vim) do Ack search
* gf          ==> (vim-rails + builtin) go to definition of whatever is under cursor
* <Ctrl>]     ==> go to tag
* <Ctrl>T     ==> return from tag jump

Code/Text Manipulation

* :<Count> >>                 ==>  indent next <Count> lines
* :<Start>,<End> >            ==>  indent lines <Start> to <End>, inclusive
* Ctrl-t                      ==>  insert indent at start of line
* Ctrl-d                      ==>  remove indent at start of line
* dd or d_                    ==>  cut line
* yy or y_                    ==>  copy line
* dw                          ==>  cut word
* cw                          ==>  change word (delete and go to insert mode)
* d<Count>w                   ==>  cut <Count> words
* d<Count>d                   ==>  cut <Count> lines
* ys<Text Object><Delimiter>  ==>  surround <Text Object> with <Delimiter>
* cs<TO><OldD><NewD>          ==>  change surrounding <OldD> to <NewD>
* ds<TO><Delimiter>           ==>  remove surrounding <Delimiter>
* s                           ==>  deletes current char and enters insert mode

* vat                         ==>  select entire tag

Code Folding

za ==> toggle fold
zc ==> close current fold
zo ==> open current fold
zj ==> navigate to next fold below
zk ==> navigate to next fold above
zM ==> fold everything
zR ==> unfold everything

Visual Selection

1. v
2. i or a
3. (object)
    * wW           => word or WORD
    * s            => sentence
    * p            => paragraph
    * b            => block (parens)
    * B            => block (Braces)
    * t            => tag (xml tag)
    * <            => block (gt lt)
    * [            => block (brackets)
    * ' or " or `  => quotes
    * iI           => indent (courtesy of vim-indent-object plugin)

File io

:e [filename/url]         ==> open file for editing (if no file named, open new file or reload current file)
:r [filename/url]         ==> inject file at cursor
:[count]r [filename/url]  ==> inject file at line number [count]
:r! [command]             ==> inject results of command at cursor
:w [filename]             ==> write to file (if no file named, write to currently open file
:q                        ==> quit
:split [filename/url]     ==> open file in horizontal split
:vsplit [filename/url]    ==> open file in vertical split

NOTE: add ! to force any of the above

Custom Mappings in my .vimrc (many of these are also listed in context, above)

* <Leader>ev       ==> edit .vimrc
* <Leader>sv       ==> source .vimrc
* <C-k>            ==> move current line up
* <C-j>            ==> move current line down
* <Leader><Space>  ==> clear previous search
* <Tab>            ==> jump to opposite tag
* :w!!             ==> retroactively sudo edit the current file
* <F3>             ==> toggle line numbering setting
* <S-Enter>        ==> insert blank line below cursor w/out leaving normal mode
* <Leader>v        ==> reselect recently pasted text (for adding indent, etc)
* jj               ==> escape from insert mode
* jjj              ==> escape from insert mode and write to the buffer

Custom Insert Mode Expansions

* .log<Tab>         ==> surrounds text with console.log();
* jj                ==> escapes from insert mode 

Plugin Mappings (many of these are also listed in context, above)

* Ctrl-n              ==> (vim-multiple-cursors) highlight current word/next match in multiple cursor mode
* <Leader>n           ==> (nerdtree) toggle NERDTree
* :A                  ==> (vim-rails) go to alternate file (i.e. controller test if in controller file)
* :AV                 ==> (vim-rails & projectionist) open alternate file in vertical split
* :AS                 ==> (vim-rails & projectionist) open alternate file in horizontal split
* :AT                 ==> (vim-rails & projectionist) open alternate file in tab
* :A <Filename>       ==> (vim-rails & projectionist) open file relative to current project
* :R :RV :RS :RT      ==> (vim-rails) related file
* <Leader>Rc          ==> (vim-rails) go to related controller file (i.e. controller if in related view or model)
* <Leader>Rv          ==> (vim-rails) go to related view file (i.e. view if in related controller or model)
* <Leader>Rm          ==> (vim-rails) go to related model file (i.e. model if in related controller or view)
* <Leader>p           ==> (ctrlp) open ctrlp window
* <Leader>bb          ==> (ctrlp) open ctrlp buffer list
* <Leader>bs          ==> (ctrlp) open ctrlp mru list
* <Leader>bm          ==> (ctrlp) open ctrlp mixed list
* <Leader>fu          ==> (ctrlp-funky) open ctrlp function list
* <Leader>fU          ==> (ctrlp-funky) function list narrowed to word under cursor
* <Leader>t           ==> (dispatch) run :Dispatch
* <Leader>bl          ==> (buffergator) open buffer list
* <Leader>jj          ==> (buffergator) open previous buffer
* <Leader>kk          ==> (buffergator) open next buffer
* <Leader>bq          ==> (buffergator) close current buffer and open previous
* <Leader>T           ==> (buffergator) open new empty buffer
* <Leader>gl          ==> (gist-vim) open list of gists
* <Leader>gp          ==> (gist-vim) save current file as private gist
* <Leader>gP          ==> (gist-vim) save current file as public gist
* <Leader>c<Space>    ==> (nerdcommenter) toggle comment on current line
* ys<TO><D>           ==> (vim-surround) surround <Text Object> with <Delimiter>
* cs<TO><OldD><NewD>  ==> (vim-surround) change surrounding <OldD> to <NewD>
* ds<TO><D>           ==> (vim-surround) remove surrounding <Delimiter>
* :Calendar           ==> (vim-calendar) show calendar (press E to view event details)
* :HackerNews         ==> (vim-hackernews) show hackernews front page (press O to open link in browser)
* gS                  ==> (splitjoin) split a single line of code into multi
* gJ                  ==> (splitjoin) join multi into a single line of code

Did you know?

  • Put % before any vim command line command to apply it to all lines!
  • Use the "norm" command to execute code in normal mode
  • Vim also has “adjectives” like “inside” and “around” (i and a) that let you craft sentences like “change inside parenthesis” (ci( or cib). Or "delete around word" (daw). Or even "visually select inside paragraph" (vip)
  • You probably only ever use caps lock on accident! Remap it to something useful like Ctrl or Esc! (I do Ctrl!)
  • The :finish command will make a vim script stop processing. It's a great tool for debugging a bad plugin by manually binary searching a file for error-causing code (add :finish in the middle of .vimrc, source it, and see if bug persists. Then, move :finish up or down and keep narrowing down until bug is found)
  • You can quickly paste the results of a linux command into a vim doc with :r !
  • When in command mode in vim, you can type % and press to see the path of the current file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment