开发者

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

开发者 https://www.devze.com 2022-12-21 21:43 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed last year.

Improve this question

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


Try sed. For example:

sed -i -e 's/foo/bar/g' myfile.txt


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.


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.


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


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

perl -p -i".backup" -e "s/foo/bar/g" test.txt


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.


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/


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


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.


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)


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消