How do I create a custom Ex command?
Answer
:command Name action
Explanation
The :command command defines a new user Ex command. This lets you create shortcuts for frequently used sequences.
How it works
:command Name actioncreates command:Name- User commands must start with an uppercase letter
:command! Name actionoverwrites an existing command
Example
command! W w " :W saves (for typos)
command! Q q " :Q quits (for typos)
command! Trim %s/\s\+$//e " :Trim removes trailing whitespace
command! JsonFormat %!python3 -m json.tool " :JsonFormat pretty-prints JSON
Tips
-nargs=*allows arguments:command! -nargs=1 G grep <args> **/*-rangeallows a range prefix-bangallows an!suffix:delcommand Nameremoves a command:commandwith no arguments lists all user commands