The Stripey Site

Key Mappings

One of the great things about Vi is its logical set of keystrokes. Vim provides a some more, but there’s a few alterations that (arguably) can make it even easier to use. The settings suggested below generally make use of unused keys or remap ‘duplicate’ keys, those which by default only duplicate the functionality of some other key. They are all included in the sample .vimrc configuration file.

Movement Keystrokes

The h and l keystrokes can be made to wrap over lines (so that trying to move left past the beginning of a line puts the cursor at the end of the line above). This command does that, and also allows ~ (convert case) to wrap over lines, and the cursor keys to wrap when in insert mode:
set whichwrap=h,l,~,[,]
In many applications (Netscape Navigator, Lynx, Mutt, Pine, SLRN, More, and Less), <Space> scrolls down a page. When reading files with Vim (particularly when instigated as view or gview) I often found myself trying to use <Space> to page down. Since <Space> by default is yet another key for moving the cursor right, it seemed sensible to remap it:
noremap <Space> <PageDown>
Applications vary between using BkSpc or - to page up, but since there are plenty of other keys for moving both left and up, I remapped them both:
noremap <BS> <PageUp>
noremap - <PageUp>
In Lynx it is possible to scroll the window by a couple of lines, leaving the cursor at the same text, with <Ins> and <Del>. (This choice of keys isn’t as barmy as it first sounds after noticing how the <Ins>/<Del> pair are situated next to the <Home>/<End> and <PgUp>/<PgDn> pairs.) Normally these only duplicate i and x do, so can safely be remapped:
noremap <Ins> 2<C-Y>
noremap <Del> 2<C-E>
To cycle through panes of split windows, I use <F6> (cos that’s what that keystroke does in the CUA definition, still used in many Windows applications), which is more convenient than the default of <Ctrl>+Ww. Cycling backwards with <Shift>+<F6> only generally works in the gui, not in terminals:
nnoremap <F6> <C-W>w
nnoremap <S-F6> <C-W>W
If viewing a large number of files (perhaps with gview *), switching between them can be tedious. Remapping <Ctrl>+N and <Ctrl<+P helps with this, and is also mnemonic:
nnoremap <C-N> :next<CR>
nnoremap <C-P> :prev<CR>
(By default these keystrokes respectively move down and up one line. It is unclear why anybody thought this was necessary, given that there are several simpler (IE unchorded) keystrokes already providing these functions; I can’t think of any situation in which it is easier to press <Ctrl>+N than j.) The % keystroke is useful for bouncing around pairs of various sorts of brackets. It can be made to work for angled brackets as well:
set matchpairs+=<:>

Viewing Help

By default <F1> displays the main help file, displaying the introduction and general information about Vim. Once the basics have been mastered, this page isn’t needed too often, so it can be more useful to have that keystroke prompt for the help page to be displayed. These mappings will make <F1> do that from any mode:
nnoremap <F1> :help<Space>
vmap <F1> <C-C><F1>
omap <F1> <C-C><F1>
map! <F1> <C-C><F1>
The required help topic can then be entered at the prompt (using command-line completion if necessary). The original function of <F1> can be obtained with <F1><Enter>.

Formatting Keystrokes

As somebody who never wants to use ‘Ex’ mode but often wants to reformat text, I have Q mapped to do the latter rather than the former. (Earlier versions of Vim also used Q for formatting, before ‘Ex’ mode was implemented.) The following mappings make Q normally reformat the current paragraph (or current and following paragraphs, if a count is used), except when text is selected, in which case it reformats the selected text:
nnoremap Q gqap
vnoremap Q gq
These mappings allow the <Ctrl>+T and <Ctrl>+D keystrokes to adjust the indent of selected text (in the same way that they do with single lines in insert mode), and for <Tab> and <Shift>+<Tab> (where it works) to do the same thing:
vnoremap <C-T> >
vnoremap <C-D> <LT>
vmap <Tab> <C-T>
vmap <S-Tab> <C-D>
Y by default is incongruous with C and D, in that it yanks a complete line (duplicating yy) rather than yanking to the end of the line. This fixes that:
noremap Y y$

Toggle Keystrokes

There are some options which it is convenient to be able to toggle on and off while editing files. Setting up keyboard mappings makes this easier to do; the toggles below all have mappings starting \t, though some are also mapped to function keys. The mappings all display the new value of the option after toggling it.

The paste option is intended for invoking temporarily while pasting in text. It avoids things like autoindent causing ‘stepped’ text. This defines the \tp (“toggle paste”) mapping, also set to <F4> in both normal and insert mode:

nnoremap \tp :set invpaste paste?<CR>
nmap <F4> \tp
imap <F4> <C-O>\tp
set pastetoggle=<F4>
This defines \tf (“toggle format”) and <F2> to toggle whether lines should automatically be wrapped as they are typed:
nnoremap \tf :if &fo =~ 't' <Bar> set fo-=t <Bar> else <Bar> set fo+=t <Bar>
  \ endif <Bar> set fo?<CR>
nmap <F3> \tf
imap <F3> <C-O>\tf
This defines \tl (“toggle list”) and <F2> to toggle indicating non-printing characters:
nnoremap \tl :set invlist list?<CR>
nmap <F2> \tl
This defines \th (“toggle highlight”) to toggle highlighting the most-recently-found search text:
nnoremap \th :set invhls hls?<CR>

Insert Mode Keystrokes

Insert-mode <BkSpc> can be made to act more like may be expected from other applications, by allowing backspacing over line breaks and indentation, and over text that hasn’t just been inserted:
set backspace=eol,start,indent
<Tab> can be used to insert spaces to indent lines to shiftwidth, rather than insert tab characters. This is generally more useful, and avoids setting tabstop to something other than 8 (which is Bad when sharing files with other people or programs that are used to it being 8). Indentation can be changed with the cursor anywhere in a line. Here are the mappings — the <Shift>+<Tab> one probably only works in the gui:
inoremap <Tab> <C-T>
inoremap <S-Tab> <C-D>
Note that so long as sensible makefile settings are used, this will not prevent <Tab> from inserting tab characters in them. In any file, a genuine tab character can be inserted at the cursor position with <Ctrl>+V<Tab>.

Useful abbreviations can be set up for anything you type often. I tend to end mail and news postings with sm, which automatically expands itself to “Smylers” as soon as <Esc> is pressed:

iabbrev lfpg Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
iabbrev hse he/she
iabbrev sm Smylers

Other Keystrokes

When mappings for new functions, using something beginning with \ is a good idea, since by default it doesn’t do anything. I use two further letters, trying to keep them reasonably mnemonic and grouping them by the first letter — the toggles above all start \t.

Defined elsewhere are mappings for spell-checking, which all start \s, and mappings useful when editing HTML files, which all start \h.

Some of these mappings are also assigned to function keys, which are obviously faster to press than three-character mappings, but since function keys don’t always work (particularly when using Vim over a telnet session), these are in addition to the \ mappings.


Feedback is welcome via vim-www@stripey.com. © Copyright 2000 — see copying information for details.