Compatible fzf Plugins Loader

每家包起來的位置都不一樣,很麻煩耶!

fzf 好好用

前一陣子又被介紹了一下 fzfvim plugin,真正花點時間了解了一下之後, 發覺到 fzf 本身還能拿來做不少事情,也能取代幾個目前用的 vim plugins, 所以就認真得來設定一番。

除了 fzf 本身之外,也還有很多跟其他程式配合的 plugins,像是 zsh 以及剛剛提到的 vim,都是日常生活中的基礎必需品,所以當然也想要每個 terminal 都一樣啊!

想想看要設定上的環境大概有:

  • Arch Linux yay -S fzf
  • Android Termux pkg install fzf
  • 沒有 root 的 Ubuntu Linux conda install -c conda-forge fzf

fzf and ohmyzsh

zsh 的管理工具 ohmyzsh 也好好用啊,當然裡面就有寫好的 fzf 的 plugin 了, 在一般的 上直接 plugins=(fzf)應該大致上都沒有問題, 但放到其他的環境像是 termux 就 load 不成功了,原因是 plugin 不知道另外裝的 fzf 在哪裡,需要另外指定.

1
2
# Set fzf installation directory path
export FZF_BASE=/path/to/fzf/install/dir

指定之後也只是找到 completion.zshkey-bindings.sh 直接 source 而已。 也可以看到就算是 ,也還是會包在不同地方,

1
2
3
4
5
6
7
8
9
10
11
# Fix fzf shell directory for Archlinux package
if [[ ! -d "${fzf_base}/shell" ]] && [[ -f /etc/arch-release ]]; then
fzf_shell="${fzf_base}"
else
fzf_shell="${fzf_base}/shell"
fi
...
source "${fzf_shell}/completion.zsh"
...
source "${fzf_shell}/key-bindings.zsh"

不是很想這樣判斷展開 fzf_base, 反正 fzf 都在 PATH 裡的嘛, 所以就自己改了以上提到的環境都能用的方法。

.zshrc
1
2
3
for F in $(find $(which fzf | sed 's/\/bin\/fzf/\/share\/fzf/') -name "*.zsh"); do
source $F
done

fzf and vim

相同的 vim 也有寫好的 plugin fzf.vim,根據

Vim
The basic Vim plugin is already included within the package and installed to Vim’s global plugin directory. Thus, you don’t need to add anything to your .vimrc to be able to use it. It only provides the FZF command, though. There is an additional Vim plugin made by the author of fzf that defines some convenience functions

本來只要裝 fzf.vim 就可以了,偏偏 anacondafzf 是沒有包進基本的 vim fzf plugin 的,即便是用了 anacondavim 也是沒有, 所以就還是額外指向原本的 upstream 的 fzf 那一個。

.vimrc
1
2
3
4
5
6
7
8
" fzf
Bundle 'junegunn/fzf'
Bundle 'junegunn/fzf.vim'
map <C-K> :Buffers<CR>
map <C-P> :Files<CR>
map <C-N> :Rg! <C-R><C-W><CR>
nmap sr :History: <CR>
nmap s/ :History/ <CR>