2

In Vim, when I'm programming, I often want Ctrl-N to behave as if - were part of a keyword; in other words, have it included in iskeyword. However, I'd like to create a new keymapping for this and have Ctrl-N keep it's existing behavior.

I've tried this:

inoremap <C-B> <C-O>:set iskeyword+=-<CR><C-N>

... which sort of works, but the iskeyword option includes the extra - afterward, which isn't desired behavior.

This also sort of works:

inoremap <C-B> <C-O>:set iskeyword+=-<CR><C-N><C-O>:set iskeyword-=-<CR>

... but the pop-up menu is killed by the <C-O>.

Is there a way I can have my cake and eat it? Have the pop-up menu from appear and remain in place, but also set iskeyword back to what it was afterward?

Alternatively, is there another way of solving this problem?

1 Answer 1

3

This seems to work.

function! CustomComplete(type)
    set iskeyword+=-
    return a:type
endfunction
inoremap <expr> <C-B> CustomComplete("<C-N>")
autocmd CompleteDone * set iskeyword-=-

We use an expression mapping to run the function which sets iskeyword every time its run (doesn't seem to be a problem. I also didn't find a autocmd for before ins-completion.) The return value of the function is then used as the replacement for the mapping. In this case you wanted <C-N> behavior. Then to remove set - from set iskeyword we use the CompleteDone autocomand which happens after ins-completion is finished.


Also I tried both of your mappings the first one doesn't really work as expected since it also has <C-O> which kills the completion window.

1
  • Perfect, thank you! - that seems to work. The insight here was using the wrapper function around C-N (which I didn't know you could do), and the CompleteDone event. Thank you - wouldn't have figured this out myself! Commented Jul 15, 2014 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .