VIM: My Personal Cheat Sheet

Movement

Key Meaning
h, j, k, l ⬇️,⬅️, ➡️, ⬆️. Also normal arrow keys work fine. Some people like to disable them to “avoid bad habits” but I think it’s OK, because a lot of neurotypical software will still force you to use it.
w, b, e, ne Word movement is one of the most essential to grasp for faster navigation in the beginning of your vim career:- Start of next word.- Start of previous word.- End of next word.- End of previous word.
^b, ^f, ^u, ^d Page movement:- One screen backwards or forwards.- 1/2 screen up or down.
gg Go to start of the file.
G Go to beginning of the file.
t<char> Jump before <char> in the current line.
f<char> Jump after <char> in the current line (used more often than t<char>).
% Takes you to matching bracket. For instance when standing on {, press % to jump to next }. Pressing % again jumps back to {.

Operational

Keys Meaning
:q Quit if no changes are made.
:qall Close all windows and tabs and quit.
:q! Quit regardless if changes are made.
:x If changes are made, save them, then quit.
:wq Write and quit.
:!% Execute current file. :! - go to command mode to run a shell command. % - file in current buffer.
!! Re-execute last execution.

Command Mode

Press : to enter command mode.

Keys Meaning
:n n is a number, goes to specified line.
% Everything in the current file.
q: (not in command mode). Opens history. Press CR to execute.

Mouse Support

Allows you to scroll around and position cursor on screen.

On Linux, set :set mouse=a which should work out of the box, even in WSL via Windows Terminal.

On Windows version of vim this doesn’t work at all and I haven’t found solution yet (other than using gVim).

Update: Mouse actually works quite well in Windows. The issue was with using scoop to install vim. I’d recommend avoiding “scoop” in future as it has endless number of bad issues with other software too.

Editing

Key Meaning
dd Delete current line. This also yanks deleted line, so works sort of like “cut”.
o Append (open) a new line below the current line.
O Same as above, but above the current line.
d0 Delete from cursor to beginning of line.
d$ Delete from cursor to end of line.
ggdG Delete all text. gg goes to start of file. dG - delete to end (G).
u Undo.
. or ^r Redo last action.
^n Invoke built-in autocomplete (no plugins required!)
R (from normal mode) Invoke Replace mode, i.e. type over existing text.

Selection

Also called Visual Mode.

Key Meaning
v Go to visual mode to select characters.
V Go to visual mode to select lines.
i} Expand selection when in visual mode. Finds first enclosing {} and selects inside it.
a} Expand selection when in visual mode. Finds first enclosing {} and selects around it.
ggVG Select all text in the current document. gg goes to start of file. V enters visual mode. G goes to end of file.

Copy/Paste

Key Meaning
y Copy (yank) current selection.
yy Yank current line.
d Cut (delete) current selection.
p Paste current selection (below cursor or P above cursor).
Sys Register
"*y Copy to system register (clipboard).
"*p Paste from system register (clipboard).

Basic search - type / then keep typing search phrase and when happy press enter. Move between occurrences with n (next) and N (previous, reverse of Next).

To turn on case-insensitive search - :set ic. ic stands for ignore case. To switch off - :set noic.

To highlight all the matches in yellow, allowing you to scan the file easily, type :set hlsearch.

To list all the matches - :g/.

Use vimgrep (short command is called vim which is confusing) for more advanced search (tutorial). In short:

:vim search_pattern filename_pattern. For instance, to search all files in all directories for a word test use :vim test **.

Multiple “Viewports”

Key Meaning
Windows
^ws or ^wv Divide (split) current window horizontally or vertically.
^ww Cycle between open windows.
^w[hjkl] Focus to window on the corresponding side. I.e. ^wl focuses on the window to the right of the active windows and so on.
^wc Close active window.
^wo Keep only active window, and close all the others.
Tabs
^wT Make current window a new tab.
:tabedit Create new tab and open it. Alternatively :tabedit filename opens a file in a new tab.
:tabc[close] Close the current tab.
:tabo[nly] Keep active tab, close all the others.
{N}gt Go to tab N. For instance 2gt.
gt Switch to next tab.
gT Switch to previous tab.
:tabmove N Moves current tab to position N. 0 moves to start, no argument moves to the end. You can also use mouse in terminal and gVim to switch to tabs and rearrange them by dragging.

When in NERDTree, you can use t to open a file in a new tab, or T to open it silently in a new tab.

HEX Mode

Vim can be used as a hex viewer - :%!xxd.

Useful Customisations

Enable Folding

set foldmethod=indent
set foldnestmax=10
set nofoldenable
set foldlevel=1

Use zo to open fold, and zc to close fold, za to toggle fold.

Folding wiki.

Select Colour Scheme

Start typing :colorscheme<space><tab> which displays list of colour schemes you can try immediately:

When happy, put it in .vimrc like :colorscheme <name>.

gVim

Not entirely sure why someone would use gVim, other than to use fonts different to console one. To customise gVim, you need to create ~/.gvimrc (gvim loads normal ~/.vimrc then merges ~/.gvimrc). Mine looks like this:

# set default colorschema (gVim specific)
:colorscheme desert
# set default UI font
set guifont=Cascadia_Code:h10

# lines and columns (basically window size, depending on font)
set lines=35
set columns=125

set go -=m # disable UI menu
set go -=T # disable UI toolbar

Windows Specific

Set PowerShell Core (PowerShell 7) as vim shell:

" make powershell a vim shell
set shell=pwsh.exe
set shellcmdflag=-command
set shellquote=\"
set shellxquote=

Plugins

There are many options, but I prefer vim-plug for managing plugins. It’s small and easy to use.

Quickstart

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
vim ~/.vimrc
call plug#begin()

Plug '...whateveryouinstall..'

call plug#end()

Commands

:PlugUpgrade - upgrade vim-plug itself.

:PlugUpdate - install missing (new) plugins, upgrade existing.

:PlugClean - physically clean out plugins from disk that were removed from .vimrc.

Useful Plugins

To discover new plugins, use VimAwesome.

Markdown preview

markdown-preview is a great plugin for markdown html preview integration. Make sure you have node and yarn installed (markdown is web tech) then install with plug:

Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install'  }

Use :MarkdownPreview command to start preview. It even supports scroll sync!

Easymotion

easymotion allows to navigate a bit faster. Install:

Plug 'easymotion/vim-easymotion'

The simplest action is jump to a character on screen:

    Default Mapping      | Details
    ---------------------|----------------------------------------------
    <Leader>f{char}      | Find {char} to the right.

By pressing that, plugin highlights occurences and shortcuts.

Programming

Check out vim-lsp and vim-lsp-settings. The first one adds support for LSP, the second one simplifies installing LSP servers when they are not found.

Servers are installed by default to $HOME/.local/share/vim-lsp-settings/servers or %LOCALAPPDATA%\vim-lsp-settings\servers. You can use asyncomplete.vim to enable code completion, and asyncomplete-lsp.vim to make vim-lsp a data source for completion

vim-plug config:

Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'

and to make tab to complete:

inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr>    pumvisible() ? asyncomplete#close_popup() : "\<cr>"

flatbuffers

Syntax highlighting for Google Flatbuffers:

Plug 'zchee/vim-flatbuffers'

Check Also

Common Issues

Some commands return a message “sorry, this command is not available in this version…”. This is because many Linux distros embed a basic vim version. You need to install vim package (i.e. sudo apt install vim).


To contact me, send an email anytime or leave a comment below.