有人知道从 linux 控制台执行快速模糊搜索的方法吗?
当我需要在项目中查找文件但我不记得确切的文件名时,我经常会遇到这种情况。在Sublime 文本编辑器中,我会按 Ctrl-P 并键入名称的一部分,这将生成一个可供选择的文件列表。这是一个令我非常满意的惊人功能。问题是,在大多数情况下,我必须通过 ssh 在远程机器上的控制台中浏览代码。所以我想知道是否有类似于 Linux 控制台的“Go Anywhere”功能的工具?
有人知道从 linux 控制台执行快速模糊搜索的方法吗?
当我需要在项目中查找文件但我不记得确切的文件名时,我经常会遇到这种情况。在Sublime 文本编辑器中,我会按 Ctrl-P 并键入名称的一部分,这将生成一个可供选择的文件列表。这是一个令我非常满意的惊人功能。问题是,在大多数情况下,我必须通过 ssh 在远程机器上的控制台中浏览代码。所以我想知道是否有类似于 Linux 控制台的“Go Anywhere”功能的工具?
您可能会发现fzf很有用。这是一个用 Go 编写的通用模糊查找器,可用于任何事物列表:文件、进程、命令历史记录、git 分支等。
它的安装脚本将为CTRL-T
您的 shell 设置键绑定。下面的 GIF 显示了它是如何工作的。
这些答案中的大多数不会像 sublime text 那样进行模糊搜索——它们可能会匹配部分答案,但它们并没有做到“按此顺序查找所有字母”的好行为。
我认为这更接近你想要的。我整理了一个特殊版本的 cd ('fcd'),它使用模糊搜索来查找目标目录。超级简单——只需将其添加到您的 bashrc 中:
function joinstr { local IFS="$1"; shift; echo "$*"; }
function fcd { cd $(joinstr \* $(echo "$*" | fold -w1))* }
这将在输入中的每个字母之间添加一个 *,所以如果我想去,例如,
/home/dave/results/sample/today
我可以输入以下任何内容:
fcd /h/d/r/spl/t
fcd /h/d/r/s/t
fcd /h/d/r/sam/t
fcd /h/d/r/s/ty
以第一个为例,这将执行cd /*h*/*d*/*r*/*s*p*l*/*t*
并让 shell 整理出实际匹配的内容。
只要第一个字符正确,并且从路径中的每个目录中写入一个字母,它就会找到您要查找的内容。也许您可以根据自己的需要进行调整?重要的一点是:
$(joinstr \* $(echo "$*" | fold -w1))*
它创建了模糊搜索字符串。
fasd shell 脚本可能也值得一看。
fasd
提供对 POSIX shell 的文件和目录的快速访问。它的灵感来自 autojump、z 和 v 等工具。Fasd 会跟踪您访问过的文件和目录,以便您可以在命令行中快速引用它们。
它与完整查找所有文件略有不同,因为它只搜索最近打开的文件。但是,它仍然非常有用。
find . -iname '*foo*'
查找包含foo
.
我通常使用:
ls -R | grep -i [whatever I can remember of the file name]
从我期望文件所在的目录上方的目录中-您在目录树中的位置越高,执行速度就越慢。
当我找到确切的文件名时,我会在 find 中使用它:
find . [discovered file name]
这可以折叠成一行:
for f in $(ls --color=never -R | grep --color=never -i partialName); do find -name $f; done
(我发现 ls 和 grep 被别名为“--color=auto”的问题)
我不知道您对终端有多熟悉,但这可以帮助您:
find | grep 'report'
find | grep 'report.*2008'
抱歉,如果您已经知道grep
并正在寻找更高级的东西。
您可能想尝试 AGREP或其他使用TRE正则表达式库的东西。
(来自他们的网站:)
TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzzy) matching.
At the core of TRE is a new algorithm for regular expression matching with submatch addressing. The algorithm uses linear worst-case time in the length of the text being searched, and quadratic worst-case time in the length of the used regular expression. In other words, the time complexity of the algorithm is O(M2N), where M is the length of the regular expression and N is the length of the text. The used space is also quadratic on the length of the regex, but does not depend on the searched string. This quadratic behaviour occurs only on pathological cases which are probably very rare in practice.
TRE is not just yet another regexp matcher. TRE has some features which are not there in most free POSIX compatible implementations. Most of these features are not present in non-free implementations either, for that matter.
Approximate pattern matching allows matches to be approximate, that is, allows the matches to be close to the searched pattern under some measure of closeness. TRE uses the edit-distance measure (also known as the Levenshtein distance) where characters can be inserted, deleted, or substituted in the searched text in order to get an exact match. Each insertion, deletion, or substitution adds the distance, or cost, of the match. TRE can report the matches which have a cost lower than some given threshold value. TRE can also be used to search for matches with the lowest cost.
您可以执行以下操作
grep -iR "text to search for" .
在哪里 ”。” 作为起点,所以你可以做类似的事情
grep -iR "text to search" /home/
这将使 grep 在 /home/ 下的每个文件中搜索给定的文本,并列出包含该文本的文件。
您可以尝试使用 bash 完成的 bash 脚本的模糊目录更改工具 c- ( Cminus )。它在某种程度上受到仅匹配访问路径的限制,但非常方便且非常快速。
GitHub 项目:whitebob/cminus
YouTube上的介绍:https ://youtu.be/b8Bem53Cz9A
您可以将这样的 find 用于复杂的正则表达式:
find . -type f -regextype posix-extended -iregex ".*YOUR_PARTIAL_NAME.*" -print
或者对于更简单的类似 glob 的匹配:
find . -type f -name "*YOUR_PARTIAL_NAME*" -print
或者您也可以使用 find2perl(它比 find 更快、更优化),如下所示:
find2perl . -type f -name "*YOUR_PARTIAL_NAME*" -print | perl
如果您只是想看看 Perl 是如何做到的,请删除该| perl
部分,您将看到它生成的代码。顺便学习一下是非常好的方法。
或者,像这样编写一个快速的 bash 包装器,并在需要时调用它:
#! /bin/bash
FIND_BASE="$1"
GLOB_PATTERN="$2"
if [ $# -ne 2 ]; then
echo "Syntax: $(basename $0) <FIND_BASE> <GLOB_PATTERN>"
else
find2perl "$FIND_BASE" -type f -name "*$GLOB_PATTERN*" -print | perl
fi
将其命名为qsearch
,然后像这样调用它:qsearch . something
在终端中搜索zsh文件或文件夹,并使用find、fzf、vim和cd的组合打开或导航到它。
在zsh中安装fzf并添加脚本,然后重新加载 shell~/.zshrc
source ~/.zshrc
fzf-file-search() {
item="$(find '/' -type d \( -path '/proc/*' -o -path '/dev/*' \) -prune -false -o -iname '*' 2>/dev/null | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --rev erse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@")"
if [[ -d ${item} ]]; then
cd "${item}" || return 1
elif [[ -f ${item} ]]; then
(vi "${item}" < /dev/tty) || return 1
else
return 1
fi
zle accept-line
}
zle -N fzf-file-search
bindkey '^f' fzf-file-search
按键盘快捷键'Ctrl+F'
运行它,这可以在bindkey '^f'
. 它搜索(find)所有文件/文件夹(fzf)并根据文件类型导航到目录(cd)或使用文本编辑器(vim)打开文件。
还可以使用fasd快速打开最近的文件/文件夹:
fasd-fzf-cd-vi() {
item="$(fasd -Rl "$1" | fzf -1 -0 --no-sort +m)"
if [[ -d ${item} ]]; then
cd "${item}" || return 1
elif [[ -f ${item} ]]; then
(vi "${item}" < /dev/tty) || return 1
else
return 1
fi
zle accept-line
}
zle -N fasd-fzf-cd-vi
bindkey '^e' fasd-fzf-cd-vi
键盘快捷键'Ctrl+E'
检查其他有用的提示和技巧以在终端内快速导航https://github.com/webdev4422/.dotfiles