Smart window resizing with splits in MacVim

前端 未结 2 819
情深已故
情深已故 2021-01-19 17:50

I\'m using the latest MacVim. Is there any way to have it so if I open MacVim without a file or with only one file, it sets the window width to n characters? Then if I do a

相关标签:
2条回答
  • 2021-01-19 18:32

    I realized that my first post modified window height, not width. Here is what I meant:

    Here's a quick solution I came up with, but it's not perfect. The function counts the number of open windows and then sets the window width to original_width * num_windows. The autocommands call the function when Vim starts, and whenever a new window is opened. You can change the default window width (80) to suit your needs.

    function! SmartWidth( width )
        let num_wins = 0
        windo let num_wins+=1
        sil exe "set columns=" . num_wins * a:width
        sil exe "normal! \<c-w>="
    endfunction
    
    autocmd VimEnter * call SmartWidth(80)
    autocmd WinEnter * call SmartWidth(80)
    

    This works in the basic case, but does not distinguish between horizontal and vertical splits. I don't know how to do that!

    0 讨论(0)
  • 2021-01-19 18:45

    This appears to work. Whether or not a horizontal split has been done, any time a vsplit is created or deleted the window is resized.

    let g:auto_resize_width = 40
    function! s:AutoResize()
        let win_width = winwidth(winnr())
        if win_width < g:auto_resize_width
            let &columns += g:auto_resize_width + 1
        elseif win_width > g:auto_resize_width
            let &columns -= g:auto_resize_width + 1
        endif
        wincmd =
    endfunction
    
    augroup AutoResize
        autocmd!
        autocmd WinEnter * call <sid>AutoResize()
    augroup END
    

    Configure the window width by changing the variable at the top. You probably want to do something like let g:auto_resize_width = &columns to set it to use the width of the original window as the width to resize by.

    Things get a little wonky if you have so many vsplits that the window becomes maximized horizontally. I'm trying to find a fix and I'll post it if I find one.

    0 讨论(0)
提交回复
热议问题