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).

Image of Bright Data

Access Niche Markets with Ease – Unlock restricted market data with precision.

Get access to hard-to-reach data with our specialized proxy services designed for niche markets.

Access Markets

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay