Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Tree-sitter is really powerful, but it's worth people learning a few methods they prefer to use because there are going to be situations where one method works better than another. Things I have found useful in the past include

- 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' py

For 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.



Author here: I'm super familiar with this kind of find and replace syntax inside vim or with sed. Usually it works great!

But in this specific situation it was tricky to handle situations with things spanning over multiple lines + preventing accidental renames.


For those tricky situations, there's "sledgehammer and review" and the second-order git-diff trick:

https://blog.moertel.com/posts/2013-02-18-git-second-order-d...


I realise that and like the article. I was trying to convey in my response that devs should have these things in their toolkit not that you "did the wrong thing"[1] somehow by using treesitter for this.

[1] like that's even possible in this situation


About the cowboy comment, that's what version control is for. Just modify in place and then stage hunk by hunk with magit or git add -p.


I'd reach for argdo as well - but I don't think this covers his use case of:

> 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 `\_.`




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: