26

I'm looking for a way to select, copy and paste the text in the terminal completely without the mouse.

I've been using tmux until now, but I find its key combinations a bit complex for everyday use of copy & paste, and I'm now looking for alternatives.

Do you guys have any suggestions?

To be clear, this is not about piping to the clipboard but about selecting the text as it's possible in e.g. tmux!

kos
  • 35,891
Morten
  • 768
  • See my answer here: http://askubuntu.com/a/756348/295286. Ive made a function that will copy whatever text is in front to clipboard. That works for copying the commands. As for output, it has to be piped – Sergiy Kolodyazhnyy Apr 20 '16 at 13:28
  • 3
    Okay guys this has nothing to do with stdout- it's about moving a cursor to some previous printed text Select it and Copy/paste it to the current line of input in the terminal:-D

    Sorry for not being more precise in description.

    – Morten Apr 21 '16 at 06:11
  • The best way to add additional information to your question is by editing it, with the [edit] button. It is better visible that way, and comments are mainly for secondary, temporary purposes. Comments are removed under a variety of circumstances. Anything important to your question should be in the question itself. – guntbert Apr 29 '16 at 19:01

5 Answers5

12

I always use Ctrl+Shift+C and Ctrl+Shift+V to copy and paste in my (non tmux) terminal.
You can change these keyboard shortcuts in the keyboard preferences of your (non-tmux) terminal.

If however you want to keep using tmux you can also change the keyboard shortcuts of tmux by adding commands to ~/.tmux.conf. simply add the following in that file:

bind-key -n [shortcut. for example Ctrl+v is C-v] [what you want to do. for example 'new-window' etc]

It will look like this

bind-key -n C-t new-window

You don't need the command prefix key to execute the commands in the ~/.tmux.conf file.

For more info about the things available for ~/.tmux.conf check this link

Very rudimentary copying and pasting can be done using this however for a bit more usefulness I recommend using a Vim-style copy/paste config.
Enabling a Vim-style of copy/paste can be done with this config file or another config file. These do however require the use of the prefix key.
Someone even made a tmux extension to simplify the copy/paste action to the system clipboard in tmux. It might be worth a look.

If you want an alternative for Tmux you might try dvtm (sudo apt-get install dvtm dtach). I haven't used it myself but it looks like it has similar options.

Akisame
  • 3,313
  • 7
  • 32
  • 63
6

To copy, use Ctrl+Shift+C

To paste, use Ctrl+Shift+V or Shift+insert

Another way to do this:

First run command screen, after then can do following steps:

  • Press Ctrl+a+Esc It will put the screen in copy mode.
  • Now, move the cursor to the beginning of the section to copy & hit enter.
  • then, move the cursor to the end of the section to copy & hit enter.
  • Now, press Ctrl+a+] to paste.
d a i s y
  • 5,511
6

You could use screen selection mode.

Enter a screen session using screen command.

Then, use Ctrl+a and Esc to enter into the selection mode.

You can then move your cursor, select some text using space, quit the selection mode, and paste it when you want with Ctrl+a and ].

More information here: http://web.mit.edu/gnu/doc/html/screen_11.html

blue112
  • 181
2

Consider xclip, which is a command-line interface to the X clipboard, and is available with sudo apt-get install xclip.

You may have noticed that in Ubuntu, if you select some text and then press the middle mouse button, that text will get pasted into whatever input is in focus. xclip hooks right into that, so if you run seq 10 | xclip -i then middle-mouse somewhere, you'll paste the numbers 1 to 10. If you were to select my username then run xclip -o | cowsay, you'll get a cow saying the word "ymbirtt".

ymbirtt
  • 269
  • 8
    I'm surprised this answer is accepted. @morten specifically pointed that this is not about piping to the clipboard but about selecting the text as it's possible in e.g. tmux. It seems like the answers from @daisy and @blue112 are on point. – Tad Lispy Oct 10 '17 at 09:16
  • 2
    @TadeuszŁazurski, so am I, honestly. xclip is a really good tool that complements the existing answer, but I'm not sure it's the answer – ymbirtt Oct 10 '17 at 09:58
  • This answer does not seem to provide a way to select text in the terminal without the mouse. Or do I miss something? – Zach Jan 20 '22 at 15:03
  • 1
    @Zach Yes, like I say, I'm not totally sure why this is accepted. The "Note: this is not about piping to clipboard" part of OP's question was actually edited in after I posted this. I still think that this answer is useful for anyone who wants to be able to use the clipboard as part of a terminal-based workflow, but it's definitely not what the OP asked for! – ymbirtt Jan 20 '22 at 15:09
0

It can be done with emacs.

  1. Open emacs
  2. Alt-X + term: opens a terminal in a new emacs buffer
  3. Ctrl-C+Ctrl-J: enter line mode (more details on the modes below)
  4. Select the desired text
  5. Alt-W: copy the text
  6. Ctrl-C+Ctrl-K: go back to char mode
  7. Ctrl-Y: paste

Remarks on line-mode and char-mode

In char-mode, each character you type is sent to the terminal (like in any terminal) and most emacs shortcuts are disabled (to enable terminal shortcuts). In particular, don't be surprised if shortcuts to change the buffer do not work. To change the buffer, you must first enter line mode. In line mode emacs behave like in a normal emacs buffer. More details here.

Zach
  • 151