You were on the right track trying to substitute the newline with nil, but :%s
works on a buffer. What you needed to execute the substitution on is the content of the clipboard, which resides in the clipboard register (see :h @r
).
The correct way to capture the clean output of a command then is this:
redir @+ | set guifont | redir END | let @+ = substitute(@+, '\n', '', '')
But this was the correct way of your approach. There are other, better ways. For example, instead of using the convoluted way of :h redir
, we can directly capture the output of a command with :h execute()
and put it in the clipboard register. Any leading and trailing newline and whitespace can be removed with :h trim()
.
let @+ = trim(execute('set guifont'))
This answer deals with capturing the output of an ex-command, not taking into account what the command is. For the exact use case of this question, which is to capture the output of a set-option command, it's simpler to use Christian Brabandt's answer