Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the keybinding used to autocomplete in vim?

I'm not a big fan of Ctrl-n, I'd like to be able to use Ctrl-Space. Any ideas how I can do that?

like image 894
Geo Avatar asked Feb 15 '10 21:02

Geo


3 Answers

if has("gui_running")
    " C-Space seems to work under gVim on both Linux and win32
    inoremap <C-Space> <C-n>
else " no gui
  if has("unix")
    inoremap <Nul> <C-n>
  else
  " I have no idea of the name of Ctrl-Space elsewhere
  endif
endif
like image 189
Jeffson Avatar answered Oct 14 '22 17:10

Jeffson


I like the Ctrl+Space mapping as well.

" Remap code completion to Ctrl+Space {{{2
inoremap <Nul> <C-x><C-o> 

In your case you want:

inoremap <Nul> <C-n>
like image 21
Pierre-Antoine LaFayette Avatar answered Oct 14 '22 15:10

Pierre-Antoine LaFayette


If you wanted to map it to, say, Ctrl-E, it'd be something like:

inoremap <C-E> <C-N>

The problem with Ctrl-Space is that most terminals will just see it as a space. I'll assume you're using some terminal program inside X; if you're using something different, you'll have to supply the relevant substitutions yourself.

Bash's readline normally has Ctrl-V mapped to "treat the next key as literal." So pressing Ctrl-V then Home at a bash command line will insert ^[[H or something like that on the command line, rather than going to the start of the line. Try pressing Ctrl-V then Ctrl-Space. Probably you'll just see a space.

In that case, you'll have to fool with xmodmap or write your own /usr/share/X11/xkb/* files to tell X to output something different when you press Ctrl-Space. Programs like Firefox don't care; they detect which base key is being pressed and figure out for themselves what modifiers are being pressed. But most terminal-based programs will just see Ctrl-Space as a Space unless you tell X to treat Space and Ctrl-Space differently.

I doubt you can make this change with xmodmap alone; you'll probably need to do the lower-level /usr/share/X11/xkb/* hacking. This is complicated, and I don't even know if you're using X in the first place, so I'll just leave it there.

Some terminals like urxvt let you specify your own keybindings. Like Firefox, they can tell when it's a Space and when it's a Control-Space even without you doing anything special to configure X. So you could tell urxvt to output "\033I_TYPED_CONTROL_SPACE_DAMMIT" when you press Ctrl-Space. And then you could tell vim to map that to <C-N>.

EDIT: I forgot that Ctrl-Space used to output \0 (I remapped that somewhere else on my keyboard). In that case, all the complexity I described above is unnecessary. What I said would apply to someone who wanted to use a more exotic mapping, like Ctrl-colon or Alt-Space.

like image 10
dubiousjim Avatar answered Oct 14 '22 16:10

dubiousjim