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.

mreq
  • 4,752
  • 8
  • 42
  • 62

7 Answers7

48

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.

Olli
  • 8,811
  • 1
  • 34
  • 40
  • 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 '14 at 09: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 '14 at 09: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 '14 at 09:22
  • 1
    @olli, easier would be to press ctrl+a to go to the beginning of prompt – dǝɥɔS ʇoıןןƎ Feb 22 '19 at 07:45
  • @ratskin I don't know which terminal (emulator) you are using, that doesn't work for me. – Olli Feb 23 '19 at 08:32
  • It might be fn + shift + left-arrow or fn + left-arrow for you. – dǝɥɔS ʇoıןןƎ Feb 24 '19 at 09:18
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
Chris Stryczynski
  • 526
  • 1
  • 5
  • 14
  • 5
    An easier alternative is to add echo in front of the command and pipe to xclip. `$ echo | xclip -selection clipboard` – Sameer May 02 '19 at 14:46
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.

Przemek
  • 121
  • 3
  • Pressing , with vi option enabled opens **nano**. However, its cut-and-paste abilities are also confined within terminal. – samus Nov 09 '21 at 21:19
2

Not sure why all answers are so complicated, from bash 4.0 you can access current content with $READLINE_LINE. Combine with xclip and you have ready to go, working solution. Add this to your .bashrc

if [[ -n $DISPLAY ]]; then
  copy_line_to_x_clipboard () {
    printf %s "$READLINE_LINE" | xclip -selection CLIPBOARD
  }
  bind -x '"\C-y": copy_line_to_x_clipboard' # binded to ctrl-y
fi
kosciej16
  • 121
  • 1
  • This should definitely be the accepted answer. Even works in WSL and I can paste to my Windows side no problem. – 0x464e Jul 21 '23 at 10:19
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.

teraflik
  • 53
  • 7
kiri
  • 27,676
  • 16
  • 81
  • 117
0

IMO, kosciej16's answer is the most user-friendly option. But it doesn't work when you SSH into a remote machine (p.e. a VPS or a Raspberry Pi) without an X server.

This solution (added to your .bashrc) doesn't depend on X on remote machines:

copy_line_to_x_clipboard () {
  # using OSC 52
  printf "\e]52;c;%s\a" "$(printf %s "$READLINE_LINE" | openssl base64 -A)"
}
# bind it to Ctrl-Shift-x
bind -x '"\C-X": copy_line_to_x_clipboard'

But it works only in terminal emulators that support OSC 52 like Alacritty, kitty, st, tmux, Windows Terminal, iTerm2. See ojroques/vim-oscyank for a list of terminal emulators that support OSC 52.

MaxGyver
  • 221
  • 1
  • 8
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?

landroni
  • 5,861
  • 7
  • 35
  • 58