• 请不要在回答技术问题时复制粘贴 AI 生成的内容
hjue
V2EX  ›  程序员

有人用 ripgrep(rg) 吗?

  •  
  •   hjue · Dec 9, 2021 · 2858 views
    This topic created in 1790 days ago, the information mentioned may be changed or developed.

    typora 的搜索不好用。

    一直在用 ripgrep 搜索我的 md 文档,非常好用。

    今天想对搜索结果时间倒排,找到含有关键字的最新文章。

    发现排序功能

    rg   --sortr modified keyword 
    

    发现搜索结果并不是按文件修改时间倒排,有谁知道是哪里的问题吗?

    12 replies  •  2022-01-12 18:34:20 +08:00
    ynyounuo
    ynyounuo
    Dec 9, 2021
    1
    我用 ls --time-style='+%d-%m-%Y %H:%M:%S' -l; rg --sortr modified '{keywords}' 试了试好像没啥问题
    felixin
    felixin
    Dec 9, 2021 via Android
    2
    silver search
    yanbo92
    yanbo92
    Dec 9, 2021 via iPhone
    3
    看名字像是搜索死人的
    ProPh3t
    ProPh3t
    Dec 9, 2021
    4
    @yanbo92 rest in peace, `grep`...
    Leviathann
    Leviathann
    Dec 9, 2021
    5
    @yanbo92 名字的确是要 grep 去死
    hjue
    hjue
    OP
    Dec 9, 2021
    6
    @ynyounuo 我用的是 mac ,不支持--time-style ,可能是这个原因导致的。 有时间看看 ripgrep 的代码,找找原因
    ynyounuo
    ynyounuo
    Dec 9, 2021
    7
    @hjue
    和 ls 没关系吧,ls -lT 试试
    hjue
    hjue
    OP
    Dec 9, 2021
    8
    @felixin ag 怎么支持搜索结果按时间倒排?
    hjue
    hjue
    OP
    Dec 10, 2021
    9
    @ynyounuo 一个目录下排序没有问题。包含子目录有问题。

    相关代码在这里 https://github.com/BurntSushi/ripgrep/blob/9f924ee187d4c62aa6ebe4903d0cfc6507a5adb5/crates/core/args.rs

    ```
    match self.kind {
    SortByKind::None => {}
    SortByKind::Path => {
    if self.reverse {
    builder.sort_by_file_name(|a, b| a.cmp(b).reverse());
    } else {
    builder.sort_by_file_name(|a, b| a.cmp(b));
    }
    }
    SortByKind::LastModified => {
    builder.sort_by_file_path(move |a, b| {
    sort_by_metadata_time(a, b, self.reverse, |md| {
    md.modified()
    })
    });
    }
    SortByKind::LastAccessed => {
    builder.sort_by_file_path(move |a, b| {
    sort_by_metadata_time(a, b, self.reverse, |md| {
    md.accessed()
    })
    });
    }
    SortByKind::Created => {
    builder.sort_by_file_path(move |a, b| {
    sort_by_metadata_time(a, b, self.reverse, |md| {
    md.created()
    })
    });
    }
    }
    ```
    ynyounuo
    ynyounuo
    Dec 10, 2021
    10
    @hjue
    rg 是这样设计的而且有人提了类似的 feature request 也没有被 maintainer 认可
    https://github.com/BurntSushi/ripgrep/issues/1298

    倒是你可以试试他的 workaround 改成一个 function 来用
    xieyuheng
    xieyuheng
    Jan 11, 2022
    11
    还有两个类似的是 ambs ambr ,我经常用这个还有 ag 。

    https://github.com/dalance/amber
    hjue
    hjue
    OP
    Jan 12, 2022
    12
    @xieyuheng ag 貌似没有排序的功能。

    我比较了一下 rg 和 ag 的搜索结果,发现他们两个搜索结果的排序也不相同。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3982 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 01:02 · PVG 09:02 · LAX 17:02 · JFK 20:02
    ♥ Do have faith in what you're doing.