2

I would like to get these line numbers of a different window without moving to that window.

line('w0')
line('w$')
line('.')
line('$')

As for the first two, we can use getwininfo([{winid}]) which returns a list including 'topline' and 'botline'. Is there any function or some workaround as for the last two?

2

As a partial answer, for line('.'), you can get the buffer number associated with a window with winbufnr({nr}) (carefull to pass the window number and not the window id) then pass it to getbufinfo([{expr}]) and get the lnum entry :

getbufinfo(winbufnr({nr}))[0]['lnum']

Thanks to @Luc Hermitte, we can get the last line number with

len(getbufline(winbufnr({nr}), 1, '$')

which gets all the lines of the buffer as a list of strings and returns its length.

Old original answer

Can't tell how to get line('$') without moving to the actual window. But we could achieve it with a function that restores the current and previous visited window, than get any line({expr}) :

function! WindowLine(winnr, expr)
    let curr_window = winnr()
    let prev_window = winnr('#')
    exec a:winnr . 'wincmd w'
    let line = line(a:expr)
    exec prev_window . 'wincmd w'
    exec curr_window . 'wincmd w'
    return line
endfunction
echo WindowLine(2, '$')
  • 1
    With len(getbufline(winbufnr({nr}), 1, '$') we can obtain the number of the last line in a given buffer. It will be a lot more efficient than any solution that switches context (and that could also trigger autocommands) – Luc Hermitte Aug 8 at 11:48
2

patch 8.1.1418 introduced :h win_execute() :

call win_execute(winid, 'let l:num_lines = line("$") | let l:current_line = line(".")')

It's fast, it has no window related side effect.

  • 1
    Thank you. win_execute() can be used not only for line() but also other window local functions without changing the window. – Rick Howe Aug 9 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.