0

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
        end
    end
    local cmd = (longerLine + padding) .. "wincmd |"
    vim.api.nvim_command(cmd)
end

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.

2
  • 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) Apr 27, 2022 at 5:58
  • I think I get a decent enough solution doing something like that, so thanks
    – Danielo515
    Apr 28, 2022 at 6:03

0

Your Answer

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

Browse other questions tagged or ask your own question.