2

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

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.

1
  • 1
    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 at 15:15

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.