# Is it possible to tell if a window is at bottom or top, left or right?

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) Dec 21, 2015 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 Dec 21, 2015 at 11:51
• Why not simply use ctrl-W_L? (or ctrl-W_R) Dec 21, 2015 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. Dec 21, 2015 at 22:55
• @AaronShen Have a look at the window restoring commands.
– muru
Dec 22, 2015 at 3:18

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()
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. Dec 24, 2015 at 23:24

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...