Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get position of splitted windows on a vim editor

Tags:

vim

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:

  • getwinposx()/getwinposy(): They doesn't work on terminal, and they don't actually return the info I want, it just returns the position of the Host OS window.

  • :winpos: the same reason as the previous bullet.

like image 442
Roman Gonzalez Avatar asked Nov 09 '11 20:11

Roman Gonzalez


1 Answers

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.

like image 167
ZyX Avatar answered Oct 21 '22 23:10

ZyX