Vim for hackers
Run a OS command on each line in the file
:%!COMMAND
:%!sort
# sort all the lines alphabetically
:%!sort | uniq
#remove duplicate lines
:%!tac
#reverse lines (last line to first line)
:%!base64 -d
#base64 decode one line at a time
Extract IP addresses in a file
:%!grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'
Create custom commands for repeatable tasks
You might want to repeat tasks regularly. For example:
You want to encode all the lines in the file (one line at a time). The below vim command would register a custom command EncodeLines
that will take one argument and operate that argument on one line at a time. Now, you can use this base64
, xxd
, sha256
or anything else as long as your shell has the command.
:command! -nargs=1 EncodeLines execute '%!awk ''{print $0 | "' . <q-args> . '"; close("' . <q-args> . '");}'''
:EncodeLines base64
:EncodeLines xxd
You might want to remove empty lines in a file regularly
:command! RemoveEmptyLines g/^$/d
Add these lines to ~/.vimrc
for persistence.