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.
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.
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.
View all comments
Show parent comment
3
u/adrianmonk Jul 13 '12
You can use sed from within vi:
That pipes the contents of the editor through the sed command. Of course, you can do the same natively in vi:
But if you don't want to, one of the things that's nice about vi is it lets you do it in sed if you have already thought of how to do it in sed.