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
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@gbnaidu
Copy link

gbnaidu commented Sep 7, 2020

Thanks for the update to open the selected files in tab/split windows.

One thing I noticed is that this does not grep for the pattern to select the files. Instead it selects the files whose names match the given query string.

Any help to make it grep for the contents of the files rather than match name of the files?

Thanks

@acepukas
Copy link
Author

acepukas commented Sep 7, 2020

@gbnaidu For grepping file contents, I took a different approach. Not saying this is the best way but it's what has worked for me. Refer to this gist for details.

@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