Mastering variable renaming with gn motion
Tags: vim, webdev, tutorial, opensource
Published: 2024-04-18 10:01:41

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712255500078/b8b5588d-8328-4879-a23d-c7b82d2e6ab5.png)

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712256223406/42b1f986-fedc-45f8-8e2b-6a9e7bb42e8c.png)

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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712258414240/fc3cd229-f6fc-45a2-929b-a084ed905799.jpeg)

So the final command will looks like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712258447301/ae188d18-3dbb-43e3-9011-c830ff99a0b1.png)

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:  
[Search and replace](https://vim.fandom.com/wiki/Search_and_replace)