Vim snippets
Written 2020-06-23 by Kalle
2 min read | 212 words
I love Vim and use it and it's bindings whenever possible. This post is a collection of useful snippets that I've collected over time.
Delete rows that match a pattern
:g/<pattern>/d
Very useful for de-cluttering log files.
It's also possible to do the inverse: delete all rows that don't match the pattern:
:g!/<pattern>/d
Rename the current file from inside vim
Let's say you are working on the file foo.txt
and realize that it's name really should be bar.txt
. One way to realize this would be to close the file in vim, navigating to a file explorer, renaming the file, then opening it again in vim. Doable, but involving lots of steps.
A smoother way is descibed in this Stack Overflow answer:
- Enter the internal file browser by typing
:Ex
. - Navigate to
foo.txt
. - Press
R
. - Change the name to
bar.txt
. - Press enter to save the edit, then again to open the renamed file.
Edit file in hex mode in Vim
- Open the file in Vim.
- Convert it to hex by running
%!xxd
. - Make any changes.
- Convert it back by running
%!xxd -r
.
For a more in-depth explanation, read the following text by Tim Murphy: Link.