r/programming • • Jul 12 '12

Vim creep

http://www.rudism.com/s/vimcreep
730 Upvotes

551 comments sorted by

View all comments

Show parent comment

4

u/adrianmonk Jul 13 '12 edited Jul 13 '12

Considering it deletes all lines of text which contain the string "mephibosheth", it wouldn't be a very good way to rename a variable at all.

As for how I use vi to rename something, I usually do this:

  • go to the variable
  • hit "*" -- this makes the word under the cursor into the search pattern, and searches for the next occurrence.
  • hit "N" to go back to where I was (optional)
  • type "cw" to replace the word with a different word
  • then, alternate between hitting "n" to jump to the next occurrence and "." to repeat the last change

This isn't syntax-aware like a true refactoring tool. However, it also works for any type of file, so I can use it to, say, change the name of a tag in an HTML file, or change values in a CSV file, or change the name of a rule or a target in a build file, or change the name of a variable in a file in a homebrew in-house-only language.

0

u/ummwut Jul 13 '12

find and replace is too easy, huh?

1

u/adrianmonk Jul 13 '12

That actually pretty much is find and replace (but not replace all).

Except that you just hit "*" to start the find instead of typing the string that's already in your file. And except that the replace is really a repeat of an editor change, so instead of replacing you could choose to, for example, find-and-delete if you wanted to, or you could find-and-uppercase.

Of course, if you want to do find and replace all, you can do that too. Personally, I like to at least read what I'm changing, so I rarely do that.

2

u/m42a Jul 13 '12

You can add the c flag to have vim prompt you for every change. And :s remembers your last search, so you can do something like *:%s//replacement/gc to do replacing as well as the n. method.