views:

327

answers:

16

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

+6  A: 

Try sed. For example:

sed -i -e 's/foo/bar/g' myfile.txt
Konrad Garus
Also: find . -name "*.php" | xargs sed -i -e 's/foo/bar/g'
Konrad Garus
curses ... you beat me to it! ;)
Reed Debaets
A: 

look into sed ... powerful command line tool that should accomplish most of what you're looking for ... its supports regex, so your find/replace is quite easy. (man sed)

Reed Debaets
+2  A: 

Notepad++ allows me to search and replace in an entire folder (and subfolders), with regex support.

alexandrul
+1  A: 

There are a number of tools that you can use to make things easier. Firstly, to search all the files in the project from vim you can use :grep like so:

:grep 'Function1' myproject/

This essentially runs a grep and lets you quickly jump from/to locations where it has been found.

Ctags is a tool that finds declarations in your code and then allows vim to jump to these declarations. To do this, run ctags and then place your cursor over a function call and then use Ctrl-]. Here is a link with some more ctags information:

http://www.davedevelopment.co.uk/2006/03/13/vim-ctags-and-php-5/

ar
A: 

Notepad++ has support for syntax highlighting in many languages and supports find and replace across all open files with regex and basic \n \r \t support.

Greg Bray
+1  A: 

I found this article, which may help you.

dbrien
A: 

The command grep -rn "search terms" * will search for the specified terms in all files (including those in sub-directories) and will return matching lines including file name and line number. Armed with this info, it is easy to jump to a particular file/line in VIM.

As was mentioned before, sed is extremely powerful for doing find-and-replace.

You can run both of these tools from inside VIM as well.

bta
A: 

Some developers I currently work with swear by Textpad. It has a UI and also supports using regex's -- everything you're looking for and more.

Dave M
Yeah! Go Textpad!
dwo
+2  A: 

You can use perl in command prompt to replace text in files.

perl -p -i".backup" -e "s/foo/bar/g" test.txt
chappar
Works perfectly !
ereOn
`perl -pi -e 's/foo/bar/g' file` . No need for the `".backup"` on *nix systems.
Zaid
+5  A: 

I don't get why you can't do this with VIM.

Just Find

/Foo

Highlights all instances of Foo in the file and you can do what you want.

Blindly Replace

:% s/Foo/Bar/g

Obviously this is just the tip of the iceberg. You have lots of flexibility of the scope of your search and full regex support for your term. It might not work exactly like your former editor, but I think your original 'use one editor' idea is a valid one.

Andrew Cowenhoven
Vim can do search and replace really well, but not across multiple files. sed is probably the tool of choice for that.
Jay
You may have to do `:set hlsearch` to turn on the all-instance highlighting.
Dennis Williamson
@Jay:VIM natively supports several flavors of grep which enable multi-file searching. See http://vim.wikia.com/wiki/Find_in_files_within_Vim
Andrew Cowenhoven
@Jay: Vim does support search and replace accross several files very well using args and argdo. E.g.: :args *.[ch] :argdo %s/TODO/DONE_NOW/ge | updateThis would replace TODO by DONE_NOW in all *.c *.h files. See also :bufdo and :windo
Habi
Thanks! I cut my teeth on vi - vim seems to have added some nice features.
Jay
If you want to do a find-and-replace one at a time (i.e., not blindly), use the c modifider: `:%s/Foo/Bar/gc`
Platinum Azure
A: 

A very useful search tool is ack. (Ubuntu refers to it as "ack-grep" in the repositories and man pages.)

The short version of what it does is a combination of find and grep that's more powerful and intelligent than that pair.

Dennis Williamson
+5  A: 

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.

Dave Kirby
YES - the best part is that once it finds all the matches, **you can type :copen to see them all in a window at the bottom, then highlight one and hit enter to jump to that file.** (You can also cycle between matches in multiple files using :cn and :cp)
Nathan Long
Also - the results list shows line numbers and code snippets, just as I requested. Move from search results to editing window just like any two windows in vim (like when using :split) - ctl-w and a direction. I am so pumped about this!
Nathan Long
+2  A: 

Since you are looking for a GUI tool, I generally use the following 2 tools. Both of them have great functionality including wildcat matching, regex, filetype filter etc. Both of them displays good useful information about the hit in files like filename/lines.

  1. Visual Studio: fast yet powerful. I uses it if the file number is huge (say, tens of thousands...)

  2. pspad: lightweight. And a good feature about find/replace for pspad is that it will organize hits in different files in a tree hierarchy, which is very clear.

Findekano
why downvote this comment?
Findekano
+1  A: 

I don't know if it is an option for you, but if you load all your files into vim with vim *.php than you can

:set hidden

:argdo %s/foo/bar/g => will execute the substitue command in all opened buffers

:wall => will write all opened buffers

Or instead of loading all your files into vim try :help vimgrep and a cominbation of :help argdo and :help argadd

vbd
+1  A: 

For Windows, I think that grepWin is hard to beat -- a GUI to a powerful and flexible grep tool for Windows. It searches, and replaces, knows about regular expressions, that sort of stuff.

High Performance Mark
For my current purposes, this works great.
Nathan Long
+1  A: 

On a MAC you will need to pass something to the -i parameter. An empty string '' should be fine.

Maher Gamal