How to copy text to clipboard programmatically?

How can you copy all contents of a text file opened in vim through Putty on a Windows desktop to Windows clipboard?

  • I'm accessing a log file on a Linux server remotely, and I need to copy the entire contents of the file to my Windows environment.

  • Answer:

    It's feasible, but a little bit complicated.  You need to: Install an X-server onto your Windows machine.  For example, X-ming.  Then enable the clipboard sync between your X-server and your Windows machine. Enable X-forward on your PuTTY.  Go to the "Connection" -> "SSH" -> "X11" screen, and check the "Enable X11 forwarding" box. Make sure your Vim on your Linux machine has X11 support.  If not, re-compile it or install another package that supports this. Launch Vim and select your text, use "+y to yank your text into your clipboard.  That is, double quote followed by a plus sign, then letter y. Vim will connect to the forwarded X server (typically at port 6010) and send your text to it.  PuTTY receives it and send it back to your local X-server.  Your X-server synchronizes it with your Windows clipboard, then your text will be available to all the other applications on your Windows box. Quite complicated, isn't it?  Indeed, but it's also the most standard way to give you *real* access to the clipboard directly.

Lifu Tang at Quora Visit the source

Was this solution helpful to you?

Other answers

If the file is big,I would rather want to use pscp, it is awesome!

Huang Yangchao

If it's a LARGE logfile - thousands of lines - I'll recommend you go around putty. Options include: installing dropbox on the server, and copying the logfile into dropbox, then pulling it from dropbox to your local machine FTP transfer rsync or scp transfer Hope that helps!

Paul Reiber

After some search, You should yank the text to the * or + registers: gg"*yG Explanation: gg gets the cursor to the first character of the file "*y Starts a yank command to the register * from the first line, until... G go the end of the file ------------------------------------------------------------------------ Credit goes to original post http://stackoverflow.com/questions/1620018/vi-editor-copy-all-the-lines-to-clipboard http://stackoverflow.com/users/5445/cms

Yuqi Deng

If it's a small amount of text, simply use the cursor to highlight the text in the PuTTY window. It will be loaded automatically to the windows clipboard. Open a text file in Notepad or your favorite Windows editor and paste in the text. If it's a large amount of text, you can change the settings in the putty session and turn on logging. Save the log to a file on your PC. Then cat the file, and turn off logging after you get the text you need.

Mike James

Lame but the way I do it is clear (clear or ctrl+k) my terminal then cat the file and copy and paste that - like I said lame :)

Rob Smith

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.