VIM: My Personal Cheat Sheet

Movement

KeyMeaning
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, neWord 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, ^dPage movement:- One screen backwards or forwards.- 1/2 screen up or down.
ggGo to start of the file.
GGo 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

KeysMeaning
:qQuit if no changes are made.
:qallClose all windows and tabs and quit.
:q!Quit regardless if changes are made.
:xIf changes are made, save them, then quit.
:wqWrite 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.

KeysMeaning
:nn 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

KeyMeaning
ddDelete current line. This also yanks deleted line, so works sort of like “cut”.
oAppend (open) a new line below the current line.
OSame as above, but above the current line.
d0Delete from cursor to beginning of line.
d$Delete from cursor to end of line.
ggdGDelete all text. gg goes to start of file. dG - delete to end (G).
uUndo.
. or ^rRedo last action.
^nInvoke built-in autocomplete (no plugins required!)
R(from normal mode) Invoke Replace mode, i.e. type over existing text.

Selection

Also called Visual Mode.

KeyMeaning
vGo to visual mode to select characters.
VGo 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.
ggVGSelect all text in the current document. gg goes to start of file. V enters visual mode. G goes to end of file.

Copy/Paste

KeyMeaning
yCopy (yank) current selection.
yyYank current line.
dCut (delete) current selection.
pPaste current selection (below cursor or P above cursor).
Sys Register
"*yCopy to system register (clipboard).
"*pPaste from system register (clipboard).
Combinations
yypDuplicate line (yy copies current line, p pastes it).

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 **.

Replace

Basic replace

Use “substitute” command (:s) such as:

:s/foo/bar

Which will replace the first occurrence of “foo” with “bar”. Add g (“globally”) flag to replace them all in the current line:

:s/foo/bar/g

To replace in the entire file, add %. So the most common command you will use is actually

:%s/foo/bar/g

Multiple “Viewports”

KeyMeaning
Windows
^ws or ^wvDivide (split) current window horizontally or vertically.
^wwCycle 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.
^wcClose active window.
^woKeep only active window, and close all the others.
Tabs
^wTMake current window a new tab.
:tabeditCreate 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}gtGo to tab N. For instance 2gt.
gtSwitch to next tab.
gTSwitch to previous tab.
:tabmove NMoves 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

Display line numbers

set number

will add column with absolute line numbers

image-20241023144441993

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.