Results 1 to 1 of 1
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Nov 2008
Improvement of "search-confirm-replace" via terminal
I'm frequently faced with the problem of replacing a string across an entire directory. For the mindless tasks, I've seen solutions ranging from general usage string manipulation tools (perl, sed) to something as specific as rpl.
However, because I'm a programmer, this sometimes involves replacements that cannot be done blindly. Asking the user for a confirmation on each replacement is viable here. And so I've compiled a small line that does this for you.
For the purposes of this demonstration, let's say I want to replace all occurrences within my current directory (a project that makes use of the 'automobile' variable which should be shortened to 'car').
I'd run this command:
vim -c "argdo %s/automobile/car/gc | w" `grep -lR automobile .`
At first when I looked into this problem I was looking for utilities that would show the current context of the line, displaying up to 80 characters of context around it. When I discovered the vim solution, however, I realised the convenience of being able to see the real context of word within the application scope, with multiple lines above and below. Now that I've gotten picky I'd sort of like to keep that functionality.
Is there a better way to do this? By better, I mean more efficient, simpler to write out (without making a shell script) or more verbose. Please post any ideas for improvement.