How do I copy command output in vim?

37

15

For example, if I type ':pwd' to get the current working directory, I can select the text in gvim but I can't figure out how to copy it to the clipboard. If I try the same in console vim, I can't even select it with the mouse. I would like this to work with all vim commands, such as set guifont to copy the guifont=Consolas:h10:cANSI output.

Steven

Posted 2010-07-25T02:29:20.130

Reputation: 2 049

Answers

43

Are you looking for this,

:redir @* | set guifont | redir END

:redir command redirects the output of a command to a register (@*). The register @* refers to the clipboard.

For more info on this,

:help :redir

asdfg

Posted 2010-07-25T02:29:20.130

Reputation: 2 266

didn't work in vim-gtk some clue ? – Sergio Abreu – 2017-01-05T21:32:13.750

@SergioAbreu try @+ instead of @* (well, it works in gvim). – leeand00 – 2018-01-03T20:45:37.763

11

Try ':r !pwd' to get the current working directory directly in to the GVIM opened file.
You can then copy it to clipboard like you would any other text file contents opened there.

nik

Posted 2010-07-25T02:29:20.130

Reputation: 50 788

This is useful, but can I use this with set guifont for example? – Steven – 2010-07-27T18:47:44.403

No, that is not a shell command so it will not work. – Kevin Panko – 2010-08-02T19:56:03.497

4

For this particular example you could do (note the "!" which makes it go through the shell):

:!pwd | xclip

or

:!pwd | xclip -selection secondary

(depending on which X-selection you want).

You might have to install xclip first

sudo apt-get install xclip

(or equivalent)

nisc

Posted 2010-07-25T02:29:20.130

Reputation: 983

3

If you're running vim in an xterm, holding the shift key while selecting the text will copy the text to the X equivalent of the clipboard.

garyjohn

Posted 2010-07-25T02:29:20.130

Reputation: 29 085

X has two clipboards (at least); your suggestion will place the text in the PRIMARY selection (paste with middle-click) rather than the CLIPBOARD hselection (paste with Ctrl+V in most apps). – Marius Gedminas – 2010-07-26T11:47:46.447