How to copy/paste between a console and an X session?

What’s the fastest way to copy/paste between a non-graphical console (<Ctrl><Alt><F…>) and an X session ?

Right now :

  • I select the text with the mouse on the console (I’ve installed gpm)
  • Then I paste the text inside a temporary file
  • And finally I switch over to the x session, open the temporary file, and copy/paste its content

Is there an easier way to do this ? Can the primary selections of the non-X console and the X session be merged ? Ideally I’d want to select the text in the console, then switch over to the X session and paste it (middle-click). Can this be done ?

Asked By: ChennyStar


The "best" way to achieve that sort of thing is almost probably opinion based.

The way I prefer uses the backlog of the native terminal.

Knowing that the backlog of tty[N] can be accessed via /dev/vcs[N], I simply fire cat /dev/vcs[N] from my Xterm and do whatever I want with the result displayed.

Of course if your Xterm user is different from the owner of the tty you want to dump, you might need to use sudo.

BTW, as wisely reported in the comments, you might be annoyed with the formatting due to the absence of line feeds. man vcs will give you possible workarounds :

Note that the output does not contain newline characters, so some
processing may be required, like in
fold -w 81 /dev/vcs3 | lpr
or (horrors)
setterm -dump 3 -file /proc/self/fd/1
Answered By: MC68020

A good alternative is to install bsdutils and use script

sudo apt install bsdutils

and run

$ script
Script started, file is typescript
$ type commands
get output
$ exit

Afterwards you can read the commands and output from another place, for example a terminal window, with less -R to take care of the ANSI formatting, but it works with cat too.

$ less -R typescript
Answered By: sudodus

Another thing you can do is use screen. It creates a virtual terminal that can be detached from any tty/pty and reattached to another terminal.

So, for your use-case, it would be start the screen session on the console:

~ $ screen -L -S TestTerm

And, to clarify, "-L" turns on logging, which creates a logfile in the directory you started screen from, and "-S" gives the screen session a name that can help you discern which session is which when you list all screen sessions with "-ls".

Now, if you have to run the application from the console, then run the application, and as it is running, hit "(Ctrl+A)+(Ctrl+D)" to detach from the screen session. Then go into your desktop, open a terminal and run:

~ $ screen -ls
There is a screen on: 
    10296.TestTerm  (Detached)
1 Socket in /tmp/screen/S-ChennyStar.

Now, you have the PID and name of your screen session, so reattach it to your xterm:

~ $ screen -d -r 10296.TestTerm

And, to clarify, "-d" tells screen to detach the session if it is still attached elsewhere, and "-r" tells screen to reattach the session to your current terminal.

Depending on the amount of output you want to copy, the output may have gone outside of the initial terminal buffer when you reattach. However, if it is that much output, the logfile will contain all of the output for you, as well.

Hope that helps.

Answered By: B.Kaatz

You can use the xsel to set the clipboard in X, and you can set the DISPLAY variable to tell it which X to connect to. As long as you are using the same user account, programs started from a terminal should have no problem connecting to X, if DISPLAY is set.

some command | DISPLAY=:0 xsel -ib

then paste.

Answered By: user253751