vimtricks.wiki Concise Vim tricks, one at a time.

How do I create shortcuts for long Ex commands I type frequently?

Answer

:cabbrev

Explanation

:cabbrev (command-line abbreviation) lets you define short aliases for longer Ex commands. Unlike key mappings, abbreviations expand only when typed at the beginning of the command line followed by a space or <CR>, so they integrate naturally into the command-line workflow.

How it works

  • :cabbrev shorthand expansion — Defines a command-line abbreviation
  • When you type the shorthand at the : prompt and press Space or Enter, Vim replaces it with the full expansion
  • :cunabbrev shorthand — Removes the abbreviation
  • :cabbrev (no arguments) — Lists all defined abbreviations

Add your abbreviations to your vimrc so they persist across sessions.

Example

You frequently type :w !sudo tee % to save files requiring root access. Add to your vimrc:

cabbrev w!! w !sudo tee %

Now typing :w!! at the command line expands to :w !sudo tee % before executing.

Other useful examples:

cabbrev Q  quit
cabbrev Wq wq
cabbrev te tabedit
cabbrev bd bdelete

Tips

  • Abbreviations are case-sensitive: cabbrev Q quit only matches :Q, not :q.
  • Unlike iabbrev (Insert mode abbreviations), cabbrev works only on the command line.
  • To prevent accidental expansion of a word that appears elsewhere in commands, use <expr> mappings for more control.
  • Abbreviations apply in the command-line window (q:) too, so your shortcuts work there as well.

Next

How do you yank a single word into a named register?