I'm trying to set a key map like
local key_map = vim.keymap.set
local input = vim.api.nvim_input
local opts = { noremap = true, silent = true }
key_map('n', '<C-j>', function()
local desired_cursor_col = tonumber(vim.api.nvim_win_get_cursor(0)[2])
input('i<CR><Esc>')
local new_line_start_col = tonumber(vim.api.nvim_win_get_cursor(0)[2])
print(desired_cursor_col, new_line_start_col) -- (*) Tried to check the values
input('i')
for j = 1, (desired_cursor_col - new_line_start_col) do -- (**)
input('<Space>')
end
input('<Esc>')
end, opts)
But the mapping seems to skip the for-loop labeled by (**)
(I tried to check the value at (*)
but the mapping does not print the values. Weird.)
Do I miss something or make some mistake? Thanks.
(*)
for-loop does not work is thatdesired_cursor_col
andnew_line_start_col
are the same.|
is the cursor. In normal mode, press<C-j>
, then cursor|
will be in the third line and its column position will be exactly under where it was in the second line, and the stringrName
now will follow the cursor. Hope this can help understanding my question.input
being a local variable used in a global mapping. Did you tried withvim.api.nvim_input
instead?vim.api.nvim_input()
in the function andvim.keymap.set()
for setting the mapping, it skips the for-loop(*)
(which can be verified by commenting out the last lineinput('<Esc>')
and after typing<C-j>
we will be in insert mode).