Skip to content

Instantly share code, notes, and snippets.

@acepukas
Forked from danmikita/init.vim
Last active April 16, 2021 19:54
Show Gist options
  • Save acepukas/e4a13c34d9ce487a2ca4dfe7c6a48dce to your computer and use it in GitHub Desktop.
Save acepukas/e4a13c34d9ce487a2ca4dfe7c6a48dce to your computer and use it in GitHub Desktop.
File preview with FZF, RG, Bat, and Devicons (supports multiple file open and opening files in splits and tabs)
" ripgrep
if executable('rg')
let $FZF_DEFAULT_COMMAND = 'rg --files --hidden --follow --glob "!.git/*"'
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" '.shellescape(<q-args>).'| tr -d "\017"', 1, <bang>0)
" Overriding fzf.vim's default :Files command.
" Pass zero or one args to Files command (which are then passed to Fzf_dev). Support file path completion too.
command! -nargs=? -complete=file Files call Fzf_dev(<q-args>)
nnoremap <silent> <leader>e :Files<CR>
endif
" Files + devicons
function! Fzf_dev(qargs)
let l:fzf_files_options = '--preview "bat --theme="OneHalfDark" --style=numbers,changes --color always {2..-1} | head -'.&lines.'" --expect=ctrl-t,ctrl-v,ctrl-x --multi --bind=ctrl-a:select-all,ctrl-d:deselect-all'
function! s:files(dir)
let l:cmd = $FZF_DEFAULT_COMMAND
if a:dir != ''
let l:cmd .= ' ' . shellescape(a:dir)
endif
let l:files = split(system(l:cmd), '\n')
return s:prepend_icon(l:files)
endfunction
function! s:prepend_icon(candidates)
let l:result = []
for l:candidate in a:candidates
let l:filename = fnamemodify(l:candidate, ':p:t')
let l:icon = WebDevIconsGetFileTypeSymbol(l:filename, isdirectory(l:filename))
call add(l:result, printf('%s %s', l:icon, l:candidate))
endfor
return l:result
endfunction
function! s:edit_file(lines)
if len(a:lines) < 2 | return | endif
let l:cmd = get({'ctrl-x': 'split',
\ 'ctrl-v': 'vertical split',
\ 'ctrl-t': 'tabe'}, a:lines[0], 'e')
for l:item in a:lines[1:]
let l:pos = stridx(l:item, ' ')
let l:file_path = l:item[pos+1:-1]
execute 'silent '. l:cmd . ' ' . l:file_path
endfor
endfunction
call fzf#run({
\ 'source': <sid>files(a:qargs),
\ 'sink*': function('s:edit_file'),
\ 'options': '-m ' . l:fzf_files_options,
\ 'down': '40%' })
endfunction
@zzandland
Copy link

I've been trying to do this for whole day. Thanks!

@tmwatchanan
Copy link

Appreciate your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment