Nifty Nvim Techniques That Make My Life Easier -- Series 5
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.
- Series 11: https://jdhao.github.io/2021/11/22/nifty_nvim_techniques_s11/
- Series 10: https://jdhao.github.io/2021/06/17/nifty_nvim_techniques_s10/
- Series 9: https://jdhao.github.io/2021/01/07/nifty_nvim_techniques_s9/
- Series 8: https://jdhao.github.io/2020/11/11/nifty_nvim_techniques_s8/
- Series 7: https://jdhao.github.io/2020/09/22/nifty_nvim_techniques_s7/
- Series 6: https://jdhao.github.io/2019/12/21/nifty_nvim_techniques_s6/
- Series 4: https://jdhao.github.io/2019/09/17/nifty_nvim_techniques_s4/
- Series 3: https://jdhao.github.io/2019/05/14/nifty_nvim_techniques_s3/
- Series 2: https://jdhao.github.io/2019/04/17/nifty_nvim_techniques_s2/
- Series 1: https://jdhao.github.io/2019/03/28/nifty_nvim_techniques_s1/
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
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.
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
more info, see
:h search-range and
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
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
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
: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.
- Why does the pattern in matchadd() is not matched?
- Difference between single quote and double quote in Vim.
How to get the path of currently sourced file?
When Vim is sourcing a file, you can use
<sfile> inside function
to get the path of the script. To get other info about the sourced file, use
" absolute path of currently sourced file echo expand('<sfile>:p') " directory containing currently sourced file echo expand('<sfile>:p:h')
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.
You can show all the available highlight groups with
License CC BY-NC-ND 4.0