Imagine that we have a variable named myVar
, and one of our teammates was reading our code and tell us that it’s not descriptive at all but we’ve used it in numerous places throughout our code, and changing its name step by step isn’t an easy task. This is where the “gn
” motion snippet becomes incredibly useful. Let’s explore how we can use it.
To begin, place the cursor above the word we want to rename in this case: myVar
.
Then, press *
to search for all instances of the word in the document and start working on it. This highlights all occurrences of the word, enabling quick navigation between them.
Next, press shift + n
to move to the previous occurrence of the word in our document. Here, press g + n
to operate on this occurrence, which is where the real magic happens. g + n
allows for actions such as editing or renaming.
Now, press c
to change the actual name and input the new one for our variable (this time, try that be something easy to understand), and then press Esc
to finish editing that word. The Escape key exits the renaming mode, returning you to normal mode after inputting the new variable name.
Pressing n
moves the cursor to the next occurrence of the word in the document, allowing for seamless continuation of the renaming process.
Finally, .
repeats the last change made in the text editor. After renaming one occurrence of the word, pressing .
applies the same change to the next occurrence, accelerating the renaming process.
Buuut there’s another useful way to do this and is with the substitute command
, let’s check it out.
The first step is to exit any active mode by pressing the Esc
key. Then, use the substitute command :s
. As our intention is to rename all occurrences of our variable myVar
, follow these steps:
So the final command will looks like this:
And if you’re that curious as me, you might be wondering, What’s the difference? or When should I use one or the other? Well, let me tell you with an example:
Suppose you have a variable named i
for indexing purposes. In such cases, the “gn
” motion is the preferred method for renaming. And this is because while the substitute command :s
would alter all instances of i
, including occurrences within words like function
, int
, if
, void
, size
, and others, “gn
” provides finer control. By navigating with the n
key and applying the changes with .
, you can selectively modify specific words.
So in conclusion use :s
for terms that won’t disrupt your code or when you want to modify all occurrences deliberately. And opt for the “gn
” motion in scenarios with numerous occurrences, allowing you to selectively choose which ones to modify.
Now the next time you find yourself needing to rename variables or make widespread changes in your code you can do it faster and efficiently.
Thank you for read and I really hope this helps you to improve in Neovim, shared if you know someone else can be useful too, Blessings!.
Useful links:
Discover more from Coursity
Subscribe to get the latest posts sent to your email.