惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
博客园 - 叶小钗
雷峰网
雷峰网
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
量子位

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Bracketed Paste Mode in Terminal
2021-02-01 · via jdhao's digital space

A note on bracketed paste mode in terminal and Neovim.

Annoying character inserted when pasting text#

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.

What is bracketed paste mode#

Typically, when we paste code snippet into a terminal application, it can not tell whether we are typing the text or we are pasting it. This may have unintended side affects for some application. For example, Vim will auto-indent the code pasted into it when it sees a newline, breaking the code structure.

Here comes the bracketed paste mode. When this feature is enabled, if we paste text into terminal, the terminal will add some special sequence in the beginning and end of the text (\e[200~ in the beginning and \e[201~ in the end).

How enable and disale it in terminal#

To check if bracketed paste mode is on and supported, first use printf "\e[?2004h" to turn on it. Then copy some text, run xxd, and paste some text into terminal. If you terminal supports bracketed paste mode, then you will see output like the following:

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

Why is it important?#

But how is this even important? If a terminal program also supports bracketed paste mode, it can interpret the special terminal sequence and know that the user is pasting text instead of typing it.

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. If Vim supports bracketed paste mode, when it see the special terminal sequence, it knows that the user is trying to paste text into it. It will not auto-indent your pasted text any more, thus you do not need to use set paste in order to paste something.

Enable bracketed paste mode in Vim/Neovim#

For plain Vim, see this post on how to enable bracketed paste mode. If you are using Neovim, bracketed paste mode in built-in, 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#