1

I have the following in my ~/.vimrc:

" Completion based on current file
command Tabf :imap <tab> <C-x><C-n>
" Dictionary completion
command Tabd :imap <tab> <C-x><C-k>

If I input :imap <tab> <C-x><C-k> or :imap <tab> <C-x><C-n> directly in Vim, it works, but running :Tabf and :Tabd return "No mapping found."

1 Answer 1

2

This paragraph, under :help :command-repl vaguely explains what is happening:

The replacement text {repl} for a user defined command is scanned for special
escape sequences, using <...> notation.  Escape sequences are replaced with
values from the entered command line, and all other text is copied unchanged.
The resulting string is executed as an Ex command.  To avoid the replacement
use <lt> in place of the initial <.  Thus to include "<bang>" literally use
"<lt>bang>".

After that paragraph you can find a list of valid escape sequences in that specific context (their meaning is irrelevant, here):

<line1>
<line2>
<range>
<count>
<bang>
<mods> 
<reg>
<args>
<lt>

So, what happens is that your command:

command Tabf :imap <tab> <C-x><C-n>

contains what looks like valid escape sequences to Vim, but it can't really replace them correctly so, instead of the expected:

:imap <tab> <C-x><C-n>

your :Tabf really does:

:imap gibberish

which tells Vim to list insert mode mappings that match gibberish, which may or may not actually return something (who knows, you might get lucky!).

You can solve that problem by following the suggested method of using <lt> instead of <:

command Tabf inoremap <lt>tab> <lt>C-x><lt>C-n>

or by doing the mapping in a function and calling that function from your command:

function! UseKeywordCompletetion()
    inoremap <tab> <C-x><C-n>
endfunction
command Tabf call UseKeywordCompletetion()
1
  • Excellent answer. After attempting to add my idea as a Vim leader key, I was starting to suspect that it was returning gibberish. I had seen <lt> in some of my searching and had been curious if that were a solution. Thank you!
    – jbrock
    Commented Apr 7 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.