Vim search replace many files




















Further tune your search by using grep to get only the files that contain the string you want to change, such as by adding:. It interactively works with all of these files without the tedium of opening, saving, and closing each file, one at a time. Using backtick-expansion to pass our search to Vim opens up multiple buffers ready to go.

Do :h backtick-expansion in Vim for more. Now you can apply the Vim command :bufdo to all of these files and perform actions such as interactive search-and-replace:. The e will omit errors if the pattern is not found. The c option makes this interactive. I will show two ways how we can do search and replace across multiple files in Vim. But before, let's go over the fundamentals. Regex works too.

I will skip the regex details here, but you can read up more about it at :h substitute. Let me show you how we can use args. Let's assume our directory contains something like this:.

To capture index. Typing :args now will display. To go to next args, type :n[ext] and :prev[ious] to go to previous args list. Now that we have all args, we can perform our regular substitution. Recalling our substitution method, we will combine it with argdo. If you're baffled by argdo, you're not alone. EricJohnson: They're They're therefore pretty much the same as grep regular expressions. Is there a reason you have -i in the sed command?

The man page says that is for specifying an extension, but you don't appear to actually specify one. On Mac OS X the only sed command that worked for me was: find.

Show 3 more comments. EDIT: Use cfdo command instead of cdo to significantly reduce the amount of commands that will be run to accomplish this because cdo runs commands on each element while cfdo runs commands on each file Thanks to the recently added cdo command , you can now do this in two simple commands using whatever grep tool you have installed. If you want to save the changes in all files :cdo update cdo executes the given command to each term in the quickfix list, which your grep command populates.

Sid Sid 2, 17 17 silver badges 22 22 bronze badges. Plus you can add :cdo update to save the changes in all files. But it will pauses to warn not saved each time before switching to the next buffer when current buffer is modified.

Zhe thanks, I've added that to the answer; lfree I personally prefer confirming each change, but you can remove the c at the end of the second command just use g to have it search and replace without asking for confirmation — Sid.

Show 9 more comments. Eric Johnson Eric Johnson I like this answer because it works even with cygwin's line endings.

Perl is a bit more sane, and this is a really great one-liner for search and replace. Also try perldoc. Perl is extremely cross platform and I'm sure there is a way for you to not end up with mangled line endings. I specifically liked the perl version because it only modifies the lines it matches whereas sed will modify all lines, even if they don't match the regex.

What if the file's path contains spaces? The thing I hate about Greplace is that if the pattern is in the filename, Greplace fails, because you end up replacing it in the filename, too. Nothing unusual here after 6 years, but this plugin is severely outdated in comparison to some new answers github. I might check out the builtin solution by Sid: stackoverflow. Benoit Benoit It's not clear to me if you need to do this "sequence" every time.

I'm faster doing that using my rusty old Delphi 5 so surely I must be missing something. Lieven: You are right, this is a bit obscure without comments. I will develop some explanation.

Thanks for your answer and the explanation of all of the commands, I really appreciate it. I'm accepting the other answer because I prefer to use a plugin for this. I've marked this down because it causes more confusion, given the disparity between the level of someone asking the question and the level required to understand this answer. Show 2 more comments.

Populate :args from a shell command It's possible on some operating systems 1 to supply the files for :args via a shell command. Community Bot 1 1 1 silver badge. Oleg Khalidov Oleg Khalidov 4, 1 1 gold badge 24 24 silver badges 28 28 bronze badges. You should also say that you are the author of the plugin : — sitilge. Kyle Heironimus Kyle Heironimus 7, 6 6 gold badges 36 36 silver badges 48 48 bronze badges.



0コメント

  • 1000 / 1000