Vim cheat sheet

Reference commands for the vim text editor.
  1. Modes
  2. Saving and exiting
  3. Text editing basics
    1. Insert
    2. Append
    3. Replace
    4. Delete (cut)
    5. Yank (copy)
    6. Put (paste)
    7. Undo/redo
  4. Movement/navigation
    1. Characters
    2. Words
    3. Sentences
    4. Lines
    5. Scrolling (screen)
  5. Visual mode
    1. Selecting a visual mode type
    2. Marking text
    3. Commands that work on marked text
  6. Ranges
    1. Stacking range patterns
  7. Search and replace inside a file
    1. Find
    2. Quick search
    3. Replace
      1. Optional flags
      2. Examples
    4. Code folding
  8. File and window management
  9. Buffer management
    1. Multiple files
    2. Tabs
    3. Explore
  10. Terminal
    1. Copy and paste source code
    2. Navigation
  11. Miscellaneous
    1. Line numbers
    2. Useful help commands
    3. Useful local options
  12. Customization
    1. Leader key
    2. Conditionals
    3. Colors
  13. Plugins
    1. NERDTree
    2. Nvim-R
  14. fzf.vim
  15. References

Modes

Vim currently supports 7 basic modes.

  1. Normal mode.
  2. Visual mode (v / V).
  3. Select mode.
  4. Insert mode (i).
  5. Command-line mode (:). Serves as a prefix for many commands.
  6. Ex mode.
  7. Terminal-job mode.

Esc returns the editor to normal mode. This will also cancel a partial, unwanted command.

Refer to :help vim-modes for details, including information on additional advanced editor modes, which aren’t covered here.

Saving and exiting

Closing out of vim is often a frustrating step for new users.

Text editing basics

Note that vim uses different naming conventions.

Normal Vim
cut delete
copy yank
paste put

Insert

These commands will enter insert mode.

Use Esc to exit back to normal mode.

Append

Replace

Delete (cut)

Almost all deletion commands are performed by typing d followed by a motion.

Yank (copy)

Put (paste)

Undo/redo

Also refer to visual mode, which is extremely useful for text selection.

Movement/navigation

Characters

Vim uses the home row for navigation. The arrow keys also work, but these are faster to type because you don’t have to lift your hand.

Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Words

Sentences

Lines

Scrolling (screen)

See :help scroll for details.

Visual mode

Selecting a visual mode type

Marking text

Commands that work on marked text

Ranges

A range is either a single address or a pair of addresses separated by , or ;. Here are some examples using :d as the short form of :delete.

Note that instead of ,, ; can be used as a separator. The difference is that in the case of from,to, the to is relative to the current line, but when using from;to, the to is relative to the address of from! Assuming you’re on line 5, :1,+1d would delete lines 1 to 6, whereas :1;+1d would only delete lines 1 and 2.

Stacking range patterns

The / address can be preceded with another address. This allows you to stack patterns:

:/foo//bar//quux/d

This would delete the first line containing “quux” after the first line containing “bar” after the first line containing “foo” after the current line.

Search and replace inside a file

Find

Useful search prefixes:

Once interactive after pressing Enter:

Replace

Substitution is accomplished with the :s command. It is commonly used in combination with ranges or the :g command. Vim uses a syntax similar to sed:

:s/<pattern>/<replacement>/<flags>

Optional flags

Examples

Code folding

File and window management

Buffer management

Vim supports multiple files, tabs, and window splitting. I recommend using this instead of tmux whenever possible because it’s easier to manage the clipboard.

Multiple files

Vim supports opening multiple files directly from the shell. Use the ‘-O’ flag to split vertically or ‘-o’ to split horizontally.

vim -O file1 file2

Alternatively, you can manage files inside vim.

To focus (zoom) the active split window, use Ctrl-w | (for a vertical split) or \_ (for a horizontal split). Exit from the focus mode with Ctrl-w =.

Tabs

Explore

Terminal

The built-in terminal was introduced in Vim 8.1.

Copy and paste source code

  1. Yank (copy) lines as normal.
  2. Switch to terminal window using Ctrl-w.
  3. Paste copied text using Ctrl-w "" (two quotes).

While on the terminal in vim, you have to switch to “Terminal-Normal” mode, with Ctrl-w Shift-N (note case). Now you can use the usual vim commands to move around, cut, copy and paste. Once finished, use either i or a to switch back to the prompt.

Miscellaneous

Line numbers

Useful help commands

Useful local options

Customization

Vim supports customization through a .vimrc configuration file, and plugins in a .vim directory. When using a graphical Vim client, such as MacVim, you can also customize the appearance with a .gvimrc file. Note that Vim 8+ now contains a native third-party package manager.

Leader key

Vim calls the prefix key the leader. Vim also has a second leader key called local leader. This is meant to be a prefix for mappings that only take effect for certain types of files, like Python files or HTML files.

You can be specific about when you want mappings to apply by using nmap, vmap, and imap. These tell Vim to only use the mapping in normal, visual, or insert mode respectively.

Each of the *map commands has a *noremap counterpart that ignores other mappings: noremap, nnoremap, vnoremap, and inoremap. Always use these non-recursive variants when defining custom mappings.

Here are some keys not used in normal mode that may be suitable for remapping:

Conditionals

Version specific conditionals (e.g. Vim 8.2+):

if v:version >= 802
    set background=dark
else
    set background=light
endif

Use finish inside if/else conditional to early return.

Colors

:colorscheme Space Tab: List installed color schemes.

Plugins

NERDTree

NERDTree is a file browser for Vim.

Nvim-R

Nvim-R is an excellent R IDE for REPL inside Vim. Conceptually, it works similarly to ESS for Emacs. Refer to the official Nvim-R.txt documentation file for additional information and configuration options.

Note: The <LocalLeader> key is \ by default.

To use the plugin, open an R or R Markdown file in Vim, and then type <LocalLeader>rf. This will spawn a new R console buffer. Depending on the size of the terminal window, this will be either horizontal (narrow window) or vertical (wide window). Now you will be able to use the plugin key bindings to send commands to R. Inside a graphical Vim client, such as MacVim, an R menu option will appear.

R Menu                                             Command
----------------------------------------------------------

Start/Close
  . Start R (default)                                  \rf
  . Start R (custom)                                   \rc
  --------------------------------------------------------
  . Close R (no save)                                  \rq
  . Stop R                                          :RStop

Send
  . File                                               \aa
  . File (echo)                                        \ae
  . File (open .Rout)                                  \ao
  --------------------------------------------------------
  . Block (cur)                                        \bb
  . Block (cur, echo)                                  \be
  . Block (cur, down)                                  \bd
  . Block (cur, echo and down)                         \ba
  --------------------------------------------------------
  . Chunk (cur)                                        \cc
  . Chunk (cur, echo)                                  \ce
  . Chunk (cur, down)                                  \cd
  . Chunk (cur, echo and down)                         \ca
  . Chunk (from first to here)                         \ch
  --------------------------------------------------------
  . Function (cur)                                     \ff
  . Function (cur, echo)                               \fe
  . Function (cur and down)                            \fd
  . Function (cur, echo and down)                      \fa
  --------------------------------------------------------
  . Selection                                          \ss
  . Selection (echo)                                   \se
  . Selection (and down)                               \sd
  . Selection (echo and down)                          \sa
  . Selection (evaluate and insert output in new tab)  \so
  --------------------------------------------------------
  . Paragraph                                          \pp
  . Paragraph (echo)                                   \pe
  . Paragraph (and down)                               \pd
  . Paragraph (echo and down)                          \pa
  --------------------------------------------------------
  . Line                                                \l
  . Line (and down)                                     \d
  . Line (and new one)                                  \q
  . Left part of line (cur)                       \r<Left>
  . Right part of line (cur)                     \r<Right>
  . Line (evaluate and insert the output as comment)    \o
  . All lines above the current one                    \su

 Command
  . List space                                         \rl
  . Clear console                                      \rr
  . Remove objects and clear console                   \rm
  --------------------------------------------------------
  . Print (cur)                                        \rp
  . Names (cur)                                        \rn
  . Structure (cur)                                    \rt
  . View data.frame (cur) in new tab                   \rv
  . View data.frame (cur) in horizontal split          \vs
  . View data.frame (cur) in vertical split            \vv
  . View head(data.frame) (cur) in horizontal split    \vh
  . Run dput(cur) and show output in new tab           \td
  . Run print(cur) and show output in new tab          \tp
  --------------------------------------------------------
  . Arguments (cur)                                    \ra
  . Example (cur)                                      \re
  . Help (cur)                                         \rh
  --------------------------------------------------------
  . Summary (cur)                                      \rs
  . Plot (cur)                                         \rg
  . Plot and summary (cur)                             \rb
  --------------------------------------------------------
  . Set working directory (cur file path)              \rd
  --------------------------------------------------------
  . Sweave (cur file)                                  \sw
  . Sweave and PDF (cur file)                          \sp
  . Sweave, BibTeX and PDF (cur file) (Linux/Unix)     \sb
  --------------------------------------------------------
  . Knit (cur file)                                    \kn
  . Knit, BibTeX and PDF (cur file) (Linux/Unix)       \kb
  . Knit and PDF (cur file)                            \kp
  . Knit and Beamer PDF (cur file)                     \kl
  . Knit and HTML (cur file, verbose)                  \kh
  . Knit and ODT (cur file)                            \ko
  . Knit and Word Document (cur file)                  \kw
  . Markdown render (cur file)                         \kr
  . Spin (cur file) (only .R)                          \ks
  --------------------------------------------------------
  . Open attachment of reference (Rmd)                 \oa
  . Open PDF (cur file)                                \op
  . Search forward (SyncTeX)                           \gp
  . Go to LaTeX (SyncTeX)                              \gt

Edit
  . Complete object name                     CTRL-X CTRL-O
  --------------------------------------------------------
  . Indent (line)                                       ==
  . Indent (selected lines)                              =
  . Indent (whole buffer)                             gg=G
  --------------------------------------------------------
  . Toggle comment (line, sel)                         \xx
  . Comment (line, sel)                                \xc
  . Uncomment (line, sel)                              \xu
  . Add/Align right comment (line, sel)                 \;
  --------------------------------------------------------
  . Go (next R chunk)                                  \gn
  . Go (previous R chunk)                              \gN

Object Browser
  . Open/Close                                         \ro
  . Expand (all lists)                                 \r=
  . Collapse (all lists)                               \r-
  . Toggle (cur)                                     Enter

fzf.vim

Fuzzy file matching is supported in Vim with fzf.vim. This is an incredibly powerful feature that enables quick searching across files directly in Vim.

:FZF :Files Fuzzy match against files. :Rg Fuzzy ma :Ag :GFiles Git files (git ls-files)

git ls-files

Bang-versions of the commands (e.g. :FZF! or :Files!) opens fzf in fullscreen.

:FZF! open fuzzy file search full screen

:Files: Runs $FZF_DEFAULT_COMMAND if defined (e.g. ripgrep rg). This will include a split file preview viewer.

:Rg: Run a ripgrep search in fuzzy file viewer, with a preview window.

:Ag: Run a silver searcher (Ag) search in fuzzy file viewer, with a preview window

References