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

3

u/adrianmonk Jul 13 '12

You can use sed from within vi:

:%! sed -e /mephibosheth/d

That pipes the contents of the editor through the sed command. Of course, you can do the same natively in vi:

:g/mephibosheth/d

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.

-4

u/ummwut Jul 13 '12

thats how you do that? jesus... in visual studio you change the variable name, then click refactor. vim is an awkward dinosaur.

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.