How do I enable tab completion inside custom command-line mappings?
Answer
:set wildcharm=<Tab>
Explanation
The wildcharm option designates a key that, when it appears inside a mapping, triggers wildmenu completion on the command line — just as if you had pressed <Tab> interactively. This solves the problem that bare <Tab> in a mapping never triggers completion because Vim processes the mapping before the command line runs.
How it works
wildcharmtells Vim which character to treat as the completion trigger inside mappings- The most common choice is
<Tab>itself (or<C-z>if you want to keep<Tab>free for other uses in mappings) - Once set, you embed that character in a
:nnoremapor:cnoremapand Vim will trigger completion at that point
Example
Add to your vimrc:
set wildcharm=<Tab>
" Open command line with :e and immediately trigger file completion
nnoremap <leader>e :e <Tab>
" Switch to any buffer with completion
nnoremap <leader>b :buffer <Tab>
Pressing <leader>e opens the command line as :e and the cursor is positioned for you to continue typing or press <Tab> again to cycle through file completions.
Tips
- Use
<C-z>as thewildcharmcharacter if you want<Tab>to remain usable for indentation in other contexts:set wildcharm=<C-z>then use<C-z>in your mappings - Combine with
set wildmode=longest,fullandset wildmenufor the best interactive experience - Works with any command that supports completion:
:buffer,:colorscheme,:help,:e,:vsplit, etc.