开发者

If you remap <cr> to a function, how can you call the original <cr> in the function?

开发者 https://www.devze.com 2023-02-06 12:05 出处:网络
I\'m trying t开发者_高级运维o write vimscript that does something when a user presses <cr> (in both normal and insert mode), but which doesn\'t interfere with the normal effect of <cr>, wh

I'm trying t开发者_高级运维o write vimscript that does something when a user presses <cr> (in both normal and insert mode), but which doesn't interfere with the normal effect of <cr>, which is to insert a line break and move the cursor to the right position on the next line (respecting smart indent or any other indent mode).

Any suggestions?


Try to omit *map where possible and you will not have such problems. This will work as expected:

function s:DoSomething()
    echom "Inside DoSomething"
    return "\<CR>"
    " return "\n" also works "
endfunction
inoremap <expr> <CR> <SID>DoSomething()
" If DoSomething function cannot be executed inside a textlock: "
inoremap <CR> <C-o>:call <SID>DoSomething()<CR><CR>

Note the nore, it prevents <CR> returned by s:DoSomething from being replaced again.


The right-hand side of the mapping needs to begin with <cr>. Like this:

:imap <cr> <cr>sometext   

Then it does not recursively trigger the mapping.

Source: http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial%28Part_1%29#Nested_.28recursive.29_maps

If the {rhs} of a map begins with the {lhs}, then it is not recursively replaced. For example, the following command will not create a recursive map for gq:

:nmap gq gq
0

精彩评论

暂无评论...
验证码 换一张
取 消