I still struggle executing commands from within a function:
Function:
function! MyFunc(key)
let r = a:key
exe r
endfun
a:key
can be anything but when it is a search or a command containing <C-..>
it doesn't work
1)
special keys example
When a:key
= 'normal \<C-w>t\<C-w>H'
it does exe 'normal \<C-w>t\<C-w>H'
in above function
it has to be exe "normal \<C-w>t\<C-w>H"
(double quotes)
How can I execute special keys from a variable (a:key)/ function call?
2)
highlighting example
When I call this function from the commandline :call MyFunc('/\s')
even if highlighting is on, I have to push the n
key to see the items found highlighted
or an alternative is to check within the function if r
starts with /
then do return normal! n
How can I avoid this?
This has the same effect:
:call MyFunc('let @/ = "\\s"')
How do I exec special keys and search items from a function call?
RE 1. You have to pass in the special keys to your function in the final form, e.g.
:call MyFunc("normal \<C-w>t\<C-w>H")
RE 2. Inside functions, the last used search pattern will not be changed (cp. :h function-search-undo
). This is a feature, not a bug. To work around that, you have to execute the command outside the function (i.e. directly in the command or mapping); you can still invoke a function to generate the search pattern, though.