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