by seanhunter · 2y ago · 0 comments
- perl -pi -e 's/foo/bar/g' files
"-pi" means "in place edit" so it will change the files in place. If you have a purely mechanical change like he's doing here it's a very reasonable choice. If you're not as much of a cowboy as I am, you can specify a suffix and it will back the files up, so something like
perl -p -i.bak -e 's/db/database/g'
pyFor example then all your original '.py' files will be copied to '.py.bak' and the new renamed versions will be '.py'
For vim users (I know emacs has the same thing but I don't remember the exact invocation because it has been >20years since I used emacs as my main editor) it's worth knowing the "global" command. So you can execute a particular command only on lines that match some regex. So say you want to delete all the lines which mention cheese
:%g/cheese/d
Say you want to replace "db" with "database" but only on lines which start with "def"
:%g/^def/s/db/database/
OK cool. Now if you go 'vim *py' you can do ":argdo g/^def/s/db/database/ | update" and it will perform that global command across all the files in the arg list and save the ones which have changed.
But in this specific situation it was tricky to handle situations with things spanning over multiple lines + preventing accidental renames.
https://blog.moertel.com/posts/2013-02-18-git-second-order-d...
[1] like that's even possible in this situation
> every instance of a pytest fixture
Although it's probably good enough for 99% of the use cases, and any extra accidental renames could be reverted when you look at the diff.
Maybe it could be covered with a multi line regex using `\_.`