<Plug> function failing, inserting as literal “<t_ý>S”

左心房为你撑大大i 提交于 2020-01-17 05:47:09

问题


To autocomplete by pressing enter, I have the following map setup to work with neocomplete.vim:

inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return pumvisible() ? "\<c-y>" : "\<CR>"
endfunction

This works fine. To explain, pumvisible() returns true when neocomplete's PopUpMenu is showing. <c-y> chooses the selected word, inserting it after the insert mode cursor, and closes the neocomplete popup.

I want to extend this mapping so that, when the autocompleted word is a "snippet" word, the neosnippet.vim plugin will automatically expand it. This is what I tried:

inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return pumvisible() ? "\<c-y>\<Plug>(neosnippet_expand_or_jump)" : "\<CR>"
endfunction

Instead of expanding the snippet word, this results in the following being inserted directly into the text where the cursor is:

<t_ý>S(neosnippet_expand_or_jump)`

How can I fix this and make it work?

Possibly relevent note: If I return to the working version of the code (first one above), hit "enter", then manually type <C-k> (while still in insert mode), the snippet expands correctly. Here is the mapping setup for <C-k>:

imap <C-k> <Plug>(neosnippet_expand_or_jump)

回答1:


<Plug> doesn't make sense in noremap versions of commands since they themselves are mappings. So you should use imap. The second thing is you should be using expression mappings instead of the expression register if you want to execute the result of a mapping.

In total this looks like

imap <silent> <expr> <CR> <SID>my_cr_function()

You probably want to look at :help :map-expression



来源:https://stackoverflow.com/questions/34487155/plug-function-failing-inserting-as-literal-t-%c3%bds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!