Search code examples
vimultisnips

Vim UltiSnips plugin remaps <CR> (Enter key) in insert mode even when I'm not in a snippet


Whenever I hit Enter in insert mode, nothing happens, which is quite annoying as you can probably imagine.

I've checked the mapping with :imap and it turns out UltiSnips maps <CR>:

i  <CR>        * <C-R>=UltiSnips#ExpandSnippetOrJump()<CR>

If i iunmap <CR>, my enter key starts working again but UltiSnips stops working (which makes sense I guess).

I'm not using any premade snippets and I have a single tex.snippets file working. I have tried deleting everything from the file to check if something is wrong with my snippet definitions.

This behaviour will occur for all filetypes.

Here is my UltiSnips config:

let g:UltiSnipsSnippetsDir         = '~/.vim/ultisnips'
let g:UltiSnipsSnippetDirectories  = [$HOME.'/.vim/ultisnips']

let g:UltiSnipsExpandTrigger       = '<C-m>'
let g:UltiSnipsJumpForwardTrigger  = '<C-m>'
let g:UltiSnipsJumpBackwardTrigger = '<C-n>'

let g:UltiSnipsEditSplit           = "vertical"

Why does UltiSnips map <CR> for its function?

Thankful for any help with this since this makes UltiSnips unusable for me.


Solution

  • That's because <CTRL-M> is same as <CR> (Enter), see :help keycodes

    notation    meaning         equivalent  decimal value(s)    ~
    -----------------------------------------------------------------------
    ...
    <Tab>       tab             CTRL-I      9   *tab* *Tab*
                                                *linefeed*
    ...
    <CR>        carriage return CTRL-M      13  *carriage-return*
    <Return>    same as <CR>                    *<Return>*
    <Enter>     same as <CR>                    *<Enter>*
    <Esc>       escape          CTRL-[      27  *escape* *<Esc>*
    ...
    

    Similarly, for example CTRL-I means tab of Ctrl-[ means Esc. So you actually have configured it this way - let g:UltiSnipsExpandTrigger = '<C-m>'.