shell下的fzf
安裝ccat
ccat和cat的功能一樣,但是ccat能夠高亮顯示,但是cat不能,ccat安裝方法如下:
wget https://github.com/jingweno/ccat/releases/download/v1.1.0/linux-amd64-1.1.0.tar.gz
tar -xvf linux-amd64-1.1.0.tar.gz
sudo cp ./linux-amd64-1.1.0/ccat /usr/bin/
安裝fzf
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
當然你需要確保你安裝了git
配置zshrc
在你的zshrc中寫入:
export FZF_DEFAULT_OPTS='--bind ctrl-j:down,ctrl-k:up --preview "[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500"'
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git . ~/.config /home / '
export FZF_COMPLETION_TRIGGER='\'
export FZF_PREVIEW_COMMAND='[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500'
如果你用的是bash或其他shell,應該也是可以的,你只要在相應的rc中寫入這段命令就可以了
終端輸入fzf就可以開始查找文件了,fzf會返回你的文件路徑
而且fzf可以和其他命令很好的交互,比方說我用vim和fzf交互,只需要輸入vim \
然後按下<Tab>鍵,就可以查找我需要編輯的文件,找到後按下回車就可以開始編輯文件
ps: 我額外安裝了fd作爲搜索工具
neovim下的fzf
安裝fzf.vim
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
配置fzf
" find file
noremap \ :FZF<CR>
" find history
noremap <C-h> :MRU<CR>
" find lines containing keywords
noremap <C-l> :LinesWithPreview<CR>
" find buffer
noremap <C-b> :Buffers<CR>
autocmd! FileType fzf
autocmd FileType fzf set laststatus=0 noruler
\| autocmd BufLeave <buffer> set laststatus=2 ruler
command! -bang -nargs=* Buffers
\ call fzf#vim#buffers(
\ '',
\ <bang>0 ? fzf#vim#with_preview('up:60%')
\ : fzf#vim#with_preview('right:0%', '?'),
\ <bang>0)
command! -bang -nargs=* LinesWithPreview
\ call fzf#vim#grep(
\ 'rg --with-filename --column --line-number --no-heading --color=always --smart-case . '.fnameescape(expand('%')), 1,
\ fzf#vim#with_preview({'options': '--delimiter : --nth 4.. --sort'}, 'up:50%', '?'),
\ 1)
command! -bang -nargs=* MRU call fzf#vim#history(fzf#vim#with_preview())
當我在vim中按下"\",vim會調用fzf查找你需要編輯的文件,選中文件後回車即可編輯
當我在vim中按下"Ctrl+h",vim會顯示你最近打開的文件
當我在vim中按下"Ctrl+l",vim會高亮包含你搜索關鍵詞的一行代碼
當我在vim中按下"Ctrl+b",vim會顯示當前你打開的buffer,輸入數字或者符號即可跳轉buffer
ranger下的fzf
修改ranger的commands.py
在commands.py
中定義fzf_select
類
class fzf_select(Command):
"""
:fzf_select
Find a file using fzf.
With a prefix argument select only directories.
See: https://github.com/junegunn/fzf
"""
def execute(self):
import subprocess
import os.path
if self.quantifier:
# match only directories
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
else:
# match files and directories
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
fzf = self.fm.execute_command(command, universal_newlines=True, stdout=subprocess.PIPE)
stdout, stderr = fzf.communicate()
if fzf.returncode == 0:
fzf_file = os.path.abspath(stdout.rstrip('\n'))
if os.path.isdir(fzf_file):
self.fm.cd(fzf_file)
else:
self.fm.select_file(fzf_file)
配置你的rc.conf
在你的rc.conf中寫入
map \ fzf_select
我的新博客