kutsan / zsh-system-clipboard

System clipboard key bindings for Zsh Line Editor with vi mode. It is similar to what `set clipboard=unnamed` does for vim.
GNU General Public License v3.0
149 stars 16 forks source link

New lines are detected and trimmed when copying and pasting #16

Closed doronbehar closed 6 years ago

doronbehar commented 6 years ago

There is a problem when cutting or copying and pasting a multi-line text when using this plugin, I'll explain with an example: Start with a multi-line buffer like this (extracted from our license):

share and change all versions of a program--to make sure it remains free
GNU General Public License for most of our software; it applies also to
software for all its users.  We, the Free Software Foundation, use the

If you will copy this and paste it in an empty $BUFFER, it will be pasted fine. How ever, if you'll try to replace the 2nd line with the 3rd for example - by pressing dd and then p (with the default mappings) you'll get the following result:

share and change all versions of a program--to make sure i
t remains free
sGNU General Public License for most of our software; it applies also tooftware for all its users.  We,
the Free Software Foundation, use the

That happens because our alias zsh-system-clipboard-set (it happens even with a function) trims newlines from the $CUTBUFFER because we use printf '%s' and not printf '%s\n' in zsh-system-clipboard-vicmd-vi-delete or zsh-system-clipboard-vicmd-vi-yank. Never the less, we can't use in these functions printf '%s\n' unconditionally because this would imply that every yank or delete operation was done on a whole line.

Additionally, zsh-system-clipboard-vicmd-vi-put-after and zsh-system-clipboard-vicmd-vi-put-before should know how to handle newlines correctly. This is even more problematic then you think since these functions will read the clipboard from zsh-system-clipboard-get without a trailing new line (see this Stack Exchange question).

I'll propose a fix for that shortly.