2

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?

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • Shells have their own internal buffers. You could use something like pbcopy/xclip depending on your system – D. Ben Knoble Apr 28 '20 at 13:37
  • Many thanks for your comment @D.BenKnoble. Do you have any idea about which command I should write in the `.zshrc`? – ecjb Apr 28 '20 at 13:47

2 Answers2

5

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.)

0

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.

compuphys
  • 1,289
  • 12
  • 28
  • Thanks but this question is about `zsh` with vim-style keybindings, not Vim. – Peter Davis Apr 05 '21 at 21:22
  • Actually this response lead me to the actual issue. I was trying to figure out why I can't copy to global keyboard in vim when using zsh, when actually the problem was my Ubuntu 22.04 did not include a vim with clipboard support. – Alex Burdusel Mar 12 '23 at 16:04