3

When I do :h 'statusline' I can see

The g:statusline_winid variable will be set to the window-ID of the window that the status line belongs to.

But :set statusline=[window_id\:%{g:statusline_winid}] does not work.

Actually I don't have any problem with this because I can use winnr(), but the mystery remains.

What's g:statusline_winid? Is it still available? Or my use was wrong?

1 Answer 1

5

The documentation say:

When the option starts with "%!" then it is used as an expression,
evaluated and the result is used as the option value.  Example:

    :set statusline=%!MyStatusLine()

The g:statusline_winid variable will be set to the window-ID of the
window that the status line belongs to.

So if you for example do:

fun! MyStatusLine()
    return "WID: " . g:statusline_winid
endfun

set statusline=%!MyStatusLine()

Your status-line will be:

WID: NNNN

where NNNN is the window-ID. Or put another way: the variable g:statusline_winid is not set unless you use a call with statusline=%!SomeFun()

Also note that the variable is unset after function exits.

winid vs winnr

As for what it is:

winid: Each window has it's own ID. When you start Vim first window typically has ID 1000. If you open a new window by e.g. :split the new window get ID 1001 if you close that split and open another that get 1002 etc.

winnr: Each window has a number based on it's position. Upper left is 1. You can for example have:

+------+-----+
|      |  2  |
|  1   +-----+
|      |  3  |
|      |     |
+------+-----+

That is: winnr is not persistent. It all depends on the layout. If you close window number 1 in example above, 2 becomes 1 and 3 becomes 2.

winid is a way to keep track of a window independent of layout changes.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .