So often if I have a code expression like points.length - 1
and I want to replace it with a variable (for example, named target_idx
then I'll define the variable:
int target_idx = points.length - 1;
But now I want to go and replace all instances of points.length - 1
with target_idx
. I'm aware I could do :%s/points.length - 1/target_idx/g
, but I'm not too confident in my regex and don't want to mistakenly replace points.length - 11
or points+length - 1
by mistake. (I'm also aware of very magic searching, but also am not super confident about all the subtleties of what it does/doesn't consider special characters)
So what I want is to be able to search for instances matching points.length - 1
via /points.length - 1
and then go through the search results with n
, and then replace the text with target_idx
.
But because the searched-for text could have spaces or other characters, I can't simply change to the end of the Word cE
since this would only change points.length
in my example.
Is there a text motion that goes to the end of the searched-for text?
So I could go
n
c<special text motion here>
target_idx
I'm using neovim with CoC installed, so am open to more in-depth ideas with plugins or language server replacements, but this feels like it should be a simple text motion that I just don't know of.
I guess you can go for gn
(:help gn
for details). (gN
searches backward.)
On the other hand, you can give :s/…/…/
the flag c
so that it asks you to confirm every substitution (:help :s_flags
for details).
Besides, you could most likely take advantage of .
to make steps 2 and 3 in one keystroke (:help .
for details). In your specific example, you'd do this the first time,
/points\.length - 1
Entercf1target_idx
Escape
and then just keep going to next match via n and applying the change via ..
The :s
solution with c
flag I mentioned above is faster, as you only press y/n instead of n/n..