If I had to name one thing about vim that is the most irritating, it would be the default movement keys. While I appreciate command mode versus insert mode, I come from a long background of using joe, and my fingers have memorized those keystrokes. Even now, after many years of using vim. To hit [ESC], then $, and then [i] to get to the end of a line is, to me, a ridiculous waste of time. Movement by line and by number of words (i.e., using 4w to move forward four words), is useful and I do not have a problem entering command mode when I need to use it, which is often enough.
Vim makes changing and adding key bindings very easy, so it makes sense to add some key commands that your fingers already know. For instance, joe uses the old WordStar key bindings: CTRL-E for the end of the line, CTRL-A for the beginning of the line. Adding the following to your ~/.vimrc will make those commands available for use in vim:
" map CTRL-E to end-of-line (insert mode)
imap <C-e> <esc>$i<right>
" map CTRL-A to beginning-of-line (insert mode)
imap <C-a> <esc>0i
The above two commands map the selected key binding (CTRL-E and CTRL-A) to the insert mode by using the imap keyword. The final parameter is the actual key commands that the key binding will execute. For CTRL-E, it will execute [ESC], then $, then [i], and finally a right character movement to position the cursor at the true end of line. Each key press is independent and discrete. Likewise, for CTRL-A, the key commands used are [ESC], then , then [i], which would translate to entering command mode ([ESC]), going to the beginning of the line (0), then re-entering insert mode ([i]). Due to the assumption that we are exiting insert mode and then re-entering it, the imap keyword is used as it makes the key binding active only when in insert mode.
Other map types include: map for all modes, cmap for command-line mode, nmap for normal mode, and vmap for visual mode. Using these five mode types allows you to define key bindings that do different things depending on the active mode.
Other useful key bindings include:
" CTRL-C to copy (visual mode)
vmap <C-c> y
" CTRL-X to cut (visual mode)
vmap <C-x> x
" CTRL-V to paste (insert mode)
imap <C-v> <esc>P
In vim, use the :help key-mapping command to get more help. Define a [CTRL] key as C and [ALT] as M (so ALT-M would be written <M-m>), and [SHIFT] as S.
Creating a set of key bindings that is familiar, eases the learning curve of any new editor, and the flexibility vim allows in this configuration makes it easy to not only leverage the power of vim, but also make it feel like a familiar old friend.
Get the PDF version of this tip here.
Delivered each Tuesday, TechRepublic’s free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays