37

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,932

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
  • 9,129
8

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

For zsh

if [[ -n $DISPLAY ]]; then
    copy_line_to_x_clipboard() {
        echo -n $BUFFER | xclip -selection clipboard
        zle reset-prompt
    }
    zle -N copy_line_to_x_clipboard
    bindkey '^Y' copy_line_to_x_clipboard
fi
hiru007
  • 45
kosciej16
  • 181
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
3

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.

kiri
  • 28,986
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
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
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
  • 6,011