Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 8
This is the 8th 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 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 5: https://jdhao.github.io/2019/11/11/nifty_nvim_techniques_s5/
- 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/
Use Neovim as man pager
The default pager used by
man command on *nix lacks syntax highlighting and
is not good for reading, searching. Why not turn nvim into the man pager? Just
add the following setting to your shell config file:
if [[ "$(command -v nvim)" ]]; then export EDITOR='nvim' export MANPAGER='nvim +Man!' export MANWIDTH=999 fi
Close other windows quickly?
When we are in a certain window, we may want to close all other windows. We may
go to the other windows and close them with
:quit. It is a bit cumbersome.
:only command is a much nicer way. It will close all the other windows
except the one we are in. There is also an equivalent shortcut:
<C-W> o (that
Ctrl-W, followed by
Execute a macro in several lines.
Macro is a powerful way to edit texts with similar structures. To execute a
macro on several lines, we can use a line range if the lines are continuous.
For example, execute macro
a for line 10 to 15, use:
Or we can visually select the lines, and run the following command (note that
if you select these lines, and then press
:, Nvim will insert
To execute a macro only on lines matching a certain pattern, run the following command:
Copy URL under cursor into a register?
We can use
expand() function get the URL under cursor (see
:h <cfile>). To copy the URL to unnamed register, use the following command:
let @" = expand('<cfile>')
The above method is not perfect, since
expand('<cfile>') will also give you
results even if your cursor is on a normal words (non-URL).
A more sophisticated method would be using actual URL patterns and search the
current line to get a valid URL. A good URL pattern is provided by plugin
highlighturl#default_pattern() method. With this knowledge, here is a more
error-proof approach to get the current URL:
let @" = matchstr(getline('.'), highlighturl#default_pattern())
Get diff between two buffers or files
If we have two different versions of the same file and we want to find the differences between them, how do we do it inside Neovim?
Suppose the two files are
manual-v2.md, here is how to
compare them inside Neovim.
If you haven’t start Nvim, you can run the following command:
nvim -d manual-v1.md manual-v2.md
This will start nvim in diff mode.
If you are already inside Neovim, first open
manual-v2.md in a vertical split window (
Finally, run the following command to start comparing:
Of course, you can use a horizontal split window, but vertical split window is better for comparing the two files, IMO. ↩︎
License CC BY-NC-ND 4.0