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

How do I create abbreviations for common Ex commands?

Answer

:cabbrev tn tabnew

Explanation

Command-line abbreviations with cabbrev let you create short aliases for frequently used Ex commands. When you type the abbreviation and press Space or Enter, it automatically expands to the full command, saving keystrokes on commands you use often.

How it works

  • cabbrev {abbr} {expansion} — creates a command-line abbreviation
  • The abbreviation expands when followed by a non-keyword character (space, enter)
  • Only triggers at the start of the command line (won't expand mid-command)
  • cunabbrev {abbr} — removes an abbreviation

Example

cabbrev tn tabnew
cabbrev vh vert help
cabbrev Wq wq
cabbrev Q! q!
You type: :tn<Space>file.txt<CR>
Expands to: :tabnew file.txt

You type: :vh<Space>pattern<CR>
Expands to: :vert help pattern

Tips

  • Fix common typos: cabbrev Wq wq and cabbrev Q q
  • Use <expr> for conditional expansion based on context
  • cabbrev only works in command-line mode — iabbrev is for insert mode
  • Check all abbreviations with :cabbrev (no arguments)

Next

How do I return to normal mode from absolutely any mode in Vim?