Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yank in visual (Vim) mode in zsh does not copy to clipboard in ordert to paste with Ctrl + d in other applications

I recently moved from bash to zsh and use vim keybindings in zsh.

When I highlight a string in visual and yank it with y, I can then paste it inside of zsh without problem. However when I try to paste that same string outside of zsh (with the command Ctrl + d) it does not work. Instead the last copied item with Ctrl + c is copied there.

Is there an additional command to write in the .zshrc?

like image 396
ecjb Avatar asked Oct 28 '25 09:10

ecjb


1 Answers

By default zsh yanks to its own internal registers. Luckily, like in Vim, it's fairly simple yank to the system clipboard.

# vi mode
bindkey -v

# Yank to the system clipboard
function vi-yank-xclip {
    zle vi-yank
   echo "$CUTBUFFER" | pbcopy -i
}

zle -N vi-yank-xclip
bindkey -M vicmd 'y' vi-yank-xclip

Replace pbcopy with the method of your system, for example xclip if you're on Linux.

Further reading and a couple alternatives at:

  • https://unix.stackexchange.com/questions/25765/pasting-from-clipboard-to-vi-enabled-zsh-or-bash-shell
  • How do I yank into the system register from v-imode?

(Which this question is a duplicate of.)

like image 189
Britt Selvitelle Avatar answered Oct 31 '25 02:10

Britt Selvitelle