Skip to content

Commit

Permalink
[GF?] Use delta to format git diff if available
Browse files Browse the repository at this point in the history
Close #1202
  • Loading branch information
junegunn committed Dec 11, 2020
1 parent 636a62f commit 97ec188
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ so you can omit it if you use a plugin manager that doesn't support hooks.

- [fzf][fzf-main] 0.23.0 or above
- For syntax-highlighted preview, install [bat](https://github.com/sharkdp/bat)
- `:GF?` will use [delta](https://github.com/dandavison/delta) if available
to format `git diff` output
- `Ag` requires [The Silver Searcher (ag)][ag]
- `Rg` requires [ripgrep (rg)][rg]
- `Tags` and `Helptags` require Perl
Expand Down
8 changes: 7 additions & 1 deletion autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,16 @@ function! fzf#vim#gitfiles(args, ...)
" Here be dragons!
" We're trying to access the common sink function that fzf#wrap injects to
" the options dictionary.
let preview = printf(
\ 'sh -c "if [[ {1} =~ M ]]; then %s; else %s {-1}; fi"',
\ executable('delta')
\ ? 'git diff -- {-1} | delta --file-style=omit | sed 1d'
\ : 'git diff --color=always -- {-1} | sed 1,4d',
\ s:bin.preview)
let wrapped = fzf#wrap({
\ 'source': 'git -c color.status=always status --short --untracked-files=all',
\ 'dir': root,
\ 'options': ['--ansi', '--multi', '--nth', '2..,..', '--tiebreak=index', '--prompt', 'GitFiles?> ', '--preview', 'sh -c "(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -1000"']
\ 'options': ['--ansi', '--multi', '--nth', '2..,..', '--tiebreak=index', '--prompt', 'GitFiles?> ', '--preview', preview]
\})
call s:remove_layout(wrapped)
let wrapped.common_sink = remove(wrapped, 'sink*')
Expand Down
44 changes: 23 additions & 21 deletions doc/fzf-vim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fzf-vim.txt fzf-vim Last change: October 22 2020
fzf-vim.txt fzf-vim Last change: December 12 2020
FZF-VIM - TABLE OF CONTENTS *fzf-vim* *fzf-vim-toc*
==============================================================================

Expand Down Expand Up @@ -92,14 +92,16 @@ so you can omit it if you use a plugin manager that doesn't support hooks.

- {fzf}{1} 0.23.0 or above
- For syntax-highlighted preview, install {bat}{5}
- `Ag` requires {The Silver Searcher (ag)}{6}
- `Rg` requires {ripgrep (rg)}{7}
- `:GF?` will use {delta}{6} if available to format `git diff` output
- `Ag` requires {The Silver Searcher (ag)}{7}
- `Rg` requires {ripgrep (rg)}{8}
- `Tags` and `Helptags` require Perl

{1} https://github.com/junegunn/fzf
{5} https://github.com/sharkdp/bat
{6} https://github.com/ggreer/the_silver_searcher
{7} https://github.com/BurntSushi/ripgrep
{6} https://github.com/dandavison/delta
{7} https://github.com/ggreer/the_silver_searcher
{8} https://github.com/BurntSushi/ripgrep


COMMANDS *fzf-vim-commands*
Expand All @@ -117,8 +119,8 @@ COMMANDS *fzf-vim-commands*
`:GFiles?` | Git files ( `git status` )
`:Buffers` | Open buffers
`:Colors` | Color schemes
`:Ag [PATTERN]` | {ag}{6} search result ( `ALT-A` to select all, `ALT-D` to deselect all)
`:Rg [PATTERN]` | {rg}{7} search result ( `ALT-A` to select all, `ALT-D` to deselect all)
`:Ag [PATTERN]` | {ag}{7} search result ( `ALT-A` to select all, `ALT-D` to deselect all)
`:Rg [PATTERN]` | {rg}{8} search result ( `ALT-A` to select all, `ALT-D` to deselect all)
`:Lines [QUERY]` | Lines in loaded buffers
`:BLines [QUERY]` | Lines in the current buffer
`:Tags [QUERY]` | Tags in the project ( `ctags -R` )
Expand All @@ -129,8 +131,8 @@ COMMANDS *fzf-vim-commands*
`:History` | `v:oldfiles` and open buffers
`:History:` | Command history
`:History/` | Search history
`:Snippets` | Snippets ({UltiSnips}{8})
`:Commits` | Git commits (requires {fugitive.vim}{9})
`:Snippets` | Snippets ({UltiSnips}{9})
`:Commits` | Git commits (requires {fugitive.vim}{10})
`:BCommits` | Git commits for the current buffer
`:Commands` | Commands
`:Maps` | Normal mode mappings
Expand All @@ -146,14 +148,14 @@ COMMANDS *fzf-vim-commands*
- You can set `g:fzf_command_prefix` to give the same prefix to the commands
- e.g. `let g:fzf_command_prefix = 'Fzf'` and you have `FzfFiles`, etc.

(1: `Helptags` will shadow the command of the same name from {pathogen}{10}.
(1: `Helptags` will shadow the command of the same name from {pathogen}{11}.
But its functionality is still available via `call pathogen#helptags()`.)

{6} https://github.com/ggreer/the_silver_searcher
{7} https://github.com/BurntSushi/ripgrep
{8} https://github.com/SirVer/ultisnips
{9} https://github.com/tpope/vim-fugitive
{10} https://github.com/tpope/vim-pathogen
{7} https://github.com/ggreer/the_silver_searcher
{8} https://github.com/BurntSushi/ripgrep
{9} https://github.com/SirVer/ultisnips
{10} https://github.com/tpope/vim-fugitive
{11} https://github.com/tpope/vim-pathogen


CUSTOMIZATION *fzf-vim-customization*
Expand Down Expand Up @@ -268,7 +270,7 @@ Want a preview window?
\ call fzf#vim#files(<q-args>, {'options': ['--layout=reverse', '--info=inline', '--preview', 'cat {}']}, <bang>0)
<
It kind of works, but you probably want a nicer previewer program than `cat`.
fzf.vim ships {a versatile preview script}{11} you can readily use. It
fzf.vim ships {a versatile preview script}{12} you can readily use. It
internally executes {bat}{5} for syntax highlighting, so make sure to install
it.
>
Expand All @@ -289,7 +291,7 @@ You can just omit the spec argument if you only want the previewer.
command! -bang -nargs=? -complete=dir Files
\ call fzf#vim#files(<q-args>, fzf#vim#with_preview(), <bang>0)
<
{11} bin/preview.sh
{12} bin/preview.sh
{5} https://github.com/sharkdp/bat


Expand All @@ -303,7 +305,7 @@ predefined `Ag` or `Rg` using `fzf#vim#grep`.
not print column numbers.
- We set the base directory to git root by setting `dir` attribute in spec
dictionary.
- {The preview script}{11} supports `grep` format (`FILE_PATH:LINE_NO:...`), so
- {The preview script}{12} supports `grep` format (`FILE_PATH:LINE_NO:...`), so
we can just wrap the spec with `fzf#vim#with_preview` as before to enable
previewer.
>
Expand All @@ -312,7 +314,7 @@ predefined `Ag` or `Rg` using `fzf#vim#grep`.
\ 'git grep --line-number -- '.shellescape(<q-args>), 0,
\ fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0)
<
{11} bin/preview.sh
{12} bin/preview.sh


Example: Rg command with preview window~
Expand Down Expand Up @@ -456,10 +458,10 @@ following exceptions:
STATUS LINE OF TERMINAL BUFFER *fzf-vim-status-line-of-terminal-buffer*
==============================================================================

When fzf starts in a terminal buffer (see {fzf/README-VIM.md}{12}), you may
When fzf starts in a terminal buffer (see {fzf/README-VIM.md}{13}), you may
want to customize the statusline of the containing buffer.

{12} https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzf-inside-terminal-buffer
{13} https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzf-inside-terminal-buffer


< Hide statusline >___________________________________________________________~
Expand Down

5 comments on commit 97ec188

@kevinhwang91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junegunn Did you miss this preview code?

fzf.vim/autoload/fzf/vim.vim

Lines 1223 to 1226 in 65b6cdb

if !s:is_win && &columns > s:wide
call extend(options.options,
\ ['--preview', 'echo {} | grep -o "[a-f0-9]\{7,\}" | head -1 | xargs git show --format=format: --color=always | head -1000'])
endif

@junegunn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit updates only :GF?, so I'm not missing anything. Are you saying that you want delta on :Commits as well?

@kevinhwang91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It's so inconsistent for delta user.

@junegunn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 54b3817

@kevinhwang91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're super-efficient.

Please sign in to comment.