DEV Community

Tyler Smith
Tyler Smith

Posted on • Edited on

Change Fugitive's number of Git context lines in Neovim

I recently started using Neovim after trying kickstart.nvim. I've spent years managing Git through VS Code's built-in Git client, and I wanted to find something similar for Neovim. I landed on Fugitive by Tim Pope. Fugitive had a tight integration with the editor, and it didn't require me to constantly jump to another window in tmux.

I had one big problem with fugitive: it only showed me 6 lines of context before and after each change. In VS Code, I had the built-in Git client configured to show me the entire file so I could make sure there wasn't old code that needed to be cleaned up as a result of the new changes.

It turns out that the configuration for the number of diff context lines is controlled by the editor itself opposed to Fugitive.

Changing Neovim's number of diff context lines

If you run Fugitive's :Gvdiffsplit command on a file that has uncommitted changes in a Git repo, Fugitive will show six context lines above and below each change. You can modify the number of context lines in realtime by running the following command:

:set diffopt+=context:10
Enter fullscreen mode Exit fullscreen mode

Without even reloading the diff, you'll see the number of context lines change from 6 to 10. But this will be restored to the default configuration once Neovim is closed.

So how can we change the default settings for future sessions in Neovim? You'll need to add the following line somewhere in your Lua config, either in your init.lua config file or a file that is reference by it.

-- Add the following to init.lua
vim.opt.diffopt:append 'context:1000'
Enter fullscreen mode Exit fullscreen mode

Change 1000 to whatever number of context lines you'd like, then restart Neovim. The next time you run any of Fugitive's diff commands, it will display up to 1000 lines of context before and after each change.

If you're using an init.vim file instead of init.lua (or you're using Vim and are using .vimrc), you can add the following to your Vimscript file to accomplish the same thing:

" If you aren't using Lua, add the following to init.vim
" or .vimrc
set diffopt+=context:1000
Enter fullscreen mode Exit fullscreen mode

A big thanks to slack1994 on Reddit for showing the syntax for how to change diff options in Lua. For more about diff options, run :h diffopt (or read more here).

Top comments (0)