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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园_首页
雷峰网
雷峰网
V
Visual Studio Blog
爱范儿
爱范儿
A
About on SuperTechFans
量子位
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
S
SegmentFault 最新的问题
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More

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
Using Virtual Text in Neovim
2021-09-09 · via jdhao's digital space

In Neovim, we can use the virtual text feature to place virtual text anywhere in the current buffer, which is not possible with normal text.

A bit of history#

Initially, the Nvim core dev bfredl introduced nvim_buf_set_virtual_text() to add virtual text to a buffer in this PR.

Over the time, the code is refactored and deprecated, in favor of the new API function nvim_buf_set_extmark()1.

Plugins utilizing virtual text#

Plugins have been using virtual text feature to do various things. Here is a non-exhaustive list:

Using extmarks#

The nvim_buf_set_extmark() function has a lot of parameters, and some of them can be confusing for users due to the conciseness of its documentation.

Here is a working example to demonstrate its usage:

local api = vim.api

local bnr = vim.fn.bufnr('%')
local ns_id = api.nvim_create_namespace('demo')

local line_num = 5
local col_num = 5

local opts = {
  end_line = 10,
  id = 1,
  virt_text = {{"demo", "IncSearch"}},
  virt_text_pos = 'overlay',
  -- virt_text_win_col = 20,
}

local mark_id = api.nvim_buf_set_extmark(bnr, ns_id, line_num, col_num, opts)

In the above, line_num specifies which line we want to put the virtual text. col_num specifies which column we want to put virtual text (but this is actually only half true, since we can put virtual text at any position). col_num shouldn’t exceed the actual column number of the line indicated by line_num. Otherwise, we get the following error message:

E5113: Error while calling lua chunk: test.lua:17: col value outside range

In the opts parameter, there are more options to change the behavior of this function. virt_text is a table of table, where each sub-table contains some text and the highlight group for it.

The option virt_text_pos has three possible values:

  • overlay: overlay the virtual text on the column specified with col_num, this is actually the only case where col_num really works.
  • eol: virtual text is placed at the end of the line (col_num does not take effect).
  • right_align: virtual text is placed on the right of current line (col_num does not take effect).

Check the title image for a comparison between the three position method.

To actually put virtual text on any column, we should use virt_text_win_col in the opts table. If we use virt_text_win_col, virt_text_pos does not take effect any more.

To delete a virtual text, use API function nvim_buf_del_extmark(bnr, ns_id, id), where id is the id used in opts.

References#