35

Is there a way to copy the currently written content of the bash prompt? Say I wrote:

ping www.google.com

so that the lines in terminal looks like:

petr@sova:~$ ping www.google.com

and didn't hit return yet. I want to copy that text to clipboard. Is that possible?

The usual usecase would be hitting up several times and finding a command which I want to copy.

5 Answers 5

46

To cut, press ctrl+u. To paste, use ctrl+y. This copies whole line to bash clipboard. If you're using X and default Ubuntu terminal, you can use your mouse to mark contents and press ctrl+shift+c to copy, and ctrl+shift+v to paste.

6
  • 10
    I am using Xubuntu and while ctrl+u and ctrl+y works in the terminal, I need to be able to cut/copy from terminal and paste in a different app, which doesn't work. Possibly, ctrl+u cuts the text into terminal's own clipboard, not the global one?
    – mreq
    Jan 31, 2014 at 9:19
  • You have to use X's clipboard. Try ctrl+shift+c and ctrl+shift+v. If you're using terminal (without graphical UI), you're out of luck. If you want to append commands to script file, you can use echo ping www.google.com >> filename.txt to work around it.
    – Olli
    Jan 31, 2014 at 9:21
  • And tip: you can prefix commands easily by pressing ctrl+u, writing echo and then hitting ctrl+y to paste original command back.
    – Olli
    Jan 31, 2014 at 9:22
  • 1
    @olli, easier would be to press ctrl+a to go to the beginning of prompt Feb 22, 2019 at 7:45
  • @ratskin I don't know which terminal (emulator) you are using, that doesn't work for me.
    – Olli
    Feb 23, 2019 at 8:32
6
  • Add a # to the front of the command (so it becomes a comment)
  • Run it
  • Grab it from the history and pipe it to a clipboard utillity like xclip: history | tail -n 1 | sed "s/[[:digit:]]* //" | sed "s/^#//" | xclip
1
  • 5
    An easier alternative is to add echo in front of the command and pipe to xclip. $ echo <command> | xclip -selection clipboard
    – Sameer
    May 2, 2019 at 14:46
2

If you don't mind using the mouse, just triple click on the line you want to copy then press Ctrl+Shift+C. You can then paste it with Ctrl+V.

1
  • 3
    should be ctrl+shift+c to copy, shouldn't it?
    – Info5ek
    Apr 23, 2017 at 8:50
2

Vi mode solution

Keyboard-only solution. All characters are copied exactly "as-is". Out of the box - pure shell solution, no dependencies (except xsel obviously).

Setup

  1. Set vi option in terminal: set -o vi.
  2. Alias copying unclosed here-document: alias c2c='cat - <<"" | tr -d '"'"'\n'"'"' | xsel -b'

Usage

  1. Write anything you want to the command line without pressing <return>.
  2. Press <escape> followed by <d>, <d>. Whole line should now disappear. To be able to type again, press <i>.
  3. Type name of the alias (c2c in my example) and press <return>.
  4. Press <escape>, <p> then <return>, <return>.

Voilà. Deleted text can be now pasted anywhere by pressing <ctrl>+<v> (GUI), or <ctrl>+<shift>+<v> (reasonable CLI). If Your CLI isn't reasonable, make it so.

fzf solution

Some likes like vi mode, others not really. But I think fuzzy finder is must have. It doesn't allow to do exactly what you've asked, it can do something better! Instead of "hitting up several times" fzf can browse command history with order of magnitude better efficiency. Moreover, it can be used inside my c2c alias to copy to clipboard. Whatever is typed to the terminal at the moment cannot be nicely copied (without mouse), but if something is in history, it is a matter of seconds to get it.

1
  • Pressing <esc>, <v> with vi option enabled opens nano. However, its cut-and-paste abilities are also confined within terminal.
    – samus
    Nov 9, 2021 at 21:19
0

In xfce4-terminal, you need to select with the mouse the text you'd like to copy, then right-click and then select the Copy item in the context-menu.

Does this work?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.