Today I wrote this little utility function to resize my current window to a comfortable width:

-- Resizes the current window to the maximum required width
function M.resize_window()
    local padding = 7
    local lines_count = vim.api.nvim_buf_line_count(0)
    local lines = vim.api.nvim_buf_get_lines(0, 0, lines_count, false)

    local longerLine = 0
    for _, line in ipairs(lines) do
        local new_len = string.len(line) or 0
        if new_len > longerLine then
            longerLine = new_len
    local cmd = (longerLine + padding) .. "wincmd |"

As you can see, I just hardcoded some padding to a value of 7. Any way to know what value should I put as padding instead of that fixed 7 ? I guess this has to do with decorations like line-numbers, relative numbers and that stuff.

  • This gets you the effective window width: stackoverflow.com/q/26315925/789222 You may want to use this and compare to the winwidth() to calculate a padding (or just use a hard-coded value, if this is comfortable for you) Commented Apr 27, 2022 at 5:58
  • I think I get a decent enough solution doing something like that, so thanks
    – Danielo515
    Commented Apr 28, 2022 at 6:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.