使用正则表达式进行反向历史搜索


10

我正在寻找一种工具,该工具允许使用简单的正则表达式(或仅多个匹配项)进行反向增量搜索。例如,如果要查找命令“ foo bar baz”,可以执行以下操作来快速查找该命令:

CRTL-R(开始搜索)键入“ foo”(匹配使用foo的最新命令)继续键入“ foo | baz”(匹配包含“ foo”和“ baz”的最新命令)。

是否存在这样的东西?如果没有,我该如何自行实施?

Answers:


4

自定义部件history-incremental-multi-searchzsh

设定

创建一个目录并将其包含在您的目录中$fpath。例如,我创建了一个目录~/.zsh/functions,并在行fpath=($HOME/.zsh/functions $fpath)中添加了一行.zshrc

将以下内容放入该history-incremental-multi-search目录中命名的文件中。

emulate -L zsh
setopt extended_glob

local oldbuffer=$BUFFER
local -i oldcursor=$CURSOR

local dir                # search direction
local chars              # input buffer
local -a words           # search terms
local -a found           # all history items that match first term
local -i hindex=$HISTNO  # current 
local -i lmatch          # last matched history item (for prev/next)

if [[ $WIDGET == *forward* ]]; then
    dir=fwd
else
    dir=bck
fi

function find-next {
    # split the input buffer on spaces to get search terms
    words=(${(s: :)chars})

    # if we have at least one search term
    if (( $#words )); then
        # get all keys of history items that match the first
        found=(${(k)history[(R)*$words[1]*]})
        if (( $#found )); then
            # search in widget direction by default
            # but accept exception in $1 for "prev match"
            search-${1:-$dir}
        else
            # no matches
            lmatch=$HISTNO
        fi
    else
        # no search terms
        lmatch=$HISTNO
        BUFFER=$oldbuffer
        CURSOR=$oldcursor
    fi
}

function search-fwd {
    # search forward through matches
    local -i i
    for (( i = $#found; i > 0; i-- )); do
        # but not before hindex as we're searching forward
        if [[ $found[$i] -gt $hindex ]]; then
            set-match $found[$i]
        fi
    done
}

function search-bck {
    # search backward through matches
    local -i i
    for (( i = 1; i <= $#found; i++ )); do
        # but not beyond hindex as we're searching backward
        if [[ $found[$i] -lt $hindex ]]; then
            set-match $found[$i]
        fi
    done
}

function set-match {
    # match history item against all terms and select it if successful
    local match=1
    local -i i
    for (( i = 2; i <= $#words; i++ )); do
        if [[ $history[$1] != *$words[$i]* ]]; then
            match=0
            break
        fi
    done
    if [[ $match -ne 0 ]]; then
        lmatch=$1
        BUFFER=$history[$1]
        CURSOR=$#BUFFER
        break
    fi
}

# display sub prompt
zle -R "${dir}-i-search-multi:"

# handle input keys
while read -k; do
    case $REPLY in
        # next
        $'\C-n' )
            hindex=$lmatch
            find-next
            ;;
        # prev
        $'\C-p' )
            hindex=$lmatch
            if [[ $dir == fwd ]]; then
                find-next bck
            else
                find-next fwd
            fi
            ;;
        # break
        $'\e' | $'\C-g' )
            BUFFER=$oldbuffer
            CURSOR=$oldcursor
            break
            ;;
        # accept
        $'\C-m' | $'\C-j' )
            if [[ $lmatch -eq $HISTNO ]]; then
                BUFFER=$oldbuffer
                CURSOR=$oldcursor
            else
                HISTNO=$lmatch
            fi
            break
            ;;
        # erase char
        $'\C-h' | $'\C-?' )
            chars=$chars[1,-2]
            hindex=$HISTNO
            find-next
            ;;
        # erase word
        $'\C-w' )
            if [[ $chars =~ \  ]]; then
                chars=${chars% *}
            else
                chars=
            fi
            hindex=$HISTNO
            find-next
            ;;
        # kill line
        $'\C-u' )
            chars=
            hindex=$HISTNO
            find-next
            ;;
        # add unhandled chars to buffer
        * )
            chars=${chars}${REPLY}
            hindex=$HISTNO
            find-next
            ;;
    esac

    zle -R "${dir}-i-search-multi: $words"
done

将其放入或从以下来源获取.zshrc

autoload -U history-incremental-multi-search

# make new widgets from function
zle -N history-incremental-multi-search-backward history-incremental-multi-search
zle -N history-incremental-multi-search-forward history-incremental-multi-search

# bind the widgets to keys
bindkey '^Xr' history-incremental-multi-search-backward
bindkey '^Xs' history-incremental-multi-search-forward

采用

您现在应该能够启动与落后的渐进式搜索Ctrl+Xr,向前Ctrl+Xs

输入您的搜索词,以空格分隔。以下按键可用于控制它:

  • ← Backspace:擦除字符

  • Ctrl+W:删除单词

  • Ctrl+U:杀线

  • Ctrl+N:下一场比赛

  • Ctrl+P:上一场比赛

  • Ctrl+G/ Esc:取消搜索

  • Enter:接受

该解决方案可能可以简化很多。它更多是概念上的功能证明,还有很大的改进空间。


感谢您抽出宝贵的时间来写这些答案,这对我有很大帮助。我希望更多的人偶然发现这一点并觉得有用。
2011年

6

您可以浏览历史记录:

history | egrep '(foo|baz)'

希望对您有所帮助。


0

建立在@peth的答案上:

Zsh现在附带history-incremental-pattern-search-backward,您不需要自己定义它。只需添加键绑定。我更喜欢^R通过将以下行添加到来覆盖.zshrc

bindkey '^R' history-incremental-pattern-search-backward

现在,您可以在搜索中使用glob(sic!not rexex)运算符。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.