Call vimscript function using setline () from normal mode mapping

I have a function that uses setline (). For simplicity, call him

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

I want to call this function from a map (using ,, ). I know that map <expr> ,, MyFunc() does not work due to the call to setline (). imap ,, <CR>=MyFunc() works, in principle, but I usually call this function from normal mode, and not in insert mode.

Is there anything that will allow me to either call the function or use the case of expressions from normal mode?

+4
source share
1 answer

To call a function, change the display as follows.

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

Usually, displaying an expression is useful for use in Insert mode, since it allows you to dynamically change the sequence of keystrokes in accordance with a special case. If you want to use the expression case to insert text into the buffer in normal mode, you must use the appropriate normal mode command to insert or change the text (for example, i , i , a , a ), followed by the expression case evaluating the text to insert. So mapping

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

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

 function! MyFunc() ... return 'text for inserting' endfunction 
+5
source

All Articles