0

For example, if I have the following:

MY_VAR = "hello world"
print "the var %s" % MY_VAR

If I position the cursor on MY_VAR and press *, Vim highlights both matches. Suppose I want to rename this to HELLO_WORLD_VAR, how might I achieve this?

3

Since you pressed *, your last search was for MY_VAR, i.e. equivalent to /MY_VAR. If you don't specify a search pattern in a substitution, the last search pattern is used, which is MY_VAR in your case. So you can simply use :%s//HELLO_WORLD_VAR/g to do the renaming.

Note that I used the g flag to indicate that all occurences should be replaced, not only the first on any line. It's not neccessary in your case, but probably makes sense in another context.

  • Related tip: I like to use set gdefault in my vimrc. This will add the /g flag by default, since this is what I want 95% of the time (you can "undo" the /g flag by adding it). – Martin Tournoij May 28 '15 at 11:22
  • Great answer! I agree with @Carpetsmoker that gdefault is useful. Until you forget it's on and do /g :) – squarefrog May 28 '15 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.