13

In this thread the top answer shows how to copy text that has been previously selected with the mouse on a gnome-terminal, to the clipboard in X11.

My question is: Say I copy a piece of text from the terminal using bash set-mark and copy keyboard shortcuts (i.e. set-mark + M-w). Is it possible to share this clipboard with X11?

EDIT: In the original question, I talked about sharing the clipboard with GNOME, but as Gilles points out below, GNOME doesn't specifically have a clipboard (it's general to X), so I have updated the question.

2
  • In gnome-terminal, you can also type Ctrl+Shift+C and Ctrl+Shift+V to copy and paste to the system clipboard.
    – Lie Ryan
    Commented Aug 14, 2011 at 17:49
  • 2
    See this post stackoverflow.com/questions/994563/… But it is not working for me.
    – great q
    Commented Feb 7, 2015 at 18:05

2 Answers 2

11

Bash's clipboard is internal to bash, bash doesn't connect to the X server.

What you could do is change the meaning of M-w to copy the selection to the X clipboard¹ in addition to bash's internal clipboard. However bash's integration is pretty loose, and I don't think there's a way to access the region information or the clipboard from bash code. You can make a key binding to copy the whole line to the X clipboard.²

if [[ -n $DISPLAY ]]; then
  copy_line_to_x_clipboard () {
    printf %s "$READLINE_LINE" | xsel -ib
  }
  bind -x '"\eW": copy_line_to_x_clipboard'
fi

If you want to do fancy things in the shell, switch to zsh, which (amongst other advantages) has far better integration between the line editor and the scripting language.

if [[ -n $DISPLAY ]]; then
  x-copy-region-as-kill () {
    zle copy-region-as-kill
    print -rn -- "$CUTBUFFER" | xsel -ib
  }
  x-kill-region () {
    zle kill-region
    print -rn -- "$CUTBUFFER" | xsel -ib
  }
  zle -N x-copy-region-as-kill
  zle -N x-kill-region
  bindkey '\C-w' x-kill-region
  bindkey '\ew' x-copy-region-as-kill
fi

¹ Gnome doesn't specifically have a clipboard, this is general to X.
² As of bash 4.1, there is a bug in the key parsing code: key sequences bound with bind -x may not be more than two characters long. I think bash 4.2 fixes some cases of longer prefixes but not all of them; I haven't researched the details.

2
  • Finally! Thanks a bunch! This copy_line_to_x_clipboard is exactly what I was missing for a very long time: the copy equivalent of shift+insert. I changed to ctrl-x (bind -x '"\C-x") because Esc is too far to my taste (and C-c would be the dumbest possible choice).
    – pbarill
    Commented Jul 15, 2018 at 17:26
  • Terminals normally have a meta key, which is bound to the alt key by default. Typing a character while holding it down is translated to esc followed by that character, so you can type it as alt-w.
    – user293144
    Commented Oct 18, 2018 at 19:16
9

@Gilles already gave an excellent answer. I would just like to mention the existence of xclip, which is also a very useful way to copy terminal output to the X clipboard, by just piping anything into it:

$ cat /etc/passwd | xclip
1
  • 3
    Just a note that xclip copies into the primary buffer by default. To use the clipboard, use xclip -selection clipboard.
    – Sparhawk
    Commented May 30, 2014 at 6:23

You must log in to answer this question.

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