You can remove padding around Neovim instance with this one simple trick…

neovim
reddit
Author

Evgeni Chasnovski

Published

August 1, 2024

Originally posted on Reddit

Left: with "frame" from terminal emulator; Right: without that "frame"

(Sorry for a slightly clickbait-y title. Always wanted to use one of those :) )

If you have different background color in your terminal emulator and Neovim, then chances are that you experience this weird “frame” around your Neovim instance. Like the one shown in the left part of the picture.

This is because CLI programs occupy screen estate based on the cell grid with cells having same width and height. If pixel dimension(s) of terminal emulator’s window are not multiple of cell pixel dimension(s), there is a gap between edge(s) of rendered CLI program and window edge(s).

Usual answers to this issue are:


As it turns out, this can be solved by keeping terminal background’s color in sync with Neovim’s background color. This is possible thanks to a dark magic called “Operating System Commands XTerm Control Sequences” or OSC control sequences for short. In particular, OSC 11 and OSC 111, which your terminal should support (most modern feature rich ones do: Kitty, WezTerm, Alacritty, etc.).

Just add the following snippet to your ‘init.lua’ (credit to u/gpanders from this comment):

vim.api.nvim_create_autocmd({ "UIEnter", "ColorScheme" }, {
  callback = function()
    local normal = vim.api.nvim_get_hl(0, { name = "Normal" })
    if not normal.bg then return end
    io.write(string.format("\027]11;#%06x\027\\", normal.bg))
  end,
})

vim.api.nvim_create_autocmd("UILeave", {
  callback = function() io.write("\027]111\027\\") end,
})

And that’s it. It synchronizes on every enter/exit Neovim instance and after loading new color scheme. And it even works with <C-z> and later fg! Couple of caveats, though:


Also, if you want a slightly more robust, maintained, and tested version, there is now a new setup_termbg_sync() in ‘mini.misc’ module of ‘mini.nvim’. It also checks if OSC 11 is supported by terminal emulator, uses only it without OSC 111, and synchronizes immediately.