Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Great tools to find and replace in files? [closed]

I'm switching from a Windows PHP-specific editor to VIM, on the philosophy of "use one editor for everything and learn it really well."

However, one feature I liked in my PHP editor was its "find and replace" capability. I could approach things two ways:

  • Just find. Search all files in a project for a string, see all the occurrences listed, and click to dive into that file at that line.
  • Blindly replace all occurrences of "foo" with "bar".

And of course I could use the GUI to say what types of files, whether to look in subfolders, whether it was case sensitive, etc.

I'm trying to approximate this ability now, and trying to piece it together with bash is pretty tedious. Doable, but tedious.

Does anybody know any great tools for things like this, for Linux and/or Windows? (I would really prefer a GUI if possible.) Or failing that, a bash script that does the job well? (If it would list file names and line numbers and show code snippets, that would be great.)

like image 717
Nathan Long Avatar asked Mar 02 '10 17:03

Nathan Long


2 Answers

Try sed. For example:

sed -i -e 's/foo/bar/g' myfile.txt
like image 87
Konrad Garus Avatar answered Oct 19 '22 03:10

Konrad Garus


Vim has multi-file search built in using the command :vimgrep (or :grep to use an external grep program - this is the only option prior to Vim 7).

:vimgrep will search through files for a regex and load a list of matches into a buffer - you can then either navigate the list of results visually in the buffer or with the :cnext and :cprev commands. It also supports searching through directory trees with the ** wildcard. e.g.

:vimgrep "^Foo.*Bar" **/*.txt

to search for lines starting with Foo and containing Bar in any .txt file under the current directory.

:vimgrep uses the 'quickfix' buffer to store its results. There is also :lvimgrep which uses a local buffer that is specific to the window you are using.

Vim does not support multi-file replace out of the box, but there are plugins that will do that too on vim.org.

like image 30
Dave Kirby Avatar answered Oct 19 '22 03:10

Dave Kirby