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.
:q! Quit regardless if changes are made.
:x If changes are made, save them, then quit.

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.

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.

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

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.

Useful Plugins

To discover new plugins, use VimAwesome.

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

Check Also

Thanks! You can always email me or use contact form for more questions/comments etc.


vim

813 Words

2021-12-12