0

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,

CLIPBOARD_STUFF PS1$

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

1 Answers1

1

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)
        READLINE_LINE=${l:0:$n}$s${l:$n:$((${#l}-n))}
        READLINE_POINT=$((n+${#s}))
}

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

Read the manual for details.

yaegashi
  • 12,326
  • 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? –  Aug 06 '15 at 05: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..... –  Aug 06 '15 at 11:04
  • Don't you ever yank a text in the middle of command line by moving a cursor? – yaegashi Aug 06 '15 at 11:10
  • yes, i do... really didn't think of it.. thanks man.. –  Aug 06 '15 at 11:13