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 ?

ChennyStar
  • 1,319
  • 9
  • 22
  • 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](https://superuser.com/questions/288320/whats-like-osxs-pbcopy-for-linux). – Jivan Pal Jun 07 '22 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 Jun 07 '22 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 Jun 07 '22 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 (https://www.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 Jun 07 '22 at 12:45

4 Answers4

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
MC68020
  • 6,281
  • 2
  • 13
  • 44
  • 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 Jun 06 '22 at 13:00
  • The "best" way to achieve that sort of thing is almost probably opinion based. ---- You are right. I replaced "best" by "fastest" in my question – ChennyStar Jun 06 '22 at 13:02
  • 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 Jun 06 '22 at 13:28
  • @sudodus : +1 for your comment ! Adding that, if width are not indentical, you can resort to fold -w Ncols+1 ;-) – MC68020 Jun 06 '22 at 13:47
  • @MC68020, +1 for `cat /dev/vcs[N]` :-) – sudodus Jun 06 '22 at 13:50
  • @ MC68020 , +1 for `fold`, never heard of it before. But why `Ncols+1` ? My console width is 192, my X-terminal width is 190. `$ cat /dev/vcs5 | fold -w 192' works perfectly for me. – ChennyStar Jun 06 '22 at 14:10
  • 1
    @ChennyStar, Use what works perfectly for you :-) – sudodus Jun 06 '22 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 Jun 07 '22 at 06:17
  • 1
    Came here for the answer, stayed for the username. – pipe Jun 08 '22 at 11:01
  • @pipe : BGACK ;-) – MC68020 Jun 09 '22 at 08:30
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.

B.Kaatz
  • 265
  • 1
  • 7
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.

user253751
  • 2,211
  • 2
  • 16
  • 17
  • 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 Jun 07 '22 at 12:24
  • how about wayland? – akostadinov Jun 07 '22 at 16:41
  • @ChennyStar you can use this command for pasting from a console as well, if you login to that console. – S P Arif Sahari Wibowo Jun 08 '22 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
sudodus
  • 6,071
  • 14
  • 27
  • 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 Jun 06 '22 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 Jun 06 '22 at 14:06