ecjb
ecjb

Reputation: 5459

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?

Upvotes: 3

Views: 4795

Answers (2)

Britt Selvitelle
Britt Selvitelle

Reputation: 461

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:

(Which this question is a duplicate of.)

Upvotes: 7

compuphys
compuphys

Reputation: 1377

You first have to be sure that your vim installation has clipboard support. Open vim and type:

:version

You'll see the features included in your installation have a + next to them, those that aren't have a -, for example:

Huge version with GTK2 GUI.  Features included (+) or not (-):
.
.
.

+clientserver      -footer            +mouse_netterm     +smartindent       +wildmenu
+clipboard         +fork()            +mouse_sgr         +startuptime       +windows
.
.
.

^^^^^^^^^^^^^ see here that clipboard is included. If you don't have clipboard support, see this stackoverflow answer on how to get it in MacOS.

Once you have +clipboard you can yank into the system clipboard by first typing "+ to tell vim to use the '+ register' (see How do I use vim registers?), followed by the normal y to yank the text.

Upvotes: 0

Related Questions