3

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.

  • 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 '18 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 – AnonymousLurker Dec 25 '18 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 '18 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. – AnonymousLurker Dec 25 '18 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 '18 at 0:05
1

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

| improve this answer | |
  • 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). – AnonymousLurker Apr 22 '19 at 1:53
2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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