Interactive search and replace from shell

Viewed 4322

Search and replace over multiple files is difficult in my editor. There are plenty of tricks that can be done with find, xargs and sed/awk incluing search-and replace in multiple files. But somehow I couldn't find a way to make this interactive. Do you know a way to do that?

8 Answers

From jhvaras answer, I've made this bash command to quickly search and replace (to add to .bashrc):

replace () {
    if [ $# -lt 2 ]
    then
        echo "Recursive, interactive text replacement"
        echo "Usage: replace text replacement"
        return
    fi

    vim -u NONE -c ":execute ':argdo %s/$1/$2/gc | update' | :q" $(ag $1 -l)
}

It's used as follows:

~$ replace some_text some_new_text

It uses ag to search in advance, as it's probably faster than letting vim do the work, but you can probably substitute anything else you like. It also calls vim with no plugins for maximum speed, and after it has finished all substitutions it automatically quits and goes back to the shell.

you can try this tool for find and replace in intercative mode inner a shell

https://sourceforge.net/projects/isartool/

No X server is required, and if you want it's recursive in directory using the option -r.

Ciao

Related