Smart Window Resizing in Neovim
2025-04-12
I'm looking for work! (click to expand)
If you have questions or thoughts on the post I'd love to hear about it! I'm currently looking for work in the northeastern US. I'll be graduating with a BS in both Computer Science and Cyber Operations at the end of the school year. I have worked in web application development and Java, and I have experience in Rust, C++, Python, and various other languages. If you're curious about what I've done in the past, I keep up a short list of projects here or contact me to give me a place to send my resume. I figure anyone who reads my posts will have an interesting offer, so all leads are appreciated!
The last day or two, I've been refitting my Neovim windowing config. Nothing too wild, just fixing a few warts here and there. What follows is a few different snippets for different behaviors I wanted. Hopefully one of these is useful to someone, even if only as a reference (something definitely needs said about the Neovim documentation searchability situation). If you want to see these in their context, links will be left below each example. The links may fall out of date, so I've linked them to the specific commit.
Also, if you're interested in this, I credit chrisgrieser who used to run a blog called Nano Tips For Vim, which was immensely helpful to me in figuring out all of my Neovim configs but was sadly taken offline as of 2025-06-12.
Turn Off Line Numbers for Small Windows
local set_numbers = function(value, window)
vim.api.nvim_set_option_value("number", value, { win = window })
vim.api.nvim_set_option_value("relativenumber", value, { win = window })
end
local window_size_threshold = 40
vim.api.nvim_create_autocmd("WinResized", {
callback = function()
for _, win in ipairs(vim.api.nvim_list_wins()) do
local width = vim.api.nvim_win_get_width(win)
if width < window_size_threshold then
set_numbers(false, win)
else
set_numbers(true, win)
end
end
end,
})
Bring Window Into Focus
local win_focus = function()
-- Uncomment if you use either of these plugins
-- require("dapui").close()
-- require("nvim-tree.api").tree.close()
local current_win = vim.api.nvim_get_current_win()
local windows = vim.api.nvim_list_wins()
vim.api.nvim_win_set_width(0, vim.o.columns - (#windows * 20))
for _, win in ipairs(windows) do
if win ~= current_win then
vim.api.nvim_win_set_width(win, 20)
end
end
end
keymap("n", "<leader>rs", function()
vim.api.nvim_win_set_width(0, 20)
end)
keymap("n", "<leader>rf", win_focus)
Resizing Mappings
This requires the winresize.nvim plugin.
local win_resize = function(win, amt, dir)
return function()
require("winresize").resize(win, amt, dir)
end
end
wk_add("<leader>r", "Window Resize")
keymap("n", "<leader>rh", win_resize(0, 10, "left"))
keymap("n", "<leader>rj", win_resize(0, 3, "down"))
keymap("n", "<leader>rk", win_resize(0, 3, "up"))
keymap("n", "<leader>rl", win_resize(0, 10, "right"))
keymap("n", "<leader>r=", "<C-w>=", { desc = "Equalize Windows" })