1

I am using zsh.

I will right-click copy something from the zsh window, and then right-click paste it. I always lose some amount of characters and the capitalization of the last character flips.

e.g.

echo this is a long message

pastes as

(empty line)

this is a long messaG

and

vim hello.txt

becomes

m hello.tX

What could be causing this and how do I fix it?

  • Vim keybindings and an escape character in your copied text? – muru Jul 20 '17 at 2:13
  • Does your terminal support bracketed paste? – meuh Jul 20 '17 at 10:34
3

What is causing this is twofold:

  • Something, possibly the Z shell, told your terminal to turn on bracketed paste.
  • The Z shell is using the vicmd or viins keymap.

What is happening is this:

  1. The Z shell starts up, and issues the control sequence to turn on bracketed paste in the terminal emulator. Or maybe a text editor did this at some point, and something caused it not to issue the control sequence to turn it back off again.
  2. You paste something.
  3. The terminal emulator sends the start of bracketed paste control sequence, the pasted text, and the end of bracketed paste control sequence.
  4. The first character of the start bracketed paste control sequence is ESC, which cancels ZLE's vi insert mode and returns to ZLE's vi command mode.
  5. Subsequent characters are vi commands and do not do much, until you reach the letter o or i, which enter insert mode.
  6. The rest of the text is inserted in insert mode.
  7. The first character of the end bracketed paste control sequence is ESC, which cancels ZLE's vi insert mode and returns to ZLE's vi command mode.
  8. Subsequent characters of the control sequence do not do much.
  9. The final character of the end bracketed paste control sequence is ~, which is the vi command to swap case. This causes the final letter of the pasted text to be swapped between upper and lower case.

You fix this in one of two ways:

  • Stop the Z shell from turning bracketed paste on; as it clearly has not been configured to handle it in vicmd/viins keymaps.
  • Make bracketed paste work in vicmd/viins keymaps. How you do this depends from how you have made the Z shell recognize bracketed paste in the first place. The safe-paste plug-in rather assumes emacs keymaps, and has to be tweaked to work well with vi keymaps. But that's not the only way to get bracketed paste in the Z shell nowadays.

Further reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.