How do I create command-line aliases so that typos like W are automatically corrected to w?
Answer
cnoreabbrev W w
Explanation
The :cnoreabbrev command creates a non-recursive command-line abbreviation. When you type the abbreviated word and then press <Space> or <Enter> on the command line, Vim automatically expands it to the full command. This is the standard technique for creating command aliases and correcting common capitalization typos.
How it works
cabbrev and cnoreabbrev both map a short word to a longer command, but cnoreabbrev prevents the expansion from triggering further abbreviation lookups — always use cnoreabbrev to avoid unintended recursion.
cnoreabbrev W w
cnoreabbrev Q q
cnoreabbrev Wq wq
cnoreabbrev WQ wq
cnoreabbrev Wa wa
cnoreabbrev E e
cnoreabbrev Qa qa
Not just for typos — you can also create genuine command shortcuts:
cnoreabbrev nt tabnew
cnoreabbrev vsp vsplit
Example
With cnoreabbrev W w in your vimrc:
:W<Enter> → executes :w
:Wq<Enter> → (with the Wq abbreviation) executes :wq
Without the abbreviation, :W would fail with E492: Not an editor command.
Tips
- Abbreviations only expand when the abbreviated word is the first word after
:— so:g/pat/Wis safe and won't expandWunexpectedly - Use
:cabbrev(withoutnore) only when you intentionally want the expansion to trigger another abbreviation - You can list all current command abbreviations with
:cabbrev(no arguments)