vim tricks

alex 9th May 2024 at 2:19pm

Make use of plugins! UltiSnips is great and here's a great resource on it.

Using backreference in substitution

When doing a substitution, catch the pattern to use as backreference. An example:

# consider the sentence below
I love apple and vinegar.

# apply the following substitution
:%s/\(apple\)/\1 juice

I love apple juice and vinegar.

This is nice to change some code to whatever was already there + something.

Tricks

- # over word to count occurences of a single word!

- gu make lowercase

- gU make uppercase

- :e refreshes the current file

Jumps

- CTRL-o/i to jump backward/forward

- g,/; jump to next/previous change

Buffers

- CTRL + ^ to change to last active buffer

- CTRL + W s/v split window horizontally or vertically

- CTRL + W n to edit a new file

- CTRL + W ^ to split with the alternate buffer

- <bufferID>CTRL-W ^ split with a given buffer ID

Create mappings

:nnoremap - Create mapping for NORMAL mode (non recursive)

to clear highlighting: nnoremap <leader>h :noh<CR>

:inoremap - Create mapping for INSERT mode (non recursive)

:vnoremap - Create mapping for VISUAL mode (non recursive)

to count characters: vnoremap <leader>q :s/\%V./&/g<CR>

:cnoremap - Create mapping for COMMAND-LINE mode (non recursive)

Substitution mode

When doing a substitution (via something like %s) the matching pattern can be referenced again using \0.

A fix to the autocompletion jump issue (when using coc)

https://github.com/neoclide/coc.nvim/issues/4202

Lisp in vim