fzf 是一个基于Go
实现的一个交互式的命令行搜索工具,可以搜索如:文件,历史命令,git提交等。功能非常强大。
基于FZF
实现的vim
插件,让我们在vim
中 搜索 更加高效。
OS: MacOS 10.13
Vim: Neovim v0.2.2
安装
- 安装
fzf
,将fzf
的目录添加到Vim
的&runtimepath
来启用它。
" If installed using Homebrew
set rtp+=/usr/local/opt/fzf
" If installed using git
set rtp+=~/.fzf
- 安装插件
fzf.vim
, 配置fzf
命令。
安装 fzf
- 使用 Homebrew 安装
fzf
brew install fzf
- 使用
Git
手动安装
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
安装 fzf.vim
使用 Vim-plug 作为插件管理工具。
Plug '/usr/local/opt/fzf'
Plug 'junegunn/fzf.vim'
OR 使用vim-plug
安装 fzf
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
fzf 命令
Command | List |
---|---|
Files [PATH] | Files (similar to :FZF ) |
GFiles [OPTS] | Git files (git ls-files ) |
GFiles? | Git files (git status ) |
Buffers | Open buffers |
Colors | Color schemes |
Ag [PATTERN] | ag search result (ALT-A to select all, ALT-D to deselect all) |
Lines [QUERY] | Lines in loaded buffers |
BLines [QUERY] | Lines in the current buffer |
Tags [QUERY] | Tags in the project (ctags -R ) |
BTags [QUERY] | Tags in the current buffer |
Marks | Marks |
Windows | Windows |
Locate PATTERN | locate command output |
History | v:oldfiles and open buffers |
History: | Command history |
History/ | Search history |
Snippets | Snippets (UltiSnips) |
Commits | Git commits (requires fugitive.vim) |
BCommits | Git commits for the current buffer |
Commands | Commands |
Maps | Normal mode mappings |
Helptags | Help tags 1 |
Filetypes | File types |
表格来自fzf.vim
在普通模式下,使用:Command
调用命令。
配置 fzf.vim 快捷键
在 fzf.vim 中有着非常详细的设置教程。这里笔者举出自己经常使用的一些快捷键设置:
-
将
Files [PATH]
命令绑定到快捷键<leader>f
上,快速搜索当前工程目录下的文件 -
将
Buffers
命令绑定到<leader>b
上,快速搜索buffer
nnoremap <silent> <Leader>f :Files<CR>
nnoremap <silent> <Leader>b :Buffers<CR>
<Esc>
或 <C-c>
退出 fzf
使用 Ag 实现文本搜索
实现Ag
命令需要安装 the_silver_searcher
brew install the_silver_searcher
- 配置
.vimrc
command! -bang -nargs=* Ag
\ call fzf#vim#ag(<q-args>,
\ <bang>0 ? fzf#vim#with_preview('up:60%')
\ : fzf#vim#with_preview('right:50%:hidden', '?'),
\ <bang>0)
nnoremap <silent> <Leader>A :Ag<CR>
将:Ag
命令绑定到<leader>A
上,这样就可以在Vim
内搜索项目内的文本了。
小结
由于大多命令笔者并不是经常使用,所以并没有对它们进行设置。更多更全的命令读者可以到 fzf.vim 或者向fzf
发起 Issues 和大神们讨论。
希望大家有所收获。