Run ex-command from terminal and return output to stdout

Hi,

as summarised by the title, I have been trying to run a neovim/vim command from the terminal and then have it’s output redirected to stdout (to store or use for later in other programs if needed).

The command in question is to see what the current background color is (if defined by the colorscheme): :echo synIDattr(hlID("Normal"), "bg"). Running this in neovim works perfectly, and returns the hex code corresponding to the background color.

From fiddling around, I got something working for things like :echo "foo" or the like:
nvim -es -c 'redir >> /dev/stdout' -c 'echo "foo"' -c 'redir END'. This does indeed return foo to stdout (though annoyingly adds an empty line before it).

I then tried using this to query the background colour using:
nvim -es -c 'redir >> /dev/stdout' -c 'echo synIDattr(hlID("Normal"), "bg")' -c 'redir END'
but the only output I get is an empty line instead of the hex code (which I would get when entering this in neovim) - which has me stumped.

I don’t know much about calling neovim commands from the terminal so the commands written above could either be wrong or way too complicated for the task. If anyone knows a way to do it or possibly what could be wrong with the commands it would be a great help!

Thanks!

Details

Nvim version: v0.7.0
Terminal: alacritty

Not sure -es is doing what you think. I’d go with something more straightforward:

nvim --headless -c "call setline(1, synIDattr(hlID(\"Normal\"),\"bg\"))" -c "wq" tmp.txt

Fortunately, quoting hell was solvable here :wink: