New fxg for me

本文介绍了一种利用ag工具改进文件搜索的方法,并通过简单的脚本实现了搜索结果与vim编辑器的集成。该方案克服了原有fxg工具性能不佳的问题,并针对复杂输出进行了优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fxg means find and xargs then grep. It has a insufferable performace, so 
I use ag to instead, after forking some tools made by lilydjwg The tool chain is used for finding pattern in files and opening file by vim. I rewrite a simple version of vv.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/env python
import sys
from subprocess import call
line_number = int(sys.argv[1])
with open('/tmp/fxg.log') as f:
    for index, line in enumerate(f.readlines()):
        if index == line_number - 1:
            args = line.split(':')
            filename = args[0].strip().split("\t")[1]
            file_line = args[1]
            print filename + ":" + file_line
            call(["vim", filename,  "+" + file_line])

Insert of writing regex pattern to match the complex colored output, I call ag the second time to gen a un-colored output to log file. And I also add a -G for file pattern.

fxg () {
    ag -s -G $1 --column --nogroup --color $2 | nl && ag -s -G $1 --column --nogroup $2 | nl > /tmp/fxg.log
}

Update: 15-12-9

Ag on my MacOSX has an unstable sorting result. It makes the twice call of Ag return different order.
So I add sort to this command. -0 or -print0 will take space in filename as a special character.

fxg () {
    ag -s -G $1 --column --nogroup --color -0 $2 | sort | nl && ag -s -G $1 --column --nogroup -0 $2 | sort | nl > /tmp/fxg.log
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值