delta
delta copied to clipboard
Using Delta as a Vim/NeoVim library
I've recently become very interested in the fact that the library used by vim and nvim for diffing, called xdiff, doesn't support in line diffs (at the word boundary) like your tool does. Obviously your tool isn't written to be a library, but I think if it were possible to create a vim or nvim plugin to use this tool to generate the two-way diffs used in the editor, it would be some massive exposure.
I don't think it even needs an API, cause vim/nvim can call an external program for diffing as far as I'm aware.
Does anyone know how doable this would be?
Hi @Sevensidedmarble, thanks for opening this. Have you seen my project doing this for Emacs (magit)? https://github.com/dandavison/magit-delta It might contain some parallels to what would be needed for vim/nvim.
It required very little code. What it relies on is an existing emacs-lisp library that can take raw text with ANSI color escape sequences as input, parse it, excise the escape sequences and annotate the strings in the Emacs buffer with the data structures that Emacs uses to add colors to text (i.e. "text properties" or "overlays").
In fact, I use delta in magit a lot and find it beneficial, so I do believe that it would be beneficial to make the same thing possible in vim/nvim. I'm happy to help where I can, but I don't know anything about vim/nvim. From a very quick google search what we want to do is similar to what is discussed in
https://unix.stackexchange.com/questions/7695/how-to-make-vim-display-colors-as-indicated-by-color-codes https://vi.stackexchange.com/questions/485/can-vim-interpret-terminal-color-escape-codes https://stackoverflow.com/questions/10592715/ansi-color-codes-in-vim/38357506
Does vim/nvim have the ability to run a shell and display colored output from the shell faithfully within the vim/nvim application? If there's something like that that works well, then it suggests that the required technology exists already.
As far as I'm aware, there's no such feature in (vanilla) Vim. There's a way to run a shell command and have its output inserted into the edited text, but the color escape codes won't be processed.
For example, if I do :r!ls -Al --color .git/
, Vim inserts the following verbatim into the text (:r!<command>
is the command to open a subshell and execute the command) :
insgesamt 20
drwxrwsr-x 1 kriss build 0 9. Sep 19:51 [0m[1;34mbranches[0m
-rw-rw-r-- 1 kriss build 262 9. Sep 19:51 config
-rw-rw-r-- 1 kriss build 73 9. Sep 19:51 description
-rw-rw-r-- 1 kriss build 23 9. Sep 19:51 HEAD
drwxrwsr-x 1 kriss build 460 9. Sep 19:51 [1;34mhooks[0m
-rw-rw-r-- 1 kriss build 289 9. Sep 19:51 index
drwxrwsr-x 1 kriss build 14 9. Sep 19:51 [1;34minfo[0m
drwxrwsr-x 1 kriss build 16 9. Sep 19:51 [1;34mlogs[0m
drwxrwsr-x 1 kriss build 52 9. Sep 19:51 [1;34mobjects[0m
-rw-rw-r-- 1 kriss build 114 9. Sep 19:51 packed-refs
drwxrwsr-x 1 kriss build 32 9. Sep 19:51 [1;34mrefs[0m
Note this is plain Vim. Maybe there's a plugin which can interpret the escape codes. Also, I don't know what this looks like with Neovim either.
They both have the option of using terminal buffers, which obviously parses all codes correctly. My gut tells me this would probably be the way to do it, I'm just not great with vimscript, or know enough about the nvim lua api, to say exactly how.
Is anyone going to do this plugin(or have done)? I wanna try it out, otherwise, I will complete it in neovim after the documents of two personal plugins are done.(Not a promise)
I find this issue now and wonder what @Sevensidedmarble and @kevinhwang91 wants is like this?

This could be done by fugitive.vim.
If you would like to just print, not opening on editor, just :bo term git diff
(in the case you set core.pager = delta)

I find this issue now and wonder what @Sevensidedmarble and @kevinhwang91 wants is like this?
![]()
This could be done by fugitive.vim.
If you would like to just print, not opening on editor, just
:bo term git diff
(in the case you set core.pager = delta)![]()
Thanks, I knew this information before commented. There're two reasons I wanna build a plugin library based on fugitive.vim which is easier to implement:
- I prefer a floating window for preview rather than a tab or a splited window while navigating commit, more like modern T/GUI git tools, such as lazygit, tig, gitui and so on.
- I also use vim-flog, extra library is better than a simple script based on fugitive.vim.
neovim is migrating from vimscript to lua, I'm following their steps and learning lua to refactor my private config and plugin. Not enough time to make this plugin recently.
@kevinhwang91
I prefer a floating window
This could be done by fzf-preview.vim. It's already integrated with fugitive and can custom preview.
git diff
is not only to a commit, but also to a file. Run :Git
with fugitive in vim, you will know my idea if you have an experience for using IDE for code review before committing.
I only use fzf preview git commit in the current buffer.
@kevinhwang91
You can do git diff
to file as well.
Use CocCommand fzf-preview.GitStatus
with config below as I said before.
let g:fzf_preview_git_status_preview_command = "[[ $(git diff --cached -- {-1}) != \"\" ]] && git diff --cached --color=always -- {-1} | delta || " .
\ "[[ $(git diff -- {-1}) != \"\" ]] && git diff --color=always -- {-1} | delta || " .
\ g:fzf_preview_command

@kevinhwang91 You can do
git diff
to file as well. UseCocCommand fzf-preview.GitStatus
with config below as I said before.let g:fzf_preview_git_status_preview_command = "[[ $(git diff --cached -- {-1}) != \"\" ]] && git diff --cached --color=always -- {-1} | delta || " . \ "[[ $(git diff -- {-1}) != \"\" ]] && git diff --color=always -- {-1} | delta || " . \ g:fzf_preview_command
![]()
Pretty cool, I think I have to try for a while, but I really hate the redraw in FZF during the interaction.
Update:
Someone who saw my example on reddit sent a request to fzf.vim, and now fzf also implements delta.
https://github.com/junegunn/fzf.vim/issues/1202
cc: @dandavison , @Sevensidedmarble
A similar question was asked on tpope/vim-fugitive
here: https://github.com/tpope/vim-fugitive/issues/2249#issuecomment-1874566335
TL;DR
:Git -c pager.diff=delta diff