Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the return value from an internal Vim command in Vimscript

Tags:

vim

I want to do something like

let colors = execute(":highlight")

This is obviously incorrect; all I can do is execute(":highlight") which will open a window, but what I really need is to get the contents of that window into a variable — much like a system() call would do for external commands. Can this be done?

like image 412
sebastiangeiger Avatar asked Sep 15 '11 17:09

sebastiangeiger


2 Answers

There is a command called :redir that is specifically designed to capture the output of one or more commands into a file, a register, or a variable. The latter option is what we want in this case:

:redir => colors
:silent highlight
:redir END

To see the complete list of the ways to invoke the command, refer to :help :redir. See also my answer to the question “Extending a highlighting group in Vim” for another practical use of :redir.

like image 165
ib. Avatar answered Oct 01 '22 16:10

ib.


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 through execute() when it's defined. This new approach is to be prefered as it has less undesired side effects.

like image 27
Luc Hermitte Avatar answered Oct 01 '22 15:10

Luc Hermitte