3

I am using Vim 7.4 and I wish to map the tab key so I can use new combinations to autocomplete arguments in command mode from the current directory

Contents of current directory:

Funwithstrings.cs Funwithstrings.exe Funwithstrings.cs~

Using vim in this directory : I am using following mapping in vim

:map! as <tab>

Now upon trying this in command mode in the manner

:e Fuas.  

I get

:e Fu^I

I.e., ^I two characters instead of special character <tab> instead of one of the filenames-- expected results with tab key. In a different computer I get :e Fu<Tab> - 5 characters.

What am I doing wrong? I am using screen 256 terminal.

3
  • from what I understand that should work out of the box, if the current directory is correct and you are not using vi mode. Oct 28, 2017 at 13:03
  • @christian no it doesn't. Using. Mapped key inserts ^I character literally. in its place instead of autocompleteing file name - - - intended effect of tab. Same for <esc>
    – mage
    Oct 28, 2017 at 15:20
  • ah, you have been talking about mapped keys. Then that is expected. See the 'wildcharm' option for that. Ah, it is already mentioned in the answer Oct 29, 2017 at 8:45

2 Answers 2

5

By default with nocompatible, vim sets wildchar to <tab> which enables you to tab complete "out of the box." However, this does not extend to <tab>s produced by mappings.

For example, if we cnoremap as <tab>, pressing as, vim will type literal tabs, which in this case render as ^I.

The solution is to use wildcharm, which is "recognized when used inside a macro" (see :help wildcharm):

set wildcharm=<tab>.

An alternative to making a mapping to <tab> is to just set wildchar or wildcharm to the key you want. This works only for single characters.

3
  • Using 'set wildcharm=as' returns error E521: Number required after =: wildcharm=as. Whereas with 'cnoremap as <tab>' and 'set wildcharm=<tab>' are able to set 'as' as command line
    – mage
    Oct 29, 2017 at 12:07
  • i am stikl unable to remap <esc> and other special keys similarly
    – mage
    Oct 29, 2017 at 12:15
  • I clarified that only single characters are permitted in wildchar/wildcharm. I am not sure what you mean about <esc>.. As far as I know wildcharm=<esc> would work. But I don't recommend mapping <esc> here.
    – Mass
    Oct 29, 2017 at 16:40
0

As Mass mentioned above, wildchar can contain only one character. It’s restricted by vim design. But it’s actually restricted to use a non-ascii symbol for wildchar either. In most cases, it doesn’t matter because people usually use vim for programming only (that means using English letters only) and don’t change their keyboard layouts for smart tricks. But I do!

So, the wildchar option can be left as is, i.e. <Tab>. Then, to avoid complex constructions in <C-r>= create a little function that emulates the keystrokes. At last, map the key sequence you want to use in Command mode to that function.

For example, assume we want to use the as sequence for the autocompletion. In such a case, somewhere in .vimrc:

function! s:wildchar()
    call feedkeys("\<Tab>", 'nt')
    return ''
endfunction

cnoremap <silent> as <C-r>=<SID>wildchar()<CR>

Voila!

1

Your Answer

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

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