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

推荐订阅源

IT之家
IT之家
Last Week in AI
Last Week in AI
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
宝玉的分享
宝玉的分享
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 司徒正美
罗磊的独立博客
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
小众软件
小众软件
Jina AI
Jina AI

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
Nifty Nvim/Vim Techniques That Make My Life Easier -- Ser...
2021-11-22 · via jdhao's digital space

This is the 11th post of my post series on nifty Nvim/Vim techniques that will make my editing experience easier.

Click here to check other posts in this series.

Move lines matching a pattern together?#

Use command :g/pattern/normal ddGp.

Here we use the :global command. It will apply the command :normal ddGp (which is deleting current line and paste it at the bottom) for each line matching pattern.

Another way to do this is to use :move together with :global command:

:[range]m[ove] {address}          *:m* *:mo* *:move* *E134*
          Move the lines given by [range] to below the line
          given by {address}.

Now the complete command to do this task is :g/pattern/m$ ($ means the last line of a buffer).

At the end, all lines matching pattern will be moved to the end of the buffer.

Ref:

Show trailing whitespace#

We need to first define a highlight group for it and then match a pattern to use this highlight group:

highlight TrailingWhitespace ctermbg=red guibg=red

call matchadd("TrailingWhitespace", '\v\s+$')

We’d better use single quote pattern to avoid backslash hell, otherwise, escaping the pattern will be a pain. If we use double quote, \s won’t work, we have to escape backslash, see also :h literal-string.

Flip a 01 string quickly#

To flip a 01 string (i.e., turn 0 to 1 and turn 1 to 0), we can use sub-replace-expression which is really powerful:

:s/\v(0|1)/\=submatch(0)==0?1:0/g

Or we can also use this (source here):

:s/\v(0|1)\=1-submatch(0)/g

Ref:

Search lines not starting with a pattern#

This is a perfect example where using look around regex solves it neatly. For example, to search for lines not starting with foo, use the following search expression:

A teardown of this expression:

  • \v: magic regex in vim, see also :h \v. We use \v to simplify writing regex in vim and make it more like PCRE.
  • ^: start of a line
  • (foo)@!: @! is the vim syntax for negative lookahead. This expression means that the previous expression should not be followed by foo, we group foo in Vim using parentheses.

Ref: