【问题标题】:cut down directory against relative directory from ag results根据 ag 结果中的相对目录减少目录
【发布时间】:2016-10-21 08:59:34
【问题描述】:

如果用 ag 在像a/long/long/long/path/to/project/ 这样的长路径目录中搜索,结果列表将包含长路径。有没有办法在结果列表中获取项目路径?

ag test a/long/long/long/path/to/project/ 的示例结果列表:

a/long/long/long/path/to/project/test.txt: test.....
a/long/long/long/path/to/project/js/test2.js: test: function() {

预期结果:

test.txt: test.....
js/test2.js: test: function() {

为什么我用指定的目录路径而不是cd 搜索并搜索?我使用 ag 在 vi​​m 中进行项目范围搜索:找到项目路径并将其传递给 vim 命令,该命令将使用 ag 搜索给定路径。

解决方案

Ps:我用Plug 'mhinz/vim-grepper

function! VimGrepperConfig()
    function! s:ag_at_project_root(keyword)
        let root = s:find_root()
        if !empty(root)
            execute 'cd' root
            execute 'GrepperAg' a:keyword
            execute 'cd -'
        else
           execute 'GrepperAg' a:keyword
        endif
    endfunction
    command! -nargs=1 AgAtProjectRoot call s:ag_at_project_root('<args>')
    " ag search
    no <Leader>/ :AgAtProjectRoot 
endfunction
call VimGrepperConfig()

【问题讨论】:

    标签: vim ag


    【解决方案1】:

    如果ag 在快速修复窗口中生成结果,您可以在快速修复窗口中执行:cd {path} 来更新显示的路径。您可能需要:cclose:copen qf 窗口才能看到简化操作。

    当我的&amp;makeprg 包含(cd build/path &amp;&amp; make target) 之类的内容时,我经常在:make 之后执行cd .

    【讨论】:

    • 但我的解决方案引发了一个新问题。搜索后我切换回最后一个pwd,现在如果我从快速修复列表中打开文件,它将在当前pwd 创建一个新文件,而不是在项目中打开文件。啊……
    猜你喜欢
    • 2021-06-04
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2017-08-09
    • 2011-12-19
    相关资源
    最近更新 更多