Roman Gonzalez
Roman Gonzalez

Reputation: 1931

Get position of splitted windows on a vim editor

I've been trying to find out how the get the position/coordinates of a splitt window inside a vim editor window, but no luck so far.

Say for example I have this layout

     (0,0)         (2, 0)           
       \____________/____________
       |            |          |
       |  Split A   |  Split C |
 (0,2)-+------------+----------+
       |  Split B   |  Split D |
       |____________|__________|  #Split D would be (2, 2)

I want to get the coordinates of the different splits on of my Vim Window, is this possible?


I've done my homework and googled this, also went through the vim :help/:helpgrep

Things that I've tried that wouldn't work:

Upvotes: 9

Views: 1287

Answers (3)

Jethro Cao
Jethro Cao

Reputation: 1050

I'm coming across this question in September 2023, which now has the built-in function winlayout(), which appears to have been added into Vim circa 2019 judging from the Vim's GH repo.

Granted, its return value isn't exactly in the form the OP asked for. But for my purpose of determining if the primary split of my windows is along the vertical or horizontal orientation, this function suffices.

Upvotes: 0

ZyX
ZyX

Reputation: 53654

I do not know a function that will do this, but here are some facts:

  1. Window size can be obtained using winwidth(wnr) and winheight(wnr).
  2. Number of windows can be obtained using winnr('$').
  3. If 0<wnr≤winnr('$'), then window with number wnr exists.
  4. Total width is &columns and total height is &lines.
  5. Windows are separated by one-column or one-line separator.

In order to get window layout you lack only one fact here: how windows are numbered. I can't find this in help now.

:h CTRL-W_w

states that windows are numbered from top-left to bottom-right. It is not enough though to determine how windows will be numbered after executing the following commands:

only
enew
vnew
new
wincmd h
new
" Result:
" +---+---+
" | 1 | 3 |
" +---+---+
" | 2 | 4 |
" +---+---+
only
enew
new
vnew
wincmd j
vnew
" Result:
" +---+---+
" | 1 | 2 |
" +---+---+
" | 3 | 4 |
" +---+---+

Thus, it looks like determining current window layout is not possible without using window movement commands (wincmd h/j/k/l).


Some time ago one additional variant was introduced: pyeval(printf('(lambda win: [win.col, win.row])(vim.windows[%s - 1])', winnr)) (also py3eval(…)) will provide exact position of the top-left corner of window winnr. Requires Vim compiled with +python[/dyn] or +python3[/dyn] and Python itself.

Upvotes: 4

Grammin
Grammin

Reputation: 12205

So I think the only thing that might help you is:

:!xwininfo -id $WINDOWID

Other then that I don't think you can get the specific coordinate splits.

Upvotes: 0

Related Questions