Tags: #vim #tips #tricks
:windo set scrollbind
and :windo set cursorbind
gf
takes you to the file referenced (e.g. [Makefile](../Makefile)
) but if you add a :
followed by a line number to the link (e.g. [Makefile](../Makefile:123)
) then pressing gF
will take you to the specific line number as well.
:-d
and :+d
delete the line above or below the current line, unlike dj
or dk
which would also delete the current line.
:copy
allows you to copy a line to another location
e.g. :10 copy 20
copies the range, in this case line 10, to below line 20), where I had always :10y
then moved to the destination.
:<range>put <register>
allows you to paste a register to a specific line.
:packadd cfilter
enables filtering results from quickfix window (e.g. :Cfilter /pattern/
, also has negated version Cfilter!
).
To ‘append’ to multiple lines: Ctrl+v
and select multiple lines, then $
, then Shift+a
and start typing (this is the opposite of ‘insert’ with Ctrl+v
and Shift+i
).
To autoformat text to a specific width (e.g. set textwidth=80
), first select the text and then execute gq
. This is super useful for when you have a code comment that you need to tweak/edit, and then it messes up the line breaks. You can just select the entire text and gq
to have it fixed!
:%!jq
will format the entire file
https://vimhelp.org/options.txt.html#modeline
<!-- vim: set colorcolumn=57,80: -->
// vim: foldmethod=indent foldlevel=0
vim -E -s config.txt <<-EOF
:%s/foo/bar/
:update
:quit
EOF
This uses :g/regex1/,/regex2/command
syntax.
:g/\[/+1,/\]/-1sort
It will operate on all lines between the lines matching /regex1/
and /regex2/
We use this to sort a bunch of arrays in a file that looked like:
some_var = [
one,
two,
three,
]
another_var = [
pineapple,
banana,
apple,
]