Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 8
source link: https://jdhao.github.io/2020/11/11/nifty_nvim_techniques_s8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 8
This post continues my previous post on nifty Nvim/Vim techniques.
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
See also :h man.vim
.
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.
The :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
is, Ctrl-W
, followed by o
).
Execute a macro in several lines.
Macros are 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
from line 10 to 15, use:
10,15normal! @a
Or we can visually select the lines we want to execute the macro, and run the
following command (note that if you select these lines, and then press :
,
Nvim will insert '<,'>
automatically):
:'<,'>normal! @a
To only execute a macro on lines matching a certain pattern, use the following command:
:g/pattern/normal! @a
Copy URL under cursor into a register?
We can use <cfile>
with 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 use actual URL patterns and search the
current line to get a valid URL. A good URL pattern is provided by plugin
vim-highlighturl via
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-v1.md
, 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 the diff mode.
If you are already inside Neovim, first open manual-v1.md
(:e manual-v1.md
),
then open manual-v2.md
in a vertical split window (:vs manual-v2.md
)1.
Finally, run the following command to start comparing:
:windo diffthis
Of course, you can use a horizontal split window, but vertical split window is better for comparing the two files, IMO. ↩︎
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK