13

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 ?

4
  • I am really surprised that Linux/X/GNU does not have commands equivalent to macOS's pbcopy and pbpaste out of the box. There are some good answers here.
    – Jivan Pal
    Commented Jun 7, 2022 at 10:27
  • 2
    Linux has tools like xsel or xclip, see user253751's answer below, I don't know if they are the same as macOS's tools.
    – ChennyStar
    Commented Jun 7, 2022 at 12:25
  • What rather surprises me is that nobody came up with a solution to merge the primary selections of both the console and the X-session. The ideal solution would have been to select the text in the console, then paste it (middle-click) in the X-session. But I guess there are some reasons why the primary selections can not be merged between TTYs.
    – ChennyStar
    Commented Jun 7, 2022 at 12:27
  • 1
    After some more digging : it seems the console's copy/paste function is provided by gpm, and the X-session's selections are provided by the X-Server (baeldung.com/linux/clipboard). I guess it could be possible to somehow merge both of them, but it hasn't be done yet it seems.
    – ChennyStar
    Commented Jun 7, 2022 at 12:45

4 Answers 4

16

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
10
  • 1
    +1 because I've never heard of /dev/vcs before. Still, the formatting of cat /dev/vcs[N]'s output isn't that great (a lot spaces), using a temp file is probably faster
    – ChennyStar
    Commented Jun 6, 2022 at 13:00
  • 2
    @ChennyStar, if you have the same width found by tput cols in the console also in the terminal window, the formatting will be nice, when looking at the backlog. (At least in my Ubuntu 18.04.6 LTS based system.)
    – sudodus
    Commented Jun 6, 2022 at 13:28
  • 1
    @ChennyStar, Use what works perfectly for you :-)
    – sudodus
    Commented Jun 6, 2022 at 14:17
  • 3
    Marked this solution as accepted. It's by far the fastest to use. I wrote a script that does the formatting work. An alias on that script, called it catty. The command catty 5 prints out tty5's content. Very fast to use ! Thanks again
    – ChennyStar
    Commented Jun 7, 2022 at 6:17
  • 1
    Came here for the answer, stayed for the username.
    – pipe
    Commented Jun 8, 2022 at 11:01
8

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.

1
8

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.

3
  • Yes I played around with xsel and xclip, it can be useful in some cases. But I was rather looking for a quick way to copy/paste everything from the console, not only the command results but also the commands themselves, and without the hassle of having to think of it in advance. MC68020's solution seems to be by far the quickest, especially with some scripting around it.
    – ChennyStar
    Commented Jun 7, 2022 at 12:24
  • how about wayland? Commented Jun 7, 2022 at 16:41
  • @ChennyStar you can use this command for pasting from a console as well, if you login to that console. Commented Jun 8, 2022 at 16:19
6

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
2
  • Great solution ! Easy to use and faster than manually handling a temp file. I will probably mark this answer as accepted, unless someone comes up with a trick to use the mouse to do the copy/paste
    – ChennyStar
    Commented Jun 6, 2022 at 14:00
  • You find some nice tricks in man script, for example how to use -f --flush and use a fifo for real-time supervision from somewhere else.
    – sudodus
    Commented Jun 6, 2022 at 14:06

You must log in to answer this question.

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