1

I am using a command like this:

:redir @+ | set guifont | redir END

The output of the command above is:


  guifont=Source Code Pro:h14

Note the blank line at the beginning. How can I remove it? I tried the following:

:redir @+ | set guifont | redir END | %s/^\n//

But it doesn't work.

2 Answers 2

7

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

5

For getting the value of options, vim provides a better way, see :h expr-option

So to get the option value of the 'guifont' setting, you can simply use:

:echo &guifont

You can put the value in the system clipboard with:

:let @+ = &guifont
1
  • 3
    Damn. I got so tunnel-visioned, I didn't even realize what it is OP was trying to do. LOL.
    – 3N4N
    Oct 13, 2022 at 17:43

Your Answer

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

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