Readline Keybindings for Vim Commands

When entering commands in Vim, readline keybindings can come in useful. Having readline keybindings for vim commands allows one to use the same keystrokes found in Bash and Emacs, making use of the muscle memory built up from having used those standard keystrokes. For example, with readline keybindings, one can move the cursor to the beginning of the command using Ctrl+a, and to the end of the line using Ctrl+e. Commands in Vim do not come with such keybindings, so this article will show how to define a subset of those keybindings.

By default, Vim comes with keystrokes for editing commands, but those are not the same as readline keybindings (reference: :h cmdline-editing), so some of them need to be rebound. There are also some readline keybindings that do not have their equivalent in vim, so some vimscript functions must be used to achieve the required effect.

cnoremap <c-a> <home>
cnoremap <c-e> <end>
cnoremap <c-p> <up>
cnoremap <c-n> <down>
cnoremap <c-b> <left>
cnoremap <c-f> <right>
cnoremap <c-k> <c-\>estrpart(getcmdline(), 0, getcmdpos() - 1)<cr>
" alt-b.
cnoremap <esc>b <s-left>
" alt-f.
cnoremap <esc>f <s-right>
" alt-backspace.
cnoremap <esc><backspace> <c-w>

The above can be placed in your vim configuration file (~/.vimrc). These keys are, of course, not exhaustive and they only represent the actions I use most. They cover the basic cursor movements and basic text deletions. Hope that they are useful to you. Also, if you find out how to accomplish the equivalent of Meta+d to delete the word in front of the cursor, do let me know. I'm trying to find out.