Pasting from clipboard to vi-enabled zsh or bash shell
Zsh doesn't support anything but internal registers, and bash doesn't support register at all as far as I know. By and large, shells support vi commands, not vim commands.
In zsh, here's a proof-of-concept for accessing the X selection from command mode. For real use you'd want to elaborate on these techniques. I use the xsel
program, you can use xclip
instead; see How to copy from one vim instance to another using registers. You'll find the features I used in the zle manual.
vi-append-x-selection () { RBUFFER=$(xsel -o -p </dev/null)$RBUFFER; }
zle -N vi-append-x-selection
bindkey -a '^X' vi-append-x-selection
vi-yank-x-selection () { print -rn -- $CUTBUFFER | xsel -i -p; }
zle -N vi-yank-x-selection
bindkey -a '^Y' vi-yank-x-selection
The function vi-append-x-selection
inserts the current X selection after the cursor (similar to p
or P
). The function vi-yank-x-selection
copies the last killed or yanked text to the X selection. zle -N
declares the functions as zle widgets (i.e. edition commands). bindkey -a
sets bindings for vi command mode.
Here is a solution for zsh (vi mode) that wraps the original widgets so the clipboard always is synchronized
Replace xclip
with your preferred clipboard tool.
function x11-clip-wrap-widgets() {
# NB: Assume we are the first wrapper and that we only wrap native widgets
# See zsh-autosuggestions.zsh for a more generic and more robust wrapper
local copy_or_paste=$1
shift
for widget in $@; do
# Ugh, zsh doesn't have closures
if [[ $copy_or_paste == "copy" ]]; then
eval "
function _x11-clip-wrapped-$widget() {
zle .$widget
xclip -in -selection clipboard <<<\$CUTBUFFER
}
"
else
eval "
function _x11-clip-wrapped-$widget() {
CUTBUFFER=\$(xclip -out -selection clipboard)
zle .$widget
}
"
fi
zle -N $widget _x11-clip-wrapped-$widget
done
}
local copy_widgets=(
vi-yank vi-yank-eol vi-delete vi-backward-kill-word vi-change-whole-line
)
local paste_widgets=(
vi-put-{before,after}
)
# NB: can atm. only wrap native widgets
x11-clip-wrap-widgets copy $copy_widgets
x11-clip-wrap-widgets paste $paste_widgets
Selection and clipboard are different things under X Window, and IMHO "desktop environments" tend to make the issue even more murky than it was.
Does shift-insert work for you? On bare X applications, it is bound to pasting the selection when such a binding is made.