39

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?

Geo
  • 93,257
  • 117
  • 344
  • 520

3 Answers3

35
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
Community
  • 1
  • 1
Jeffson
  • 581
  • 3
  • 3
  • 4
    Note: you might have to use `has("gui_running")`, as `has("gui")` simply returns if your vim is compiled with `gui` option, not that you're using it right now. – Dogbert Apr 16 '11 at 17:05
  • 1
    It is not connected to unix directly. `` is translated to `` by the terminal emulator, so if you find a terminal emulator that does the same job on windows it will work as well. And `` is **not** a *name* of `` anywhere, for the same reason `[B` is not a name of ``, it is just a symbol produced by terminal when you press ``. – ZyX Feb 28 '12 at 18:21
21

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>
Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
10

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.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
dubiousjim
  • 4,722
  • 1
  • 36
  • 34
  • 2
    Nevertheless, it is good someone made the effort to describe whats going on here. The other answers lack any description on why their solutions work :-) – JepZ Oct 06 '17 at 08:34