6

How can I get the current cursor position in Lua? I want to get a number so that I can use it in a for-loop.

I tried this, but I got a hashed value instead:

local lineNum = vim.api.nvim_win_get_cursor(0)
print(lineNum)

The value I got:

table: 0x010134d4e8

How can I get the value I need?

3 Answers 3

9

You get a hashed value because nvim_win_get_cursor() returns a tuple (row, col). You can show the value with:

print(unpack(lineNum))

You can also get the row and column in two variables with this:

local r,c = unpack(vim.api.nvim_win_get_cursor(0))
print(r, c)

You might be interested in this SO answer. Also note that I am not really a Lua expert so the code above works (I tested it) but it might not be the Lua best practice. For more information on that you'll probably want to ask directly on SO.

6
  • 3
    Thank you for your answer. I found the documentation here, and I found out that I can access its value with local lineNum = vim.api.nvim_win_get_cursor(0)[1] as well. May 4, 2021 at 15:15
  • @John Winston How can I change local lineNum = vim.api.nvim_win_get_cursor(0)[1] to get column instead? Jun 29, 2022 at 19:37
  • @SergioAraujo Have you tried vim.api.nvim_win_get_cursor(0)[2]?
    – statox
    Jul 1, 2022 at 9:26
  • Yes @statox you are right Jul 1, 2022 at 12:36
  • Hello, idk if the API function name is not well-chosen, but this is not really giving the position of the cursor in the "window" as when you have wrapped lines or full lines of virtual text it will give the position of the cursor in the file/buffer. How can I get the position of the cursor in the window/screen. (example of use case is staying on same exact screen position when disabling line wrap)
    – gruvw
    Nov 21 at 8:01
1

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.

4
  • Welcome to Vim :-). It is difficult for me to identify your solution in your answer. Maybe should you convert it to a comment. Sep 12, 2022 at 8:30
  • 2
    @VivianDeSmedt The question is "How can I get the current cursor position in Lua?" and my answer is that this can be done in Lua with nvim__buf_stats(). This differs from @statox answer in that it works for any buffer (not just the current window). The caveat is, it only gives the line number. My answer is relevant to the OP's question and provides a solution (albeit with caveats) that is not mentioned in @statox answer.
    – tmillr
    Sep 12, 2022 at 9:23
  • Thanks for your comment. Maybe there is a way to highlight the positive part of the answer and keep the rest as remark. I believe it would help user to identify your answer more easily. Sep 12, 2022 at 9:30
  • 1
    @VivianDeSmedt alright I cleaned it up a bit
    – tmillr
    Sep 12, 2022 at 10:17
0

If you just want to print the values in the table, you can do:

vim.pretty_print(lineNum)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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