Reputation: 20749
In the below vim window represented by visible area
, how do I get the horizontal position of X
, relative to Y
, from a function?
Y------------------------+
1 File contents |
| |
| +-X--------------+ |
| |4| | |
| |5| Visible area | |
| |6| | |
| +-+--------------+ |
$ ^ |
+----|-------------------+
\
line numbers
For example, the vertical position of X
relative to Y
is four, as in the window is scrolled four rows down. I can get this as a zero-based index with line("w0") - 1
.
I how do I determine how many columns rightwards the window is scrolled at a given moment? I've tried virtcol(".") - wincol()
but that alone is slightly off if the cursor is over a double-width character.
Upvotes: 5
Views: 587
Reputation: 536
Here is a solution without modifying the cursor position:
winsaveview().leftcol
Upvotes: 4
Reputation: 18205
It's unfortunate that vim doesn't have a more direct way to get this, but the following will work
let cursor_pos = getpos('.')
normal g0
let scroll_x = col('.')
setpos('.', cursor_pos)
The trick is the g0
motion, which moves the cursor the left of the window, and from there we can get the current cursor column.
Upvotes: 0
Reputation: 53604
If you want to have number column width use max([len(line('$')), &numberwidth])+1
.
What does “horizontal position” mean? wincol()
is horizontal position in the window, col(".")
is byte offset from the start of the line, strchars(getline('.')[:(col('.')-1)])
is the number of unicode codepoints from the start of the line, len(split(getline('.')[:(col('.')-1)], '.\@='))
is the number of characters from the start of the line.
Upvotes: 0