[vim/vim] Line number is 0 in dict returned from getbufinfo() (#7999)

21 views
Skip to first unread message

Linda_pp

unread,
Mar 23, 2021, 12:16:04 PM3/23/21
to vim/vim, Subscribed

Describe the bug
getbufinfo() returns buffer information as dict and it includes lnum key which indicates current line number. getbufinfo() returns 0 for lnum in some situation.

To Reproduce
Detailed steps to reproduce the behavior:

  1. Run vim --clean
  2. :new
  3. :new again
  4. :echo getbufnr(2)[0].lnum

Expected behavior

It echoes 1

Screenshots

スクリーンショット 2021-03-24 1 10 34

It echoes 0

Environment (please complete the following information):

  • Vim version: 8.2.2646
  • OS: macOS 10.15
  • Terminal: iTerm2

Additional context

lnum is still 0 even if the buffer (bufnr=2) contains text of multiple lines in the case.

When moving cursor to the window which opens the buffer (bufnr=2), echo getbufinfo(2)[0].lnum echoes 1 as intended.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

lacygoill

unread,
Mar 23, 2021, 12:24:00 PM3/23/21
to vim/vim, Subscribed

Duplicate of #6612.


See :h getbufinfo():

lnum Line number used for the buffer when
opened in the current window.

Notice the "when opened in the current window" part.

Christian Brabandt

unread,
Mar 23, 2021, 12:30:21 PM3/23/21
to vim/vim, Subscribed

closing then

Christian Brabandt

unread,
Mar 23, 2021, 12:30:26 PM3/23/21
to vim/vim, Subscribed

Closed #7999.

Linda_pp

unread,
Mar 23, 2021, 12:30:36 PM3/23/21
to vim/vim, Subscribed

Oh, thank you for the point. I didn't notice that. So the value is useless when it is not opened in the window...

lacygoill

unread,
Mar 23, 2021, 12:38:50 PM3/23/21
to vim/vim, Subscribed

@rhysd If your Vim includes the patch 8.2.1727, and if your buffer is currently displayed in a window, you can get the address of the line where the cursor was the last time that window was active, by passing its window number or window ID as an argument to getcurpos():

:echo getcurpos(2)[1]

lacygoill

unread,
Mar 23, 2021, 12:47:37 PM3/23/21
to vim/vim, Subscribed

And if your Vim includes the patch 8.1.1967, you can get the same information with line():

:echo line('.', win_getid(2))

lacygoill

unread,
Mar 23, 2021, 12:54:12 PM3/23/21
to vim/vim, Subscribed

I think this is a common mistake. Maybe we should improve the documentation:

diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 86df7b570..6d3aac7e9 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -5097,8 +5097,16 @@ getbufinfo([{dict}])
 					last used.
 					{only with the |+viminfo| feature}
 			listed		TRUE if the buffer is listed.
+
 			lnum		Line number used for the buffer when
 					opened in the current window.
+					NOTE: Only useful if the buffer has
+					been displayed in the window in the
+					past.  If you want the line number
+					of the last known cursor position in
+					a given window, use |line()|: >
+						:echo line('.', {winid})
+<
 			linecount	Number of lines in the buffer (only
 					valid when loaded)
 			loaded		TRUE if the buffer is loaded.

Linda_pp

unread,
Mar 23, 2021, 8:18:38 PM3/23/21
to vim/vim, Subscribed

Yes, I could use line() instead. One trouble for me was that I needed to use different functions for supporting both Vim and Neovim since Neovim does not support the second parameter of line(). But it is not an issue of Vim.

Reply all
Reply to author
Forward
0 new messages