Code Monkey home page Code Monkey logo

fzf-preview.vim's Introduction

fzf-preview.vim

fzf-preview is a Neovim plugin that provides a preset of commands using fzf.
Provides multiple resources and a preview command for it.

fzf-preview mainly uses neovim floating window. vim may work depending on the setting, but it is not recommended.

This plugin does not use fzf.vim but uses the library attached to fzf. Though it is different from this plugin and has a lot of functions, fzf.vim has no preview of the project's file list and grep on the interactive project.

Features

  1. Fzf can be operated using floating window (or any layout).
  2. Real time preview of the selected item.
  3. Fast file and buffer search using fuzzy match.
  4. Search through all the project files and history.
  5. Searching from file history file using oldfiles or mru.
  6. File search from git status with diff preview.
  7. Jump line from jumplist and changelist.
  8. Interactive grep from current project.
  9. Highlight code in preview with bat. (Optional)
  10. Export fzf candidates to QuickFix.
  11. Execute callback function for select item.

Demo

Project Files

fzf-preview

Git Status (Integrate fugitive)

fzf-preview

Project Grep

fzf-preview

Export QuickFix

fzf-preview

Requirements

Optional

Functional

Appearance

When bat is installed you can highlight the preview and see it.

Otherwise, head will be used

Installation

Use Dein, vim-plug or any Vim plugin manager of your choice.

If you are using MacOS and installed fzf using Homebrew suffice:

set  runtimepath+=/usr/local/opt/fzf
call dein#add('yuki-ycino/fzf-preview.vim')

You install fzf as well using Dein:

call dein#add('junegunn/fzf', { 'build': './install --all', 'merged': 0 })
call dein#add('yuki-ycino/fzf-preview.vim')

Usage

Command

:FzfPreviewProjectFiles                      " Select project files

:FzfPreviewGitFiles                          " Select file from git ls-files

:FzfPreviewDirectoryFiles {path or none}     " Select file from directory files (default to current working directory) (Required [ripgrep](https://github.com/BurntSushi/ripgrep))

:FzfPreviewGitStatus                         " Select git status listed file

:FzfPreviewBuffers                           " Select file buffers

:FzfPreviewAllBuffers                        " Select all buffers

:FzfPreviewProjectOldFiles                   " Select project files from oldfiles

:FzfPreviewProjectMruFiles                   " Select project mru (Most Recently Used) files

:FzfPreviewProjectMrwFiles                   " Select project mrw (Most Recently Written) files

:FzfPreviewProjectGrep {word}                " Grep project files from args word (Required [Python3](https://www.python.org/))

:FzfPreviewProjectCommandGrep {word or none} " Grep project files with advanced ripgrep integration (not fuzzy find) Ref: [Advanced ripgrep integration](https://github.com/junegunn/fzf.vim#example-advanced-ripgrep-integration) (Required [Python3](https://www.python.org/))

:FzfPreviewCtags                             " Select tags from tags file (Required [universal-ctags](https://github.com/universal-ctags/ctags) and [Python3](https://www.python.org/))

:FzfPreviewBufferTags                        " Select tags from current files (Required [universal-ctags](https://github.com/universal-ctags/ctags) and [Python3](https://www.python.org/))

:FzfPreviewOldFiles                          " Select files from oldfiles

:FzfPreviewMruFiles                          " Select mru (Most Recently Used) files

:FzfPreviewMrwFiles                          " Select mrw (Most Recently Written) files

:FzfPreviewQuickFix                          " Select line from QuickFix (Required [Python3](https://www.python.org/))

:FzfPreviewLocationList                      " Select line from LocationList (Required [Python3](https://www.python.org/))

:FzfPreviewLines                             " Select line from current buffer (Required [Python3](https://www.python.org/))

:FzfPreviewBufferLines                       " Select line from loaded buffer (Required [Python3](https://www.python.org/))

:FzfPreviewJumps                             " Select jumplist item (Required [Python3](https://www.python.org/))

:FzfPreviewChanges                           " Select changelist item (Required [Python3](https://www.python.org/))

:FzfPreviewMarks                             " Select mark (Required [Python3](https://www.python.org/))

:FzfPreviewConflict                          " Select conflict (Required [Python3](https://www.python.org/))

:FzfPreviewFromResources                     " Select files from selected resources (project, git, directory, buffer, project_old, project_mru, project_mrw, old, mru, mrw)

Recommended mappings

nmap <Leader>f [fzf-p]
xmap <Leader>f [fzf-p]

nnoremap <silent> [fzf-p]p     :<C-u>FzfPreviewFromResources project_mru git<CR>
nnoremap <silent> [fzf-p]gs    :<C-u>FzfPreviewGitStatus<CR>
nnoremap <silent> [fzf-p]b     :<C-u>FzfPreviewBuffers<CR>
nnoremap <silent> [fzf-p]B     :<C-u>FzfPreviewAllBuffers<CR>
nnoremap <silent> [fzf-p]o     :<C-u>FzfPreviewFromResources buffer project_mru<CR>
nnoremap <silent> [fzf-p]<C-o> :<C-u>FzfPreviewJumps<CR>
nnoremap <silent> [fzf-p]g;    :<C-u>FzfPreviewChanges<CR>
nnoremap <silent> [fzf-p]/     :<C-u>FzfPreviewLines -add-fzf-arg=--no-sort -add-fzf-arg=--query="'"<CR>
nnoremap <silent> [fzf-p]*     :<C-u>FzfPreviewLines -add-fzf-arg=--no-sort -add-fzf-arg=--query="'<C-r>=expand('<cword>')<CR>"<CR>
nnoremap          [fzf-p]gr    :<C-u>FzfPreviewProjectGrep<Space>
xnoremap          [fzf-p]gr    "sy:FzfPreviewProjectGrep<Space>-F<Space>"<C-r>=substitute(substitute(@s, '\n', '', 'g'), '/', '\\/', 'g')<CR>"
nnoremap <silent> [fzf-p]t     :<C-u>FzfPreviewBufferTags<CR>
nnoremap <silent> [fzf-p]q     :<C-u>FzfPreviewQuickFix<CR>
nnoremap <silent> [fzf-p]l     :<C-u>FzfPreviewLocationList<CR>

Fzf window Keymaps

<C-g>, <Esc>
  Cancel fzf

<C-x>
  Open split

<C-v>
  Open vsplit

<C-t>
  Open tabedit

<C-q>
  Build QuickFix

<C-d>
  Preview page down

<C-u>
  Preview page up

?
  Toggle display of preview screen

DEPRECATED
<C-s> (Neovim Only)
  Toggle window size of fzf, normal size and full-screen

Customization

Example of integrate Fugitive

nnoremap <silent> [fzf-p]gs :<C-u>FzfPreviewGitStatus -processors=g:fzf_preview_fugitive_processors<CR>

augroup fzf_preview
  autocmd!
  autocmd User fzf_preview#initialized call s:fzf_preview_settings()
augroup END

function! s:fugitive_add(paths) abort
  for path in a:paths
    execute 'silent G add ' . path
  endfor
  echomsg 'Git add ' . join(a:paths, ', ')
endfunction

function! s:fugitive_reset(paths) abort
  for path in a:paths
    execute 'silent G reset ' . path
  endfor
  echomsg 'Git reset ' . join(a:paths, ', ')
endfunction

function! s:fugitive_patch(paths) abort
  for path in a:paths
    execute 'silent tabedit ' . path . ' | silent Gdiff'
  endfor
  echomsg 'Git add --patch ' . join(a:paths, ', ')
endfunction

function! s:fzf_preview_settings() abort
  let g:fzf_preview_fugitive_processors = fzf_preview#resource_processor#get_processors()
  let g:fzf_preview_fugitive_processors['ctrl-a'] = function('s:fugitive_add')
  let g:fzf_preview_fugitive_processors['ctrl-r'] = function('s:fugitive_reset')
  let g:fzf_preview_fugitive_processors['ctrl-c'] = function('s:fugitive_patch')
endfunction

Optional Configuration Tips

  • Increase the size of file history:
" oldfiles uses viminfo, but the default setting is 100
" Change the number by setting it in viminfo with a single quote.
" Ref: viminfo-'
set viminfo='1000
  • Set values for each variable. The default settings are as follows.
" Add fzf quit mapping
let g:fzf_preview_quit_map = 1

" Use floating window (for neovim)
let g:fzf_preview_use_floating_window = 1

" floating window size ratio
let g:fzf_preview_floating_window_rate = 0.9

" floating window winblend value
let g:fzf_preview_floating_window_winblend = 15

" Commands used for fzf preview.
" The file name selected by fzf becomes {}
let g:fzf_preview_command = 'head -100 {-1}'                       " Not installed bat
" let g:fzf_preview_command = 'bat --color=always --style=grid {-1}' " Installed bat

" g:fzf_binary_preview_command is executed if this command succeeds, and g:fzf_preview_command is executed if it fails
let g:fzf_preview_if_binary_command = '[[ "$(file --mime {})" =~ binary ]]'

" Commands used for binary file
let g:fzf_binary_preview_command = 'echo "{} is a binary file"'

" Commands used to get the file list from project
let g:fzf_preview_filelist_command = 'git ls-files --exclude-standard'               " Not Installed ripgrep
" let g:fzf_preview_filelist_command = 'rg --files --hidden --follow --no-messages -g \!"* *"' " Installed ripgrep

" Commands used to get the file list from git reposiroty
let g:fzf_preview_git_files_command = 'git ls-files --exclude-standard'

" Commands used to get the file list from current directory
let g:fzf_preview_directory_files_command = 'rg --files --hidden --follow --no-messages -g \!"* *"'

" Commands used to get the git status file list
let g:fzf_preview_git_status_command = "git status --short --untracked-files=all | awk '{if (substr($0,2,1) !~ / /) print $2}'"

" Commands used for git status preview.
let g:fzf_preview_git_status_preview_command =  "[[ $(git diff -- {-1}) != \"\" ]] && git diff --color=always -- {-1} || " .
\ "[[ $(git diff --cached -- {-1}) != \"\" ]] && git diff --cached --color=always -- {-1} || " .
\ g:fzf_preview_command

" Commands used for project grep
let g:fzf_preview_grep_cmd = 'rg --line-number --no-heading'

" Commands used for current file lines
let g:fzf_preview_lines_command = 'cat'
" let g:fzf_preview_lines_command = 'bat --color=always --style=grid --theme=ansi-dark --plain'

" Commands used for preview of the grep result
let g:fzf_preview_grep_preview_cmd = expand('<sfile>:h:h') . '/bin/preview_fzf_grep'

" Cache directory for mru and mrw
let g:fzf_preview_cache_directory = expand('~/.cache/vim/fzf_preview')

" Keyboard shortcuts while fzf preview is active
let g:fzf_preview_preview_key_bindings = 'ctrl-d:preview-page-down,ctrl-u:preview-page-up,?:toggle-preview'

" Specify the color of fzf
let g:fzf_preview_fzf_color_option = ''

" Set the processors when selecting an element with fzf
" Do not use with g:fzf_preview_*_key_map
let g:fzf_preview_custom_default_processors = {}
" For example, set split to ctrl-s
" let g:fzf_preview_custom_default_processors = fzf_preview#resource_processor#get_default_processors()
" call remove(g:fzf_preview_custom_default_processors, 'ctrl-x')
" let g:fzf_preview_custom_default_processors['ctrl-s'] = function('fzf_preview#resource_processor#split')

" Use as fzf preview-window option
let g:fzf_preview_fzf_preview_window_option = ''
" let g:fzf_preview_fzf_preview_window_option = 'up:30%'

" Command to be executed after file list creation
let g:fzf_preview_filelist_postprocess_command = ''
" let g:fzf_preview_filelist_postprocess_command = 'xargs -d "\n" ls -U --color'      " Use dircolors
" let g:fzf_preview_filelist_postprocess_command = 'xargs -d "\n" exa --color=always' " Use exa
" on Mac
" let g:fzf_preview_filelist_postprocess_command = 'gxargs -d "\n" exa --color=always' "use exa


" Use vim-devicons
let g:fzf_preview_use_dev_icons = 0

" devicons character width
let g:fzf_preview_dev_icon_prefix_length = 2

" DEPRECATED
" Use g:fzf_preview_custom_default_processors
" Keyboard shortcut for opening files with split
let g:fzf_preview_split_key_map = 'ctrl-x'

" DEPRECATED
" Use g:fzf_preview_custom_default_processors
" Keyboard shortcut for opening files with vsplit
let g:fzf_preview_vsplit_key_map = 'ctrl-v'

" DEPRECATED
" Use g:fzf_preview_custom_default_processors
" Keyboard shortcut for opening files with tabedit
let g:fzf_preview_tabedit_key_map = 'ctrl-t'

" DEPRECATED
" Use g:fzf_preview_custom_default_processors
" Keyboard shortcut for building quickfix
let g:fzf_preview_build_quickfix_key_map = 'ctrl-q'

" DEPRECATED
" fzf window layout
let g:fzf_preview_layout = 'top split new'

" DEPRECATED
" Rate of fzf window
let g:fzf_preview_rate = 0.3

" DEPRECATED
" Key to toggle fzf window size of normal size and full-screen
let g:fzf_full_preview_toggle_key = '<C-s>'

Command Options

-processors
" Set processor when selecting element with fzf started by this command.
" Value must be a global variable name.
" Variable is dictionary and format is same as g:fzf_preview_custom_default_processors.
"
" Most commands are passed a file path to the processor function.
" FzfPreviewAllBuffers will be passed “buffer {bufnr}”
"
" Value example: let g:foo_processors = {
"                \ '':       function('fzf_preview#resource_processor#edit'),
"                \ 'ctrl-x': function('s:foo_function'),
"                \ }
"

" Example: 'bdelete!' buffers

augroup fzf_preview
  autocmd!
  autocmd User fzf_preview#initialized call s:fzf_preview_settings()
augroup END

function! s:fzf_preview_settings() abort
  let g:fzf_preview_buffer_delete_processors = fzf_preview#resource_processor#get_default_processors()
  let g:fzf_preview_buffer_delete_processors['ctrl-x'] = function('s:buffers_delete_from_lines')
endfunction

function! s:buffers_delete_from_lines(lines) abort
  for line in a:lines
    let matches = matchlist(line, '^buffer \(\d\+\)$')
    if len(matches) >= 1
      execute 'bdelete! ' . matches[1]
    else
      execute 'bdelete! ' . line
    endif
  endfor
endfunction

nnoremap <silent> <Leader>b :<C-u>FzfPreviewBuffers -processors=g:fzf_preview_buffer_delete_processors<CR>


-add-fzf-arg
" Set the arguments to be passed when executing fzf.
" This value is added to the default options.
" Value must be a string without spaces.

" Example: Exclude filename with FzfPreviewProjectGrep
nnoremap <Leader>g :<C-u>FzfPreviewProjectGrep -add-fzf-arg=--nth=3<Space>


-resume
" Reuse the input that was last used to select the element with fzf.
" Do not need to pass a value for this option.

" Example: Reuse last query for project grep.
nnoremap <Leader>G :<C-u>FzfPreviewProjectGrep -resume<Space>


" EXPERIMENTAL: Specifications may change.
-overwrite-fzf-args
" Set the arguments to be passed when executing fzf.
" Value must be a global variable name.
" Variable is string and format is shell command options.
" This option is experimental.
"
" Value example: let g:foo_arguments = '--multi --reverse --ansi --bind=ctrl-d:preview-page-down,ctrl-u:preview-page-up,?:toggle-preview'
"

" Example: Exclude filename with FzfPreviewProjectGrep

augroup fzf_preview
  autocmd!
  autocmd User fzf_preview#initialized call s:fzf_preview_settings()
augroup END

function! s:fzf_preview_settings() abort
  let g:fzf_preview_grep_command_options = fzf_preview#command#get_common_command_options()
  let g:fzf_preview_grep_command_options = g:fzf_preview_grep_command_options . ' --nth=3'
endfunction

nnoremap <Leader>g :<C-u>FzfPreviewProjectGrep -overwrite-fzf-args=g:fzf_preview_grep_command_options<Space>

Function

" Function to display the floating window used by this plugin
call fzf_preview#window#create_centered_floating_window()

" Example
call fzf#run({
\ 'source':  files,
\ 'sink':   'edit',
\ 'window': 'call fzf_preview#window#create_centered_floating_window()',
\ })

" Get the initial value of the process executed when selecting the element of fzf
call fzf_preview#resource_processor#get_default_processors()

" Get the current value of the process executed when selecting the element of fzf
" Use in fzf_preview#initialized event.
call fzf_preview#resource_processor#get_processors()

" EXPERIMENTAL: Specifications may change.
" Get the common value of the passed when executed fzf.
" Use in fzf_preview#initialized event.
call fzf_preview#command#get_common_command_options()

Inspiration

License

The MIT License (MIT)

fzf-preview.vim's People

Contributors

cirnatdan avatar glepnir avatar guillaumewuip avatar heimann avatar kdheepak avatar pappasam avatar ripperapid avatar tsuyoshicho avatar yuki-yano avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.