1

For example, I have this in the buffer:

line1 .... A ....
line2 B
line3 .... C ....

Follow these steps:

  1. At first it's in Normal mode and the cursor is under A
  2. Press j and the cursor will move to B
  3. Press j again and the cursor will move to C

Seems like internally Vim maintains a "user-expected" column position so when it goes from line2 to line3 it smartly restores the correct column position.

So how can I get the same behavior with Lua API (like vim.api.nvim_win_set_cursor()) when moving the cursor one line down?

1
  • 1
    Idk about Lua, but :normal! j would probably work
    – D. Ben Knoble
    Nov 19, 2022 at 16:48

1 Answer 1

2

As reminded by @D.Ben's comment, the following workaround works for me:

vim.cmd('norm! j')
2
  • 1
    The bang matters; without it, you’re command invokes whatever j is mapped to
    – D. Ben Knoble
    Nov 20, 2022 at 20:19
  • @D.BenKnoble thanks for the info. updated.
    – vim.ggyG
    Nov 21, 2022 at 1:14

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.