Copy & Paste Between the Linux Console and GUI



In a previous post I showed how to use command line scripting to improve the Linux GUI experience. This included the use of “xsel“. This can be used for those who use the Linux console. Not the console that is loaded in the GUI but the consoles available on key presses CTRL-ALT F1 to F6. These are useful because they can be more robust than using the console in the GUI which are dependent on the GUI not crashing or freezing. If the GUI crashes then the Linux consoles on the F1 to F6 key presses remain untouched. However there is a problem with pasting and cutting between those consoles and the GUI. The GUI and external consoles do not share a common cut/paste buffer. This is solved like this …

First at the in a terminal in the GUI find the name of your GUI display.

$ echo $DISPLAY

You need to tell xsel this so it knows where to send you cut/paste.

Now you can send any text to the GUI cut/paste buffer from a console. There are two buffers. The first command sends the text to the primary buffer which is available on middle mouse click or SHIFT – INSERT.

$ echo "hello world" | xsel -i

You can now paste from that buffer in the GUI. However this won’t work, as I found, in places such as the WordPress blog post text box. To fix that copy text to the secondary buffer. Like this.

$ echo "hello world" | xsel -s -i

Now you can paste in the usual way in the GUI with CTRL – V.

If you are using gpm mouse in the F1 – F6 consoles then you will highlight text and use mouse middle click to paste. I think there might be a way to link gpm to xsel but I’ve not worked out how to do that yet. A much better way is to use the utility screen and use keypress CTRL – A then [ and then RETURN to highlight, and then RETURN again to copy. CTRL – A to paste. Not exactly easy but, at least for me, this all beats pasting things to text files and having to open them in the GUI just to get text between the console and GUI. However it may be possible to use screen to handle the mouse without gpm and get it to put the copy buffer straight to the GUI, but I have not worked out how to do this yet. For example this is in the screen manual.

writebuf [-e encoding] [filename]

Writes the contents of the paste buffer to the specified file, or the public accessible screen-exchange file if no filename is given. This is thought of as a  primitive means  of  communication  between screen users on the same host. If an encoding is specified the paste buffer is recoded on the fly to match the encoding.  The filename can be set with the bufferfile command and defaults to “/tmp/screen-exchange”.

With the use of something like Parcellite in the GUI this method makes it much easier to work with text snippets between the GUI and console.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s