4

I am looking for a way to toggle between these two layouts:

-------
|  |  |
-------

-------
|     |
-------
|     |
-------

I only have two windows and I need to switch the layout often. I need a solution that will preserve the aspect ratio of windows in both horizontal and vertical split layouts, so I don't end with

---------
|  |    |
---------

-------
|     |
-------
|     |
-------

---------
|    |  |
---------

after toggling twice.

If this isn't possible with some oneliner in vimrc, I am willing to start using a plugin that offers this functionality, so feel free to suggest it.

Edit: I am familiar with Ctrl-W + H/J/K/L but that doesn't preserve aspect ratio of windows.

5
  • What about CTRL-W_t+CTRL-W_K (to horizontal), and CTRL-W_t+CTRL-W_H (to vertical). stackoverflow.com/questions/1269603/…
    – lsrdg
    Dec 25, 2018 at 21:21
  • @Isrdg it doesn't preserve aspect ratio, it just splits the screen into two equal parts for both horizontal and vertical splits Dec 25, 2018 at 21:31
  • Oh, sorry, my bad. Not sure I can help, but let me try to understand: do you want to any layout (e.g. if you manually resize the splits) or do you wanna preserve the aspect so that it is always equal (CTRL-w_=)?
    – lsrdg
    Dec 25, 2018 at 21:52
  • @Isrdg I do need to manually resize the splits and I want the aspect ratio preserved. As in if I have the vertical split with ratio 1:2 then toggle to horizontal split, then again to vertical split, I shall get the same 1:2 ratio. Dec 25, 2018 at 22:24
  • I've got how to keep track of window sizes (:h getwininfo()), but I can't find a way to keep track of how they split (verticallyXhorizontally). I may resort to two commands... Let me know if you find something, or else I'll come back whenever I've something working. (:
    – lsrdg
    Dec 26, 2018 at 0:05

2 Answers 2

2

Use tabs. Tabs in Vim function more like OS-level virtual desktops than tabs in most other text editors. Combined with the Vim's ability to display the same buffer in multiple splits, and you've got a built-in solution to your needs.

Set up your first layout as you normally would, then

:tab split

to open a new tab with a single split showing your current buffer. You can then set up your second layout like normal.

gt will cycle you through your tabs. It's similar to CTRL-Tab in most web browsers. gT cycles tabs in reverse order.

You can find more information about tabs with :help tabpage

Demo:

Demo gif of tabs maintaining split layouts

1
  • Very creative! And since it's just two buffers, there's not that much redundancy by having two tabs (I suspect this could've been the reason why I've instinctively overlooked such an approach). Apr 22, 2019 at 1:53
5

add this to your ~/.vimrc (use whatever function-key numbers you want)

:map <F3> <C-w>K<CR>
:map <F4> <C-w>H<CR>

then with two windows open, you can use F3 and F4 to switch the layout.

if there's a way to determine the current state (are windows arranged vertically or horizontally), it would be possible to make a function that checks that state and sets horizontal or vertical layout.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.