53

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.

7 Answers 7

61

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
3
  • didn't work in vim-gtk some clue ? Jan 5, 2017 at 21:32
  • @SergioAbreu try @+ instead of @* (well, it works in gvim).
    – leeand00
    Jan 3, 2018 at 20:45
  • Didn't work. Had to :redir @", paste in a buffer and copy to clipboard, "*Y, from there
    – CervEd
    May 5, 2023 at 9:16
18

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.

2
  • This is useful, but can I use this with set guifont for example?
    – Steven
    Jul 27, 2010 at 18:47
  • 1
    No, that is not a shell command so it will not work. Aug 2, 2010 at 19:56
6

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.

1
  • 1
    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). Jul 26, 2010 at 11:47
6

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)

0

You could send to a file and copy it from there:

SomeCommand > SomeFile.txt
vim SomeFile.txt

See How do I save terminal output to a file? on AskUbuntu.

0

let @*=execute('set guifont') will copy the output into the * register which corresponds to the clipboard.

0

I'm so late to the party... I've been looking for a way to pipe that list to my fzf-powered shell goodies for some time, and today this post allowed me to finish!

I got this nice one-liner, it could be done better but it works!

env _FILE="/tmp/.vim$RANDOM"  vim +":redir > ${_FILE} | oldfiles | redir END" +:q && cat  $_FILE | awk '{print $2}' | fzf

Now, it's easy to pipe that into any fzf-consuming pipe command, or whatever I need!

Thanks

New contributor
mrArias is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .