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

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
WordPress大学
WordPress大学
爱范儿
爱范儿
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
博客园_首页
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理

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
Regex Keyword and Python Interpolation in Ultisnips
2020-01-05 · via jdhao's digital space

Vim-snippet provides a lot of useful Ultisnips snippets for various filetypes. For example, for Markdown, to insert level 1 header, the snippet trigger is sec, and for level 2 and 3 header, the triggers are ssec and sssec respectively. These triggers are non-intuitive and hard to remember and type.

Today, I want to share how to create just one Markdown header snippet using regex keyword and Python interpolation1 with Ultisnips. The final effect of the snippet is that you can type h{NUM} (where NUM is a header level) and press Tab will create the corresponding headers.

Creating a header snippet#

In order to create a single snippet with chaning trigger keyword, we need to use regex in the trigger word. So the first line of the snippet looks like this:

snippet "h([1-6])" "Markdown header" br

Note the br flag at the end of the line:

  • b: This snippet only triggers at the beginning of a line.
  • r: This snippet contains regex and is not a normal snippet.

The idea is that we will capture the number after h in the begining of a line. This number must be in the range [1-6] to trigger the snippet since there are only six header levels in Markdown.

Also note that, unlike other snippet trigger, the regex trigger word needed to be quoted with double quotes.

A simple snippet#

Combining regex keyword and Python interpolation, we can create a simple header snippet like the following one:

snippet "h([1-6])" "Markdown header" br
`!p snip.rv = int(match.group(1))*'#'` ${1: Section Title} `!p snip.rv = int(match.group(1))*'#'`

$0
endsnippet

In the above snippet, we use two same Python interopolation blocks to create the left and right header level marker, and a regular snippet text is between them.

Inside Python interpolation block, Ultisnips provides the match object, which is the captured result from regex keyword. match.group(1) will be the content of the first capture group, and in this case, it will be the number in string format. The rest of the code is self-explaining so I will not elaborate.

A complex one using post_jump#

A more advanced way is to use post_jump actions, which can perform various complicated logics inside a Python function.

With post_jump, the header snippet now looks like this:

global !p
def gen_header(snip):
    placeholders_string = snip.buffer[snip.line].strip()
    level = int(placeholders_string[0])

    # erase current line
    snip.buffer[snip.line] = ""
    line_content = "#"*level + " ${1:Section Name} " + "#"*level
    line_content += '\n\n$0'

    snip.expand_anon(line_content)
endglobal


post_jump "gen_header(snip)"
snippet "h([1-6])" "Markdown header" br
`!p snip.rv = match.group(1)`
endsnippet

In the post jump code, snip.buffer is the object representing the current buffer and is indexable. snip.line is the line in which the snippet expand.

snip.expand_anon() will accept the snippet content and expand it, just like you have typed it in the snippet body.

References#