我如何使 window 拆分保持一致的大小与当前一样大,其他人一样小

How do I make window splits maintain consistent sizing with the current as large and others as small

这个想法是做一些类似于 Golden Ratio(vim 插件)所做的事情。但是我不想将大小设置为 "golden ratio",而是想为未选择的 windows.

设置一个特定的大小

例如:

# Window 1 selected
----------------------
|        |   |   |   |
|        |   |   |   |
|   1    | 2 | 3 | 4 |
|        |   |   |   |
|        |   |   |   |
----------------------

# Window 3 selected
----------------------
|   |   |        |   |
|   |   |        |   |
| 1 | 2 |    3   | 4 |
|   |   |        |   |
|   |   |        |   |
----------------------

这是我到目前为止所写的内容(这只是一个 WIP):

function g:ResizeWindow()
  let tabs = gettabinfo()
  let current_tabnr = tabpagenr()
  let current_window = win_getid()

  let tab = filter(tabs, 'v:val.tabnr == current_tabnr')[0]

  for window in tab.windows
    if window != current_window
      call win_gotoid(window)
      exe 'vertical resize' 20
    endif
  endfor

  call win_gotoid(current_window)
  let current_window_size = &columns - ((len(tab.windows) - 1) * 20)
  exe 'vertical resize' current_window_size
endfunction

autocmd WinNew,WinEnter * :call g:ResizeWindow()

要进行测试,您可以打开一个缓冲区并 :vsp 多次。然后,当您导航 windows 时,它似乎在大多数情况下都有效,但偶尔 windows 之一会以不一致的方式崩溃。它比其他的小得多。通常这种情况发生在我从左到右导航时...以及从右到左后退时。

关于这有什么问题以及如何解决它有什么想法吗?

超级有趣的功能!

这是一个工作版本:

function g:ResizeWindow()
  let tabs = gettabinfo()
  let current_tabnr = tabpagenr()
  let current_window = win_getid()

  let tab = filter(tabs, 'v:val.tabnr == current_tabnr')[0]

  let small_size = 5

  for window in tab.windows
    if window == current_window
      let size = &columns - ((len(tab.windows) - 1) * small_size) - (len(tab.windows) - 1)
    else
      let size = small_size
    endif
    noautocmd call win_gotoid(window)
    exe 'noautocmd vertical resize ' . size
  endfor

  call win_gotoid(current_window)
endfunction

set winwidth=1
set winminwidth=1
autocmd WinNew,WinEnter * :call g:ResizeWindow()

说明

我对您的初始 WIP 做了很多更改,以下是您的代码遇到的主要问题:

  • 函数被递归调用:函数 win_gotoid 的调用触发了 autocmd。所以这弄乱了所有尺寸

  • 默认的最小 window 大小 (minwinwidth) 和默认的 window 大小 (winwidth) 与您应用的大小不符

  • 您将当前 window 的大小调整为上次,这挤压了右侧的 window

  • 您的主要 window 尺寸计算没有考虑 window 分隔符


免责声明

如果一个 window!

上存在水平分割,则此函数会中断