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.

7 Answers 7


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.

  • 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
  • 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
  • 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

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

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
  • This should definitely be the accepted answer. Even works in WSL and I can paste to my Windows side no problem.
    – 0x464e
    Jul 21, 2023 at 10:19

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.

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

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).


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


  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.

  • 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

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?


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .