How do I add Emacs-style editing shortcuts to Vim's command line?
Answer
:cnoremap <C-a> <Home>
Explanation
Vim's command line has limited navigation by default. By using cnoremap, you can add Emacs-style shortcuts like <C-a> for Home and <C-e> for End, making command-line editing much more efficient without leaving the command line.
How it works
cnoremap— creates a non-recursive mapping for command-line mode<C-a>— maps Ctrl+A to<Home>(move to start)<C-e>— maps Ctrl+E to<End>(move to end)- These only affect the
:command line, not normal mode
Example
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <C-d> <Delete>
Command: :some long command with typo|
Press <C-a>: |:some long command with typo
Cursor jumps to the beginning
Tips
- Use
<M-b>and<M-f>for word-level movement:cnoremap <M-b> <S-Left> <C-b>is already mapped to something in Vim —cnoremapoverrides it only for command mode- Add these to your vimrc for permanent availability
- Use
cmapinstead ofcnoremapif you need recursive mappings