fzf设计是怎样的

fzf设计是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

fzf说明

fzf本身是一个非常快速的、通用的,模糊匹配/严格匹配或是否区分大小写的命令行匹配工具,不仅仅可以支持文件、历史命令,可以支持其他任何通用的内容的匹配。

问题

fzf用于匹配文件时,每次启动都会重新扫描磁盘上的文件列表(而且貌似是深度优先),导致一个问题: 如果文件数量较大,经常搜索不到很浅的目录中的文件。

fzf目前还是主要被vim等编辑器用在项目级别的文件匹配,还无法像everything一样全局/系统级的文件匹配。 如果想做到后者的目的,感觉可以从以下几点进行优化:

  1. 在后台运行一个daemon进程,缓存文件列表

  2. 把文件列表缓存在文件中,避免下次大范围扫描磁盘

  3. 通过inotify等机制监视文件的实时变

2019-12-12 补充

fzf官方提供了很多好用的方法,可以自己看情况使用: https://github.com/junegunn/fzf/wiki/examples

例如:

# fd - cd to selected directory
fda() {
  local dir
  dir=$(find ${1:-.} -path '*/\.*' -prune \
                  -o -type d -print 2> /dev/null | fzf +m) &&
  cd "$dir"
}

# Another fd - cd into the selected directory
# This one differs from the above, by only showing the sub directories and not
#  showing the directories within those.
fd() {
  DIR=`find * -maxdepth 0 -type d -print 2> /dev/null | fzf-tmux` \
    && cd "$DIR"
}

# fe [FUZZY PATTERN] - Open the selected file with the default editor
#   - Bypass fuzzy finder if there's only one match (--select-1)
#   - Exit if there's no match (--exit-0)
fe() {
  local files
  IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
  [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
}

# fkill - kill processes - list only the ones you can kill. Modified the earlier script.
fkill() {
    local pid 
    if [ "$UID" != "0" ]; then
        pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
    else
        pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
    fi  

    if [ "x$pid" != "x" ]
    then
        echo $pid | xargs kill -${1:-9}
    fi  
}

看完上述内容,你们掌握fzf设计是怎样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!