开发者

Calling a vimscript function with setline() from a normal-mode mapping

I have a function that uses setline(). For simplicity, let's call it

function! MyFunc()
    call setline( ".", "test" )
endfunc

I want to call this function from a mapping (using ,,). I know that map <expr> ,, MyFunc() doesn't work, because of the setline() call. imap ,, <C-R>=MyFunc() does work, in principle, but I'd normally be calling this func开发者_如何转开发tion from normal mode, not insert mode.

Is there anything that will let me either call a function, or use the expression register, from normal mode?


In order to call the function, change the mapping as follows.

:nnoremap ,, :call MyFunc()<cr>

Usually, an expression mapping is useful for using in Insert mode, since it allows to dynamically change the sequence of key presses according to a particular case. If you want to use the expression register to insert text into the buffer in Normal mode, you should use appropriate Normal mode command to insert or change text (for example, i, I, a, A) followed by the expression register evaluating to the text to insert. So, the mapping

:nnoremap ,, cc<c-r>=MyFunc()<cr><esc>

would have the same effect as the first one if function MyFunc() returns the string containing the text to insert:

function! MyFunc()
    ...
    return 'text for inserting'
endfunction
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜