implicit substitution in (neo)vim
(Neo)Vim search and replace supports using whatever pattern was last used when no pattern is specified. It's simple but powerful and I had no idea it existed.
That is, T383838:s//replacment/g (or T383838:%s//replacement/g) will replace all occurrences on the current line (or in the current document) of the previously searched (and likely still highlighted) text with the provided replacement text.
It never occurred to me to try this or look for this! Most recently, I wanted to change the link text in a reference link, but I wanted to do it with fewer keystrokes and with certainty I'd only be replacing the thing I meant to replace.
The Steps
Here's the basic recipe where I find it most useful:
1. Enter visual mode
2. Make a selection
3. Use T383838* to search on the selection
4. Use the substitute command and omit the search pattern
An example
Here's an example:
T282828
Here is my [link] that should not replace link.
[Te6edf3link]: Te6edf3link.mu "A link to elsewhere"
At the opening T383838[ of the T383838[link] I hit T383838v% so that it would go into visual mode and then select the brackets and everything inside of them. The result was just a visual mode selection of T383838[link] with nothing in the search buffer yet. Note, that if I had done T383838vi[ it would not include the T383838[. Next, I hit T383838* to do a search. This left visual mode and searched on the selection. With my editor settings it highlighted every occurrence, too, so I could see what (two occurrences) I'd be replacing. Next, I typed T383838:%s//[better link]/g and hit enter, all the highlighted occurrences of (the two) T383838[link]s were then replaced with T383838[better link].
The result:
T282828
Te6edf3Here Te6edf3is Te6edf3my Tb4b4b4[Te6edf3better Te6edf3linkTb4b4b4] Te6edf3that Te6edf3should Te6edf3not Te6edf3replace Te6edf3linkTb4b4b4.
Tb4b4b4[Te6edf3better Te6edf3linkTb4b4b4]Tff7b72: Te6edf3linkTb4b4b4.Te6edf3mu Ta5d6ff"Ta5d6ffA link to elsewhereTa5d6ff"
Bonus Round - Scope Matters
This will probably be a bit harder to follow, but the same action can be performed on a range. That is, T383838:'<',>s//replacement can be used to limit the scope of the replacement to a selected range.
For example, the following document:
T282828
Tc9d1d9# Top
[a] link to preserve.
[Te6edf3a]: Te6edf3first.mu
Tc9d1d9# Bottom
[a] link to replace.
[Te6edf3a]: Te6edf3second.mu
This document contains an error in that the same reference is used for two different links to two different documents. If the user moves to the first T383838[a] and does a search such that all the T383838[a] are highlighted they might notice the error and decide to fix the second one. So, after T383838[a] has been searched and higlighted the user could move to T383838# Bottom and enter visual mode with T383838v and select a range that includes the second occurrences of T383838[a]. If the user then, without breaking the selection mode, types T383838: then (Neo)Vim will automatically start a range based action with T383838:'<',> thta the user can then finish by typing in T383838s//[b]/g so the prompt string reads as T383838:'<',>s//[b]/g and then hit enter. The result will be only the content under T383838# Bottom will be changed. That is, the document will now look like this:
T282828
Tc9d1d9# Top
[a] link to preserve.
[Te6edf3a]: Te6edf3first.mu
Tc9d1d9# Bottom
[b] link to replace.
[Te6edf3b]: Te6edf3second.mu
Closing remarks
Obviously the implicit search pattern has other uses than the two limited examples I provided, but these examples are things I do commonly, so they're the clearest and most recent examples I had to illustrate it. It's yet another example of some simple and probably commonly used thing that I had no idea existed or was so easy, so I figured I'd record it in case I forget or in case someone else stumbles over this and finds it helpful.
Tags: vim, neovim, index
Tags
Navigation