20

I have some rather long commands and file paths which I have copied to clipboard and need to execute in a TTY, however the file paths are too long to retype and I would like to just paste them in after the $ (I can retype the commands before them then), is there a way to do this?

So I would like to execute a command and have it on the next line starting with $ put the text copied to the clipboard like so:

$ specialCommandToPasteText
$ pastedText

I am running Ubuntu GNOME 16.04 with GNOME 3.20.

7
  • 3
    Paste the commands into a Bash script in a text editor, then execute the script from the TTY? Jun 19, 2016 at 13:29
  • @NickWeinberg: Not really an option, this is all in the CLI and the programs I have running there only have the copy ability.
    – user364819
    Jun 19, 2016 at 13:30
  • 2
    What program(s) are you copying them from? Jun 19, 2016 at 13:34
  • Are you using gpm to provide mouse support in the VT, as suggested in your own answer to a previous question? Jun 19, 2016 at 13:36
  • 1
    One possible solution would be to use the copy/paste functionality in tmux or screen Jun 19, 2016 at 13:49

4 Answers 4

29

It's simple, but you need an additional tool.

  1. Install the package xsel which provides an easy command to access the clipboard:

    sudo apt-get install xsel
    
  2. Find out which $DISPLAY your desktop is using. Usually it should be :0, but you can check it by running this command in a terminal emulator on your GUI desktop:

    echo $DISPLAY
    

    I will assume the output is :0, replace that with your actual output in the following commands if it's different.

  3. Copy the command you would like to execute in the TTY, e.g. using Ctrl+C.

  4. Switch to the TTY you want to use, e.g. to TTY1 using Ctrl+Alt+F1.
    Log in by typing your username and password.

  5. Enter the full command you wish to run, but replace the part you want to insert from the clipboard with $(DISPLAY=:0 xsel -ob).

    For example if you copied a large list of packages to install, you could type this into the TTY:

    sudo apt-get update && sudo apt-get install $(DISPLAY=:0 xsel -ob)
    

    The clipboard snippet does not necessarily have to be at the end of your command though, it may appear anywhere.


To simplify things further, let's move this still a bit complicated DISPLAY=:0 xsel -ob to a script. I'll name it PASTE (because paste is already taken), but you can also call it differently.

To create the script file in a location where every user can run it without having to specify the full path (I recommend /usr/local/bin for this) and to make it executable, simply run those two commands:

( echo '#!/bin/bash' && echo 'DISPLAY=:0 xsel -ob' ) | sudo tee /usr/local/bin/PASTE
sudo chmod +x /usr/local/bin/PASTE

Now you can simply embed $(PASTE) into your commands on a TTY to insert the clipboard content from your desktop there.

4
  • 2
    Thanks! This will help me with a lot more things now than what I originally asked about! :)
    – user364819
    Jun 19, 2016 at 14:09
  • 1
    You could even store DISPLAY=:0 xsel -ob as Bash alias or small script anywhere (let's name it PASTE) and only use a shortened form like $(PASTE) to use clipboard contents in your commands.
    – Byte Commander
    Jun 19, 2016 at 14:42
  • Very nice approach Jun 19, 2016 at 19:13
  • Does this work on Ctrl+Alt+F2 ? I get this error: xsel: Can't open display: (null)
    – Shayan
    Mar 2 at 14:41
23

Personally, I just use text files:

  1. In your desktop environment, open a terminal and

    echo "whatever long text you have copied" > file
    
  2. Drop to the tty and

    $(cat file)
    
4
  • 2
    This is good to know. I actually didn't think that a simple command such as this would work so well. Sometimes the mind just thinks too complicated. Thank you! =)
    – Terrance
    Jun 19, 2016 at 14:55
  • 1
    I just use some-command ' shift+insert (to paste). ' return. Pasting inside single or double quotes prevents newlines in the pasted text from running the command before I can edit it to fix up any problems. IDK what it gains you to put the text in a file. Bash has very powerful line-editing keystrokes, like ctrl-left or ctrl-right arrow to move by words. alt-backspace to delete backwards words. Alt-d to delete forward words, etc. etc. Jun 20, 2016 at 11:43
  • 1
    Oh, I just figured out that the OP means a text console VT when he says TTY. Someone should really edit the question, because every terminal you can run a shell on is a tty, including pseudo-terminals controlled by xterm-like programs. Jun 20, 2016 at 11:45
  • @PeterCordes yeah but the term "tty" is often, if inaccurately, used to refer to virtual consoles.
    – terdon
    Jun 20, 2016 at 11:51
5

Another possible workaround not listed above involve the use of vim, pasting and running :!unix_command in command mode:

  1. copy the commands and the path to the clipboard
  2. open vim, go to command mode Esc, enter the prompt :
  3. type a bang ! and then paste Ctrl + Shift + V the command you previously copied in the prompt and execute
2

Another workaround with tmux that needs no configuration:

Launch tmux (Open terminal and type tmux) in the virtual "TTY" (Ctrl+Alt+F2 for Ubuntu) window and do your general work.

Whenever you need to paste something into that terminal session just open a regular desktop terminal (Ctrl+Alt+T) and enter tmux, then use keystrokes Ctrl+b , then w to list active sessions and find the one you wish to paste the content into. Once done, just close the terminal on your desktop and you may continue using "TTY" terminal as if you hadn't ever left it!

1
  • Thanks for editing @Kulfy! Looks like I thought it would when I originally wrote it haha! Mar 9, 2020 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy