5

Today, I wanted to import a public GPG key. To do so, I have navigated to page, copied the key, opened up vim, entered insert mode by pressing i and pasted the text.

Every line has been pasted fine except the following line:

EgdlR1BHAAEBCRCMcY07UHLh9bDbAJ4mKWARqsvx4TJ8N1hPJF2oTjkeSgCeMVJljxmD+Jd4

That line appears as:

gCeMVJljxmD+Jd4

Note that this corresponds to trailing part of the line.

Then I have exited from the insert mode and pressed u. Now, the line has been turned into:

EgdlR1BHAAEBCRCMcY07UHLh9bDbAJ4mKWARqsvx4TJ8N1hPJF2oT

Note that this corresponds to the beginning portion of the line.

All in all, the only characters not present in any of the two forms are:

jkeS

My speculation is, for some reason, when text is pasted, this character sequence acts like a command that says: 'Delete all characters in this line, up to this point'. I can reproduce the behavior with a line like:

aaaajkeSbbbb

So, I have two questions regarding this:

  1. Why exactly does this character sequence, jkeS, causes this behavior? What is going on behind the scenes?
  2. What exactly is the use case for this behavior? I think there must be a use case for this because this is AFAIK a feature, not a bug. Even, there is a paste mode in vim which we activate by typing :set paste for correct pasting, which is a fact that supports that this 'weird' pasting behavior is a feature, definitely not a bug.
  • 1
    I have set pastetoggle=<F2> in my .vimrc and press <F2> before & after pasting – Rolf Oct 6 '16 at 12:05
10

Why exactly does this character sequence, jkeS, causes this behavior? What is going on behind the scenes?

Your gpg key contains jk which you likely mapped to <Esc> (it is very common) so:

  1. Vim sees jk, it executes your custom mapping and exists insert mode.
  2. The next character is e so Vim jumps to the end of the current word.
  3. The next character is S so Vim cuts the whole line and enters insert mode.
  4. The rest of your gpg key is typed in.

What exactly is the use case for this behavior?

Pasting is handled as a series keystrokes, not as an actual paste.

  • If the text you paste contains newlines, Vim will autoindent just like if you typed everything.
  • If the text you paste contains character sequences that match with mappings those mappings are executed.
  • And so on…

This makes pasting using your system/terminal's tools a pretty hazardous affair. By setting the paste option, you disable a bunch of features like autoindenting and insert mode mappings which allows pasting to work more like actual pasting. Don't forget to unset it afterwards!

If you can, I'd suggest you get a proper Vim, built with the +clipboard feature, and use p instead of relying on your system/terminal emulator tools.

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.