I want to search and replace some text in a large set of files excluding some instances. For each line, I want a prompt asking me if I need to replace that line or not.
Something similar to vim's :%s/from/to/gc (with the c to prompt for confirmation), but across a set of folders.
Is there some good command line tool or script that can be used?
|
|
||||
|
Why not use vim? Open all files in vim
Or open only relevant files (as suggested by Caleb)
And do then run the replace in all buffers
You can also do it with |
|||||||||||
|
|
You can do something crude with a small Perl script which is instructed to perform replacements line by line (
Possible improvements would be to color parts of the prompt and support things like “replace all occurences in the current file”. Separately prompting for each occurrence on a line would be harder. Second part, matching the files. if there aren't too many files involved and you're running zsh, you can match all the files in the current directory and its subdirectories recursively:
If your shell is bash ≥4, you can run
If you need finer control over which files you're replacing, or your shell doesn't have the recursive directory matching construct
That being said, I'd stick to an interactive editor if you need interaction. Gert has shown a way to to this in Vim, though it requires opening all the files that you want to search through, which may be a problem if there are a lot. In Emacs, here's how you can do this:
|
||||
|
|
s/from/to/gwith a formatting glitch after it, rather thans/from/to/gcwith emphasis on thecas you'd attempted to write (you can't do that with Markdown, you could do it with<code>and<strong>HTML tags). – Gilles May 3 '11 at 14:39