Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Live substitution and yank highlighting

#73

Run time:

This video covers a couple of small but delightful Neovim features. You’ll see how to make Neovim show a live preview of how the substitute command will change our document. And you’ll find out how to make the yank operation highlight the range of text that it copied.

Shownotes

Live substitution

Vim’s :substitute command only modifies the document when you execute the command, by pressing <Enter>. In Neovim, you can make the :substitute command change the document live by setting the 'inccommand' option:

:set inccommand=nosplit

Now, you’ll see the document updating in real-time as you type out your substitute command. If you press the <Esc> key, you can abort the substitute command and your document will instantly revert to how it looked before you started typing the command. If you press the <Enter> key, your substitute command will apply and the changes to the document will stick.

Currently, the 'inccommand' option only influences the behaviour of the substitute command. In future, it may become possible to get live feedback on other commands, such as changing visual block mode, :global and :normal commands.

Yank highlighting

Vim doesn’t show any visual feedback when you use the yank command. The vim-highlightedyank plugin changes this, by applying a temporary highlight to the region of text that was yanked.

The highlightedyank plugin works both in Neovim and in Vim (since version 8.0.1394). It makes use of the TextYankPost autocommand. If you want to use the plugin in an older version of Vim you have to remap the y command, because that autocommand is not available.

At first, I didn’t think I needed to use the highlightedyank plugin. I was accustomed to the behaviour of the yank command, and I didn’t feel as though I needed any visual feedback to help me understand how the command worked. Even so, I installed the highlightedyank plugin out of curiosity. I was surprised to find how much I enjoyed that extra visual feedback. I’m a convert!

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..