O'Reilly logo

Mastering Vim by Ruslan Osipov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Renaming a variable, a method, or a class

Oftentimes, we rename things when refactoring, and these changes need to be reflected throughout the code base. However, simple search and replace often won't cut it, since you'll risk accidentally renaming unrelated things.

For example, let's try renaming our Dog class as Pitbull. Since we need to carry this out in multiple files, we'll use arglist:

:arg **/*.py

Now, move your cursor over the class name you'd like to rename (Dog), and enter the following(here, \<[Ctrl + r, Ctrl + w]\> signifies pressing Ctrl + r followed by Ctrl w and not typing in square brackets):

:argdo %s/\<[Ctrl + r, Ctrl + w]\>/Pitbull/gec | update

Once you run it, you'll be prompted for every match:

Press y to approve ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required