|
| 1 | +" Copyright (c) 2019-present Kaiming Guo. All rights reserved. |
| 2 | +" Use of this source code is governed by a BSD-style license that can be |
| 3 | +" found in the LICENSE file. |
| 4 | + |
| 5 | +if exists('g:plug_installing_plugins') |
| 6 | + if isdirectory('/usr/local/opt/fzf') |
| 7 | + Plug '/usr/local/opt/fzf' | Plug 'junegunn/fzf.vim' " hidden readme |
| 8 | + else |
| 9 | + Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --bin' } " hidden readme |
| 10 | + Plug 'junegunn/fzf.vim' |
| 11 | + endif |
| 12 | + finish |
| 13 | +endif |
| 14 | + |
| 15 | +if has('autocmd') |
| 16 | + augroup auto_fzf_settings |
| 17 | + autocmd! |
| 18 | + |
| 19 | + autocmd! FileType fzf |
| 20 | + autocmd FileType fzf set laststatus=0 noshowmode noruler |
| 21 | + \| autocmd BufLeave <buffer> set laststatus=1 showmode ruler |
| 22 | + augroup END |
| 23 | +endif |
| 24 | + |
| 25 | +let $FZF_DEFAULT_COMMAND = |
| 26 | + \ get(g:, 'fzf_default_cmd', |
| 27 | + \ !empty($FZF_DEFAULT_COMMAND) ? $FZF_DEFAULT_COMMAND : |
| 28 | + \ executable('rg') ? 'rg --files --hidden --follow --glob "\!.git/*"' : |
| 29 | + \ executable('ag') ? 'ag --hidden --ignore .git -g ""' : |
| 30 | + \ executable('fd') ? 'fd --type f' : |
| 31 | + \ 'find * -path "*/\.*" -prune -o -path "node_modules/**" -prune -o -path "target/**" -prune -o -path "dist/**" -prune -o -type f -print -o -type l -print 2> /dev/null' |
| 32 | + \ ) |
| 33 | + |
| 34 | +if executable('rg') |
| 35 | + set grepprg=rg\ --vimgrep |
| 36 | + command! -bang -nargs=* Find call fzf#vim#grep( |
| 37 | + \'rg --column --line-number --no-heading --fixed-strings --ignore-case --hidden --follow --glob "\!.git/*" --color "always" ' |
| 38 | + \. '| tr -d "\017"', 1, <bang>0) |
| 39 | +elseif executable('ag') |
| 40 | + set grepprg=ag\ --nogrup\ --nocolor |
| 41 | +endif |
| 42 | + |
| 43 | +let s:ctags_bin = get(g:, 'ctags_bin', 'ctags') |
| 44 | +let g:fzf_tags_command = s:ctags_bin . ' -R' |
| 45 | +let g:fzf_layout = { 'down': '~40%' } |
| 46 | +let g:fzf_colors = { |
| 47 | + \ 'fg': ['fg', 'Normal'], |
| 48 | + \ 'bg': ['bg', 'Normal'], |
| 49 | + \ 'hl': ['fg', 'Comment'], |
| 50 | + \ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'], |
| 51 | + \ 'bg+': ['bg', 'CursorLine', 'CursorColumn'], |
| 52 | + \ 'hl+': ['fg', 'Statement'], |
| 53 | + \ 'info': ['fg', 'PreProc'], |
| 54 | + \ 'border': ['fg', 'Ignore'], |
| 55 | + \ 'prompt': ['fg', 'Conditional'], |
| 56 | + \ 'pointer': ['fg', 'Exception'], |
| 57 | + \ 'marker': ['fg', 'Keyword'], |
| 58 | + \ 'spinner': ['fg', 'Label'], |
| 59 | + \ 'header': ['fg', 'Comment'] |
| 60 | + \ } |
| 61 | + |
| 62 | +nnoremap <silent> <leader>ff :FzfFilesWithDevIcon<cr> |
| 63 | +nnoremap <silent> <leader>bb :Buffers<cr> |
| 64 | +
|
| 65 | +" advanced customization using autoload functions |
| 66 | +inoremap <expr> <C-x><C-k> fzf#vim#complete#word({'left': '15%'}) |
| 67 | +
|
| 68 | +"" |
| 69 | +" @private |
| 70 | +" Get file and devicons |
| 71 | +" From: https://github.com/ryanoasis/vim-devicons/issues/106 |
| 72 | +function! s:FzfFilesWithDevIcons() abort |
| 73 | + if executable('bat') |
| 74 | + let l:fzf_files_options = printf('--preview "bat %s %s | head -%s"', |
| 75 | + \ '--style=numbers,changes --color always', |
| 76 | + \ exists('*WebDevIconsGetFileTypeSymbol') ? '{2..-1}' : '{}', |
| 77 | + \ &lines) |
| 78 | + else |
| 79 | + let l:fzf_files_options = '' |
| 80 | + endif |
| 81 | + |
| 82 | + function! s:GetFiles() |
| 83 | + let l:files = split(system($FZF_DEFAULT_COMMAND), "\n") |
| 84 | + return <sid>PrependIcon(l:files) |
| 85 | + endfunction |
| 86 | + |
| 87 | + function! s:PrependIcon(candidates) |
| 88 | + let l:result = [] |
| 89 | + for candidate in a:candidates |
| 90 | + let l:fname = fnamemodify(candidate, ':p:t') |
| 91 | + if exists('*WebDevIconsGetFileTypeSymbol') |
| 92 | + let l:icon = WebDevIconsGetFileTypeSymbol(l:fname, isdirectory(l:fname)) |
| 93 | + call add(l:result, printf('%s %s', l:icon, candidate)) |
| 94 | + else |
| 95 | + call add(l:result, candidate) |
| 96 | + endif |
| 97 | + endfor |
| 98 | + return l:result |
| 99 | + endfunction |
| 100 | + |
| 101 | + function! s:EditFile(item) |
| 102 | + let l:pos = stridx(a:item, ' ') |
| 103 | + let l:file_path = a:item[pos+1:-1] |
| 104 | + execute 'silent e' l:file_path |
| 105 | + endfunction |
| 106 | + |
| 107 | + call fzf#run({ |
| 108 | + \ 'source': <sid>GetFiles(), |
| 109 | + \ 'sink': function('s:EditFile'), |
| 110 | + \ 'options': '-m ' . l:fzf_files_options, |
| 111 | + \ 'down': '40%' }) |
| 112 | +endfunction |
| 113 | +command! FzfFilesWithDevIcons call <sid>FzfFilesWithDevIcons() |
| 114 | + |
| 115 | +" similarly, we can apply it to fzf#vim#grep. To use ripgrep instead of ag: |
| 116 | +command! -bang -nargs=* Rg |
| 117 | + \ call fzf#vim#grep( |
| 118 | + \ 'rg --column --line-number --no-heading --color=always --smart-case '.shellescape(<q-args>), 1, |
| 119 | + \ <bang>0 ? fzf#vim#with_preview('up:60%') |
| 120 | + \ : fzf#vim#with_preview('right:50%:hidden', '?'), |
| 121 | + \ <bang>0) |
| 122 | + |
| 123 | +" command for git grep |
| 124 | +" - fzf#vim#grep(command, with_column, [options], [fullscreen]) |
| 125 | +command! -bang -nargs=* GGrep |
| 126 | + \ call fzf#vim#grep( |
| 127 | + \ 'git grep --line-number '.shellescape(<q-args>), 0, |
| 128 | + \ { 'dir': systemlist('git rev-parse --show-toplevel')[0] }, <bang>0) |
| 129 | + |
| 130 | +" augmenting Ag command using fzf#vim#with_preview function |
| 131 | +" * fzf#vim#with_preview([[options], [preview window], [toggle keys...]]) |
| 132 | +" * For syntax-highlighting, Ruby and any of the following tools are required: |
| 133 | +" - Bat: https://github.com/sharkdp/bat |
| 134 | +" - Highlight: http://www.andre-simon.de/doku/highlight/en/highlight.php |
| 135 | +" - CodeRay: http://coderay.rubychan.de/ |
| 136 | +" - Rouge: https://github.com/jneen/rouge |
| 137 | +" |
| 138 | +" :Ag - Start fzf with hidden preview window that can be enabled with "?" key |
| 139 | +" :Ag! - Start fzf in fullscreen and display the preview window above |
| 140 | +command! -bang -nargs=* Ag |
| 141 | + \ call fzf#vim#ag(<q-args>, |
| 142 | + \ <bang>0 ? fzf#vim#with_preview('up:60%') |
| 143 | + \ : fzf#vim#with_preview('right:50%:hidden', '?'), |
| 144 | + \ <bang>0) |
| 145 | + |
| 146 | +" vim: set sw=2 ts=2 et tw=78 : |
0 commit comments