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

推荐订阅源

C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
A
About on SuperTechFans
J
Java Code Geeks
量子位
博客园 - 三生石上(FineUI控件)
博客园 - Franky
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

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 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 Mintty Tips and Configurations
Nifty Nvim Techniques That Make My Life Easier -- Series 5
2019-11-11 · via jdhao's digital space

This is the 5th 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.

How do I source other files in the same directory as my config?#

As my config becomes longer and longer, I decided to split it into several Vim scripts. I can then source those files in my init.vim. Initially, I wrote something like source xxxx.vim. To my surprise, Nvim complained that it could not find those Vim script. After searching on the internet, I find that source command works relatively to your current work directory. In order to source certain Vim script in the same directory as init.vim, we have to use their absolute paths. Here is what I come up with:

let g:nvim_config_root = stdpath('config')
let g:config_file_list = ['variables.vim',
\ 'options.vim',
\ 'autocommands.vim',
\ 'mappings.vim',
\ 'plugins.vim',
\ 'ui.vim'
\ ]

for f in g:config_file_list
    execute 'source ' . g:nvim_config_root . '/' . f
endfor

In the above settings, we use the stdpath() function to get the directory of nvim config directory, which is rather convenient and works across platforms. Then we loop the files we want to source and source them one by one.

References#

How to search a pattern only in a line range#

Sometimes, we only want to search in a range of lines, i.e., search from line n to line m. The syntax can be best explained by giving a concrete example. Suppose we want to search for the word search from line 10 to line 20, we can use the following search pattern:

The start line is given by \%>10l and the end line is given by \%<20l. For more info, see :h search-range and :h /\%l.

References#

Add highlight to a pattern#

To highlight some patterns, we can use the :match command. For example, in order to highlight trailing white spaces, we may use the following command:

:match WarningMsg /\s\+$/

The 1st argument to :match is a valid highlight group1. In the 2nd argument, the highlight pattern is inside //. For more info, see :h match-highlight.

The problem is that we can only highlight one pattern using this command. This means that if we want to highlight another pattern with :match command, only one pattern get highlighted. Vim provides additional :2match and :3match command to some avail. So in total, you can highlight three different patterns using this command.

Fortunately, Vim also provides the matchadd() function which works similarly to :match, except that there is no limit on the number of patterns to match. For example, to match both trailing white spaces and leading tab characters, use the following setting:

call matchadd('Warnings', '\s\+$')
call matchadd('Warnings', '^\t\+')

In the above settings, it is important that you quote the pattern to match with single quote. If you use double quotes, those backslashes will get translated so that the regex pattern will not right.

References#

How to get the path of currently sourced file?#

When Vim is sourcing a file, you can use <sfile> inside function expand() to get the path of the script. To get other info about the sourced file, use filename-modifiers (see :h filename-modifiers).

" absolute path of currently sourced file
echo expand('<sfile>:p')

" directory containing currently sourced file
echo expand('<sfile>:p:h')

References#

Turn tabs to spaces in a buffer quickly#

I do not like tabs in my source file so I have set up the following settings in my config:

set tabstop=4
set softtabstop=4
set expandtabs
set shiftwidth=4

This setting works great when I am editing source code. However, for existing code using tabs, Vim/Nvim will not convert tabs into spaces automatically. Usually, I just replace tabs with four spaces manually: :s/\t/ /g. But it is tedious to type all this.

There is a quicker way to turn tabs into spaces using the :retab command. It will turn tabs into 4 spaces if you use the above settings.

References#