Live data from Hacker News

Go Replace - simple and fast search and replace tool for command line

solovyov.net

11–20 of 64 posts

Re: Go Replace - simple and fast search and replace tool for command line

#11
post #10

For x86 binaries: go get github.com/piranha/goreplace go install github.com/piranha/goreplace is another way to get it if you have GO and GOPATH set up. I personally aliased it to 'gor' in my zshrc as the git plugin already occupied the 'gr' (short for git remote).

Ha-ha, I've got an issue asking to add some notes about conflict with oh-my-zsh alias. :) I hastily suggested `gp`, but it's already taken by same plugin, so I changed it to `gor` there, thanks for idea.

Re: Go Replace - simple and fast search and replace tool for command line

#12
post #9

I really like Facebook's codemod.py for stuff like that: https://github.com/facebook/codemod It has something like automatic and manual modes, so you can check (and fix) every diff during the replacement

Well you see... It's in Python, which breaks the deal for me (I despise slow startup). But interactive mode sounds interesting, that's something I'll consider implementing.

Re: Go Replace - simple and fast search and replace tool for command line

#13

    find ~/my/docs/ -type f -name '*.txt' -exec sed -i.bak 's/inheritance/composition/g' {} +
This will search all files ending in `.txt` and will exec `sed 's/inheritance/composition/g'` on it. Sed modifies files in-place and saves a backup file with .bak extension (-i.bak) and is called the minimum necessary times (-exec +).

My best advice to someone using the command line is to learn `find` + `xargs` or, even better, `find` + `parallel`.

Re: Go Replace - simple and fast search and replace tool for command line

#14

find ~/my/docs/ -type f -name '*.txt' -exec sed -i.bak 's/inheritance/composition/g' {} + This will search all files ending in `.txt` and will exec `sed 's/inheritance/composition/g'` on it. Sed modifies files in-place and saves a backup file with .bak extension (-i.bak) and is called the minimum necessary times (-exec +). My best advice to someone using the command line is to learn `find` + `xargs` or, even better,…

Compare with "gr what-is-it -r here-you-go". I do not have to remember all that argument hell.

Re: Go Replace - simple and fast search and replace tool for command line

#15
post #6

Wow, it's super fast AND the output looks great. Good job, piranha! Copied to /usr/bin, I will use this a lot.

If you're the only user on your box, you can just create ~/bin and some distributions will automatically add it to your personal PATH. That way you don't lose track of what you've copied into /usr/bin, leaving it to be maintained by packages.

Re: Go Replace - simple and fast search and replace tool for command line

#16
Nice! svn support would be useful (ignore .svn directories) and the -r option is easy to remember, but perhaps not so much in line with the already messy standard Unix options (-r or -R for recursive operation).

Note that specifically for Go source code, there is "go fix -r" for syntax/context-aware code replacement (see "godoc fix").

Re: Go Replace - simple and fast search and replace tool for command line

#17

Nice! svn support would be useful (ignore .svn directories) and the -r option is easy to remember, but perhaps not so much in line with the already messy standard Unix options (-r or -R for recursive operation). Note that specifically for Go source code, there is "go fix -r" for syntax/context-aware code replacement (see "godoc fix").

In case of go fix `-r` does something absolutely unrelated.

To be honest, I was just lazy inventing a command line key and it's recursive by design, so it happened this way. Plus, as you say, it's easy to remember because of mnemonics.

I haven't touched svn repos in years, but yeah, that could be added. I know they exist, it just that I haven't felt the need to support them.

Re: Go Replace - simple and fast search and replace tool for command line

#18
post #9

I really like Facebook's codemod.py for stuff like that: https://github.com/facebook/codemod It has something like automatic and manual modes, so you can check (and fix) every diff during the replacement

I love this little gem. I can do in seconds what takes my colleagues an hour (they generally don't know about macros, extending their editors, regexes, etc). It showed me that solving text problems outside your text editor ends up benefiting more people.

Its a little bit different from Go Replace, giving the option to drop on your editor if the current replacement can't be appied directly (this is the great feature).

Re: Go Replace - simple and fast search and replace tool for command line

#19
This isn't working for me. I get:

    C:\Users\swah\docs>gr TEST
    panic: Given path should be anchored at /

    goroutine 1 [running]:
    main.NewIgnorer(0xc08005bcf0, 0x29, 0x0, 0x0, 0x140110, ...)
            /Users/piranha/dev/go/src/goreplace/ignore.go:32 +0xa4
    main.main()
            /Users/piranha/dev/go/src/goreplace/goreplace.go:62 +0x1b7

    goroutine 2 [runnable]:
[edit] New version is working!
Post reply on HN