Using this link, i wrote custom function to yank text from x-clipboard to shell terminal on pressing C-y. I see two issues here,

copy_line_from_x_clipboard () {
    xsel -o

bind -x '"\C-y": copy_line_from_x_clipboard'

1) It adds shell prompt string, PS1 after pressing C-y. I prefer this function to behave exactly like Ctrl - Shift -v. Presently, it outputs,


2) It empties the system clipboard, after yanking the text first time. Second time, i press C-y, no more contents are getting yanked.


You need to update $READLINE_LINE and $READLINE_POINT in the function. Insert xsel -o output at $READLINE_POINT of $READLINE_LINE.

copy_line_from_x_clipboard() {
        local n=$READLINE_POINT
        local l=$READLINE_LINE
        local s=$(xsel -o)

bind -x '"\C-y": copy_line_from_x_clipboard'

Read the manual for details.

| improve this answer | |
  • it works, yaegashi.... i understand READLINE_LINE changes are reflected in terminal... the new READLINE_LINE is a concatenated version of old contents plus yanked stuff. In the answer, i see a third string${l:$n:$((${#l}-n)). What does it do? – user93868 Aug 6 '15 at 5:16
  • i donno, why READLINE_LINE is lengthy in the above answer but READLINE_LINE=${l:0:$n}$s does the work neatly as well..... – user93868 Aug 6 '15 at 11:04
  • Don't you ever yank a text in the middle of command line by moving a cursor? – yaegashi Aug 6 '15 at 11:10
  • yes, i do... really didn't think of it.. thanks man.. – user93868 Aug 6 '15 at 11:13

Your Answer

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