The 'wildcharm' Setting

by Noah Frederick | | level: intermediate

You are probably already familiar with tab-completion in Vim’s command line. You may also be familiar with the 'wildmode' setting, which provides a completion menu:

When 'wildmenu' is on, command-line completion operates in an enhanced mode. On pressing 'wildchar' (usually <Tab>) to invoke completion, the possible matches are shown just above the command line, with the first match highlighted.

:help 'wildmenu'

You’ll notice, however, that a literal tab character is inserted if you try to invoke completion in a mapping with <Tab>. This is where 'wildcharm' comes in. It’s the 'wildchar' equivalent for macros (hence the “m”). <C-z> is a popular candidate for 'wildcharm' and is what the help entry suggests:

set wildcharm=<C-z>

Why would one want to invoke completion from a mapping? Here’s a practical example:

nnoremap <Leader>b :buffer <C-z><S-Tab>

<Leader>b populates the command line with the :b[uffer] command and shows the wildmenu. Note that since showing the wildmenu inserts the first completion option into the command line, we are invoking the previous-completion key (<S-Tab>) to clear the result such that the wildmenu is still showing. This leaves you with the option of either tabbing through to the buffer you want or typing out a few characters to uniquely identify the buffer name before hitting return. It makes for a great lightweight alternative to :CtrlPBuffer.

Further Reading