A note on bracketed paste mode in terminal and Neovim.

I use ZOC terminal to connect to my remote server. Today, I noticed a strange issue when I copied text from elsewhere to the terminal shell command line. When I copied some text and pasted it in the terminal, I found that 0~ was added in the beginning of text, and 1~ was added in the end of text.

I found a similar issue on stackexchange. It turns out that this has something to do with bracketed paste mode in terminal emulators. Without this mode, our terminal can not discern the difference between pasting text and typing text manually. But how is this even important? A noticeable example is when we paste multi-line code into Vim. Since the code have already been properly indented, we do not want Vim to alter it. Without this terminal ability, Vim does not know whether you are typing the text or paste it from the terminal. So when it meets a newline character, it will try to re-indent the following line, causing a complete mess when pasting code into a Vim buffer.

If the terminal supports bracketed paste mode, to enable it, use the following command:

printf '\e[?2004h'

To disable this mode, use printf '\e[?2004l'.

For plain Vim, see this post on how to enable bracketed paste mode. If you are using Neovim, bracketed paste mode in builtin, if your terminal supports it, you do not need to do any setup. Neovim will just work, see also :h bracketed-paste-mode inside Neovim.

Ref: