1

I want to paste some text into Vim in GNOME Terminal on Ubuntu 20.04. Before doing that, I run:

:set paste

I use that so that autoindent and other Vim features don't change the text that I am copying.

Then I enter insert mode, and I press Ctrl+Shift+V to paste the text into the document.

I have found that it prefixes the text copied ("foobar" in this example) with this:

<F22>foobar

Yes, that is the lesser-than sign, followed by the letter F, followed by the number 2, followed by the number 2, followed by the greater-than sign. Does anyone know why this appears?

  • 1
    Where are you copying the text from? Can you reproduce it if you copy it from, say, gedit? Can you reproduce it if you paste it into gedit? Can you start a command that takes input on gnome terminal (such as cat or tail) and paste it there to see how it will show up? – filbranden May 6 at 16:54
  • 2
    Any chance this is bracketed paste mode going wrong? What happens if you don’t set paste or enter insert mode before pressing <c-s-v>? – Rich May 6 at 17:13
  • @filbranden If I copy it from gedit, I still get the same issue. If I paste into gedit, I don't see <F22>. If I run cat and press Ctrl-Shift-V, I don't see <F22> – Flimm May 7 at 8:44
  • @Rich If I don't run :set paste, I don't see <F22> once I enter insert mode and press Ctrl-Shift-V. If I don't enter insert mode first, pasting seems to work! This is a surprise to me, as I remember it not working but treating the pasted text as if I had typed them (i would enter insert mode, a would append, etc). But it could well be that an update or a config modification changed this. Any idea of what might have changed this? – Flimm May 7 at 8:46
  • 1
    @Flimm You probably upgraded either Vim or your terminal: bracketed paste mode is a reasonably new feature. – Rich May 7 at 10:48
2

Thanks to a hint from Rich, I found a workaround.

Many terminal emulators (such GNOME Terminal) support bracketed paste mode. When you paste text in these terminals, the ANSI escape sequence ESC [ 2 0 0 ~ is added as a prefix, and ANSI escape sequence ESC [ 2 0 1 ~ is added as a suffix. It will send the text with the prefix and the suffix to whatever program is running in the foreground (in this case, Vim).

If you are using a version of Vim that supports bracketed paste mode, Vim will detected these ANSI escape sequences and know what to do automatically. You can paste code without entering insert mode (and without running :set paste for that matter), and it will paste the text as you expect. Vim 8 should support this. Run :help xterm-bracketed-paste to check.

However, for some reason, if you do run :set paste and enter insert mode, Vim seems to be converting the opening ANSI escape sequence to <F22>, and I'm not sure why. The workaround is to not use :set paste and to just paste the text when in command mode.

| improve this answer | |
2

This turned out to be caused by Vim inserting text because of the escape sequences sent as part of bracketed paste mode. The solution is simply to paste directly into your terminal with Vim in normal mode and 'paste' unset.

If you don't want to use bracketed paste mode (which you implied in your answer by calling it a "workaround"), and would prefer to use your old workflow of manually entering insert mode and pasting the text into your terminal to be sent to Vim as individual keystrokes, you can disable bracketed paste mode entirely by adding the following command to your .vimrc:

set t_BE=

When this option is unset, Vim will disable bracketed paste mode by sending the contents of the 't_BD' option to the terminal and the terminal will then just send the contents of the clipboard with no extra escape sequences when pasting.

| improve this answer | |
  • Thank you. I am going to continue to use bracketed paste mode. I still don't understand why <F22> appeared though. Can't Vim handle it? How does the ANSI escape sequence get converted to <F22> specifically? – Flimm May 7 at 12:35
  • 1
    @Flimm In your Vim's internal termcap database, the definition of <F22> may match the start of a bracketed paste sequence. Check out the output of :set <F22> or :echo &t_FC. The issue is affected by $TERM and your terminfo db. FWIW, the definition of <F22> in the gnome-256color terminal description is kf22=\E[21;2~, which doesn't match the start of a bracketed paste sequence (and which is why I can't reproduce your issue). – user938271 May 7 at 17:29
  • Running :set <F22> outputs: t_FC <F22> ^[[200~. What is the definition of <F22> used for? – Flimm May 7 at 18:24
  • @flimm It's the keycode Vim expects when you press the F22 key on your keyboard. The fact that's set up in your configuration to use the same escape sequence as bracketed paste mode explains the behaviour. – Rich May 7 at 18:35

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.