A function relevant to your question that I came across in the public/documented api (:help api
) is vim.api.nvim__buf_stats()
which takes a buffer number (or use 0 for current buffer) and returns a table with the field current_lnum
. current_lnum
will be the line number of the current/last known cursor position for that buffer. Currently however, as of Neovim v0.7.2, this function is missing a description and also includes 2 underscores in its name instead of the usual 1 (this may mean that it is an internal or experimental function that is not meant to be used).
So if you only need the line number, this will work for any currently valid buffer (pass the buffer number for the argument, or 0
for current buffer):
local lineNum = vim.api.nvim__buf_stats(0).current_lnum
print(lineNum)
Note: the buffer will need to have been displayed in a window before, otherwise current_lnum
will be 0 which is not a valid line number
Additional Remarks
It seems maybe a little strange to me because there does not appear to be a direct/concise way (at least not without calling a Vimscript builtin function with vim.fn
or the like) to get the exact last known cursor position - both row and column - for an arbitrary buffer in Lua. Yes, there is vim.api.nvim_win_get_cursor()
, but this requires the buffer to be currently displayed in a window.