Skip to content

Instantly share code, notes, and snippets.

@kar9222
Last active April 26, 2021 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kar9222/3d2580b2fbf7956da7b224d7f5765da6 to your computer and use it in GitHub Desktop.
Save kar9222/3d2580b2fbf7956da7b224d7f5765da6 to your computer and use it in GitHub Desktop.
Vim - Useful tricks

Vim - Useful tricks

There is always something to learn in Vim 😎

Sharing my personal notes -- Vim's useful tricks I learned from Learn Vim owned by Igor Irianto. Feel free to follow them on Twitter! I can't remember them all so I started making notes weeks ago.

Igor Irianto has also written a great book Gourmet Vim: A Vim Cookbook to Master the Vim Editor

Groument Vim: A Cookbook...

Gourmet Vim: A Cookbook, is a hands-on, practical book to explore efficient editing using the Vim editor. Text editing is an art.

When programming, we write re-usable codes. Why should it be any different with text editing?

What? The Gourmet Vim explores the art of efficient text editing using Vim, with 200+ practical examples.

This book packs plenty of tips and tricks of how to do common (and some uncommon) actions in Vim that you can practice immediately. It will give you a jump start to use Vim like a pro (even if you're already one, there is always something new that you can learn).

When you're done reading this, you'll be better at Vim than ever before.

...


Here are my notes


g/{pattern_1}/{rng},/{pattern_2}/{rng} {cmd}

g/start/+1,/end/-1 sort
" Pattern 2: non-empty line and the -1 means a line above
g/^$/,/./-1 j

" Surround digits on lines with numbers with quotes
g/\d/s//"&"

" Think of :t as "to". Move from here "to" there.
[range]t {address}  " no docs?

q{reg}q:g/{pattern}/y {reg}

{v/V/<C-v>}{count}{motion}{operator}
{operator}{v/V/<C-v>}{count}{motion}


" You can quickly generate a list of numbers with:
%s/^/\=printf("%-3s", line("."))
" ^ is a pattern for "the start of a line"
" \= substitutes using an expression
" printf() returns a string
" line(".") gets current line number
" "%-3s" returns string with -3 field width

0put=range(1,10)
0put=join(range(1,10))
0put=join(range(1,10,2), ', ')
0put = '[' . join(range(1,10,2), ', ') . ']'
0put= \"['\" . join(range(1,10), \"', '\") . \"']\"
for i in range(1,10) | put = 'abc ' . i | endfor


" e.g. ~/.vim/generators.vim
" in init.vim
" source ~/.vim/generators.vim

let days = ['monday', 'tuesday', 'wednesday', 'thursday']
for i in range(1,3) | put = '- ' . days[i] | endfor

let g = { 'days': ['monday', 'tuesday', 'wednesday', 'thursday'], 'others': ['a', 'b'] }
for i in range(1,3) | put = '- ' . g.days[i] | endfor

g/{pattern}/put='{text}'
g/{pattern}/-put='{text}'



" % in Vim represents the current file (try: open a file, then run :e % then press <Tab> to expand) .

" Other usages:
!cat "%"
!ls "%"

" Some useful % modifiers: example of using `echo`
echo expand('%:p') " full path
echo expand('%:.') " relative to current directory
echo expand('%:h') " head of file
echo expand('%:t') " tail of file
echo expand('%:e') " extension of file

" Edit file
noremap <leader>e :e <C-R>=expand("%:p:h") . "/" <CR>
" Put path
cnoremap <C-P> <C-R>=expand("%:p:h") . "/" <CR>
" Lazy git
noremap <leader>gg :Gwrite<CR>:Gcommit -m "Edit "%<CR>:Gpush<CR>
" I also use <C-R>% to insert the current filename


" Combine macros
let @z = @a . @b . @c . {...}

" Repeat the last substitute on ALL lines
s/donut/pancake/g
g&
" Vim runs :%s/donut/pancake/g. Technically Vim runs :%s//~/&:
" - %s to sub on all lines
" - // to repeat the last pattern
" - ~ to repeat the sub string
" - & to use the last flag

" Sort based on X element where characters after the match are used to sort
sort /,/
sort /,.\+,/

" Append text to register (e.g. reg `a`) and e.g. with global command
let @a=''
g/{pattern}/y A

" Copy matching lines to end of buffer
" `t`: copy matching lines
" `$`: to end of buffer
g/{pattern}/t $

" Delete even/odd lines
g/^/if line('.') % 2 == 0 | norm! d$
g/^/if line('.') % 2 != 0 | norm! d$
" Delete lines that are less than 3 char long
g/^/if strlen(getline('.')) < 3 | d

" Pass two patterns into the global command and Vim will execute the command exclusively within those patterns
g/{pattern1}/,/{pattern2}/ {cmd}
" Example: Delete all texts btw 'start' and 'end'
g/start/,/end/ d
" Example: Delete all texts btw 'start' and 'end', excluding 'start' and 'end'
g/start/+,/end/- d

" Create TOC
1ka  " roughly the same as 'ggma' to put mark 'a' on the first line of the file
g/^"/t'a-

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment