问题
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