12

I don't know if there's some function to check a window's position, I mean is it currently sitting at bottom or top, left or right??

__________________________
|           |            |
|           |            |
|  left     |   right    |
|           |            |
|           |            |
|___________|____________|

__________________________
|                        |
|          top           |
|________________________|
|                        |
|         bottom         |
|_______________________ |
  • What are you trying to do? (XY Problem) – mMontu Dec 21 '15 at 11:42
  • I'm trying to write a function to rotate window splits to vertical if current layout it's horizontal and vice versa when there're just two windows. So I need to know what's the current splits layout and where's my current window – Aaron Shen Dec 21 '15 at 11:51
  • 3
    Why not simply use ctrl-W_L? (or ctrl-W_R) – Vitor Dec 21 '15 at 14:09
  • I want it to be convenient toggle function, it will automatically detect the current layout and do the correct rotation for me instead that I need to think it in my brain and choose the right command to use. All I need to do is always hit the same key mapping. – Aaron Shen Dec 21 '15 at 22:55
  • @AaronShen Have a look at the window restoring commands. – muru Dec 22 '15 at 3:18
3

Summing up the comments of the question:

-- I'm trying to write a function to rotate window splits to vertical if current layout it's horizontal and vice versa when there're just two windows.

-- Why not simply use ctrl-W_L? (or ctrl-W_R)

-- I want it to be convenient toggle function, it will automatically detect the current layout and do the correct rotation for me instead that I need to think it in my brain and choose the right command to use. All I need to do is always hit the same key mapping.

For the layouts containing just two windows the following function should work:

function! Rotate()
   " save the original position, jump to the first window
   let initial = winnr()
   exe 1 . "wincmd w"

   wincmd l
   if winnr() != 1
      " succeeded moving to the right window
      wincmd J " make it the bot window
   else
      " cannot move to the right, so we are at the top
      wincmd H " make it the left window
   endif

   " restore cursor to the initial window
   exe initial . "wincmd w"
endfunction

nnoremap <F3> :call Rotate()<CR>
  • awesome function, althought I don't know all the vim scripts, but thanks, it work perfectly!! btw, I add just one additional check for it only works when there're just two windows. – Aaron Shen Dec 24 '15 at 23:24
6

I don't think there is any way to check this, unfortunately.

There might be a way to make a custom function that checks this: The window numbers are always numbered from 1 on the top left to n on the bottom right. Perhaps one can use this combined with the geometry data from winwidth(), winheight() and the values of &lines and &columns to create the desired map?

Note, in order to be general, this should support more complex layouts. What should the output be if for instance the left side is split into two? Perhaps this is the reason why the function does not exist in the first place...

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.