Skip to main content
  1. Posts/

Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 8

··566 words·3 mins·
Table of Contents

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.

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.

Ref:

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).

Ref:

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:

10,15normal! @a

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 '<,'> automatically):

:'<,'>normal! @a

To execute a macro only on lines matching a certain pattern, run the following command:

:g/pattern/normal! @a

Ref:

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 using 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())

Ref:

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 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

Ref:


  1. Of course, you can use a horizontal split window, but vertical split window is better for comparing the two files, IMO. ↩︎

Related

Using Diffs in Vim
··235 words·2 mins
Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 9
··684 words·4 mins
Migrating from Packer.nvim to Lazy.nvim
··651 words·4 mins