0

I'm using a plugin that performs multiple *noremap operations in its initialization. After I added the following mapping to YCM/UltiSnips:

inoremap <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : "\<CR>"

I've broken the other plugin's ability to see the <Enter> key, or rather I overrode/destroyed the original inoremap. I want to reenable that functionality, but without disabling ability to pick snippets from the auto-complete menu using Enter key. This keymap is effectively saying "if the menu is visible, select the option, otherwise simulate <Enter> key". What I need it to say instead is "if the menu is visible, select the option, otherwise perform whatever command <Enter> key is already mapped to". So basically, at the time of declaration of this inoremap, I need to expand existing inoremap for this key into the else.

I've looked at <C-R>=feedkeys('<CR>')<CR> and <C-R>=mapcheck('\<CR>', 'i')<CR> but couldn't get either to work. Can someone help me out?

Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59

1 Answers1

0

Finally figured it out... this was way more vim troubleshooting than I needed for one day.

function! LoadPumvisibleEnter()
    let smartmap = maparg("<Enter>", "i")
    execute printf('inoremap <script> <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : %s', smartmap)
endfunction
au VimEnter * :execute LoadPumvisibleEnter()
  • throwing it in a function allowed me to stay sane with escapes and which mode I was in
  • autocommand is needed because vim apparently runs vimrc BEFORE loading any plugins (Does Vim load plugins after loading vimrc?), so without it the map will not yet be set
Community
  • 1
  • 1
Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59
  • 1
    I'd put this as a map inside .vim/after/plugin/ because it's really not necessary for this to be an autocommand that repeatedly sets a map. – Steven Lu Oct 23 '16 at 15:19
  • The mapped functionality is not part of the plugin, it's part of my vim config that I wish to work without breaking existing plugin mapping. Bundling it with the original plugin (which I don't maintain nor wish to fork/maintain) is not a clean solution. Should I choose to switch that plugin out in the future, I don't want the ExpandSnippet mapping to disappear. Nor do I want the plugin to break if I remove ExpandSnippet function from my vimrc. – Alexander Tsepkov Oct 23 '16 at 23:43
  • Steven Lu's comment is right. The _after_ directory can be anywhere in your `'runtimepath'`, it doesn't need to be next to the plugin. It's cleaner than using `VimEnter`. Alternatively, you could also `:runtime plugin/original.vi` in your `.vimrc` and then do the remapping right there. – Ingo Karkat Oct 24 '16 at 12:11