1

I am trying to write a wrapper that will execute the function that the cursor is over and send its output to a log file. Here is what I have so far:

nnoremap <expr> GG ExecuteCurrentFunction()
func ExecuteCurrentFunction ()
    " get the current word under the cursor and execute if a function
    " redirecting output to a log file
    redir >> vim.log
    let name_of_function = expand("<cword>")
    let call_string = printf(':call %s()', name_of_function)
    echom call_string
    call execute(call_string)
    redir END
endfunc

It works great, however it doesn't seem to be redirecting any of the echom statements in the called function. For example, if the function I execute is:

func NewFunction ()
    echom "Calling inner function"
endfunc

It will only print the following to my log file:

:call NewFunction()

And it won't print Calling inner function. Why is this so, and how could I change this to effectively create a wrapper function as I'm trying to above?

1 Answer 1

2
  1. execute() has silent modifier by default, so normal output doesn't appear on the screen. It must be invoked as execute(cmd, '') to allow "echoing".

  2. execute() is a sort of :redir by itself (essentially it captures the output into a variable and returns it to the caller), and you cannot do two redirections at a time. It should become clear from reading :h :redir and :h execute() until the end.

You can either directly save output of execute() with writefile(), or use :execute command instead.

1
  • 1
    s/operator/command
    – D. Ben Knoble
    Jun 13, 2020 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.