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

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

问题描述

我在 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?

推荐答案

我决定在 Vim 之外使用 ackperl 来解决这个问题,这样我就可以使用更强大的 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 是一个很棒的命令行工具,它混合了 grepfind 和完整的 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). It's written in pure Perl, it's fast, it has match highlighting, it 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/replacement/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/replacement/g' command.

Perl 是一种编程语言.

Perl is a programming language.

-p 选项使 Perl 围绕您的程序创建一个循环,循环遍历文件名参数.

The -p option causes Perl to create a loop around your program which iterates over filename arguments.

-i 选项使 Perl 就地编辑文件.您可以修改它以创建备份.

The -i option causes Perl to edit the file in place. You can modify this to create backups.

-E 选项使 Perl 执行指定为程序的一行代码.在我们的例子中,程序只是一个 Perl 正则表达式替换.

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.

有关 Perl 命令行选项的更多信息,请参阅 perldoc perlrun.有关 Perl 的更多信息,请参阅 http://www.perl.org/.

For more information on Perl command line options, see perldoc perlrun. For more information on Perl, see http://www.perl.org/.

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

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