如何进行搜索和用ack替换成vim? [英] How to do search & replace with ack in vim?

查看:141
本文介绍了如何进行搜索和用ack替换成vim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Vim中使用Ack插件,它可以帮助我快速搜索项目中的字符串。但是,有时我想替换所有或部分出现的找到的字符串。您可以像这样使用Vim arglist进行某种全局搜索并替换(

I am using the Ack plugin in Vim, which helps me to quickly search for strings in my project. However, sometimes I want to replace all or some occurrences of the found strings. You can do some kind of global search and replace using the Vim arglist like this (source) :

:args app/views/*/*
:argdo %s/, :expire.*)/)/ge | update

但是我没有使用 args ,而是希望通过Ack进行搜索,然后在找到的所有文件中进行替换。有没有一种类似于 argdo 命令的方法?

But instead of using args, I would prefer to do a search via Ack and then do the replace in all files that have been found. Is there a way to do it similar to the argdo command?

推荐答案

我决定使用ack和Perl在Vim之外解决此问题,因此我可以使用功能更强大的Perl正则表达式代替GNU子集。您可以将其映射到vimrc中的按键。

I've decided to use ack and Perl to solve this problem outside of Vim so I could use the more powerful Perl regular expressions instead of the GNU subset. You could map this to a key stroke in your vimrc.

ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'



说明



ack



ack 是一个很棒的命令行工具,它混合了 grep find 和完整的Perl正则表达式(不仅仅是GNU子集)。它使用纯Perl编写,速度快,具有突出显示的匹配项,可在Windows上运行,并且比传统命令行工具对程序员更友好。使用 sudo apt-get install ack-grep 在Ubuntu上安装它。

Explanation

ack

ack is an awesome command line tool that is a mix of grep, find, and full Perl regular expressions (not just the GNU subset). Its written in pure Perl, it's fast, it has match highlighting, works on Windows and it's friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep.

Xargs 是旧的UNIX命令行工具。它从标准输入读取项目,并执行指定的命令,然后执行为标准输入读取的项目。因此,基本上将ack生成的文件列表附加到 perl -pi -E's / pattern / replacemnt / g'命令的末尾。

Xargs is an old unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacemnt/g' command.

Perl是一种编程语言。 -p选项使Perl在您的程序周围创建一个循环,该循环遍历文件名参数。 -i选项使Perl在适当位置编辑文件。您可以修改它以创建备份。 -E选项使Perl执行指定为程序的一行代码。在我们的例子中,该程序只是Perl regex的替代品。有关Perl命令行选项的更多信息,请 perldoc perlrun 。有关Perl的更多信息,请参见 http://www.perl.org/

Perl is a programming language. The -p option causes Perl to create a loop around your program which iterates over filename arguments. The -i option causes Perl to edit the file in place. You can modify this to create backups. The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution. For more information on Perl command line options perldoc perlrun. For more information on Perl see http://www.perl.org/.

这篇关于如何进行搜索和用ack替换成vim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆