Getting return value from vim internal command in vimscript

I want to do something like

let colors = execute(":highlight") 

This is obviously wrong, all I can do is execute(":highlight") , which will open the window, but I really need to get the contents of this window into a variable, which is very similar to calling system() for external commands. It can be done?

+4
source share
2 answers

There is a command called :redir , which is specifically designed to capture the output of one or more commands in a file, register, or variable. In the latter case, the use is as follows: Example.

 :redir => colors :silent highlight :redir END 

For a complete list of command invocation methods, see :help :redir . See also my answer to the question Extending the highlight group in Vim for practical use :redir .

+5
source
 let colors = lh#askvim#exe(':hi') 

Which just encapsulates :redir . Or even better:

 let colors = lh#askvim#execute(':hi') 

which returns the result as a list variable, either through :redir if we have no choice, or via execute() when it is defined. This new approach should be preferred as it has less undesirable side effects.

+3
source

All Articles