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 function 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.
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 theexpression register evaluating to the text to insert. So, the mapping
would have the same effect as the first one if function
MyFunc()returns thestring containing the text to insert: