60

After searching a bit on the net it seems that I can't map CtrlSpace to anything/alot. Is there a way to do it today, what I found was usually 2 years old.

udondan
  • 57,263
  • 20
  • 190
  • 175
plitter
  • 765
  • 1
  • 8
  • 11

8 Answers8

61

I've run into the same issue, the short answer is yes you can, and not only in the gui version. Adding this on you .vimrc is enough:

inoremap <C-Space> <C-x><C-o>
inoremap <C-@> <C-Space>
  • 3
    I know this is _really_ nitpicking, but I would consider using `inoremap` instead of just `imap`, since the former will make sure that any messing around you do later won't change these settings. But, really, that's overcautious overkill. – ravron Jun 10 '13 at 16:09
  • 19
    For me it's working just `inoremap ` (on linux) – pevik Sep 26 '13 at 06:46
  • I tried this again, and in fact... it just worked with `inoremap ` and works fine in terminal vim and gvim. Now if I add the last part, it stops working. Trying with an "empty" vimrc (`vim -u NONE`), none of the combinations work. So I guess some configuration **has** to be set in your `.vimrc` (which one is something that I don't know). – Pablo Olmos de Aguilera C. Sep 26 '13 at 19:12
  • 1
    In my experience, using `inoremap` for these 2 mappings worked on Mac but not Linux. I changed them to `imap` and now it works on both platforms. So: `:imap ` and `:imap ` – jonS90 Jun 29 '16 at 00:26
  • I can't get this working properly in GVim on Windows. It keeps inserting a space and so defaults to the first autocompletion – icc97 Jun 23 '17 at 22:03
  • 1
    Could you add a link to the relevant docs of ``? I've been searching but can't find anything. I'm curious what this answer actually does. I'd love to have a deeper understanding of this.... nevermind. Answered by @romainl. It can be found in `:h CTRL-@`. Seems it was too easy! – exhuma Sep 20 '17 at 15:17
  • Please guys let's make this answer clearer : `inoremap ...` alone works in GUI, and in terminals, `` is interpreted as `` so one must remap it _recursively_ to for the first instruction to be taken into account, with `imap ` ; @Pablo please change the second `inoremap` to `imap` in your answer. – Hugo Raguet Mar 02 '21 at 10:38
26

The problem seems to be that Terminal.app doesn't interpret <C-Space> correctly and Vim understands it as <C-@> which is a built-in mapping (:help CTRL-@).

Maybe you could go with something like the following in your .vimrc:

if !has("gui_running")
    inoremap <C-@> <C-x><C-o>
endif

which seems to work, here, but I don't like the idea of overriding built-ins like that.

Instead you should try with <Leader> (:help leader), it gives you huge possibilities for defining your own custom mappings and (depending on the mapleader you choose) won't interfere with OS/app specific shortcuts/limitations and hence be more portable.

With this in my .vimrc:

let mapleader=","
inoremap <leader>, <C-x><C-o>

I just hit ,, to complete method names.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 4
    It is not only Terminal.app who outputs NULL when you type ``. I won't be surprised if I find out that it is written in some standart: at least urxvt and konsole also do this thing. – ZyX Oct 11 '11 at 19:28
6

The nitpicker broke pablox solution. The crux of the solution was just about remapping. So when you disable remapping, it cannot work.
If you really want to throw in a noremap, this is what it looks like:

inoremap <expr><C-space> neocomplete#start_manual_complete()
imap <C-@> <C-Space>

What will not work: inoremap <C-@> <C-Space> 'cause the <C-Space> part will not be remapped itself.

oliver
  • 9,235
  • 4
  • 34
  • 39
  • For me results in: `E117: Unknown function: neocomplete#start_manual_complete E15: Invalid expression: neocomplete#start_manual_complete()` – user2609980 Aug 29 '14 at 21:27
  • @user2609980 you need to map to a function that is available in your setup. the function I used as an example was taken from my personal setup. – oliver Sep 07 '14 at 19:06
4
  • Have you tried :inoremap <c-space> <c-x><c-o> ?
  • Does CtrlX CtrlO do anything when you type in insert mode? Is omnifunc set?
udondan
  • 57,263
  • 20
  • 190
  • 175
Benoit
  • 76,634
  • 23
  • 210
  • 236
3

Add the following code to ~/.vimrc:

" Ctrl-Space for completions. Heck Yeah!
inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
        \ "\<lt>C-n>" :
        \ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
        \ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
        \ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-@> <C-Space>

Source: https://coderwall.com/p/cl6cpq

user2609980
  • 10,264
  • 15
  • 74
  • 143
2

To accommodate both Windows and Linux I applied this to ~/.vimrc

if has("unix")
  inoremap <C-@> <c-x><c-o>
elseif has("win32")
  inoremap <C-Space> <c-x><c-o>
endif
Kidquick
  • 1,102
  • 12
  • 13
0

I had better results with this set of mappings across all modes on Mac OS. Have not tested Windows or Linux.

I don't understand how the excepted answer is supposed to work in terminal mode.

inoremap <C-space>   <ESC>
vnoremap <C-space>   <ESC>
cnoremap <C-space>   <C-c>
" When in terminal, <C-Space> gets interpreted as <C-@>
imap     <C-@>       <C-space>
vmap     <C-@>       <C-space>
cmap     <C-@>       <C-space>
cmcginty
  • 113,384
  • 42
  • 163
  • 163
0

Like the others said, using inoremap with the correct key for your term (as discovered using i_Ctrl_v) should work. I will add to this another possible cause for problems with insert mode mappings: paste mode. As the docs state:

    When the 'paste' option is switched on (also when it was already on):
           - mapping in Insert mode and Command-line mode is disabled

This may seem irrelevant, but this very thing tripped me up trying to get a similar inoremap binding to work in Vim 8.2. I had set paste in my .vimrc, and had to chop it up with :finish statements (as [recommended in the vim faq) to isolate the line causing the problem.

timblaktu
  • 375
  • 3
  • 11