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])                        
                        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


                        for j = 1, (desired_cursor_col - new_line_start_col) do  -- (**)

                      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.

  • I think the reason why (*) for-loop does not work is that desired_cursor_col and new_line_start_col are the same.
    – HRC
    Jun 12 at 19:17
  • By the way, this mapping is used in the such case that the code is composed of the following lines: ` @fn funcName(varType varName, -- indented by four spaces.` and ` varType va|rName) -- indented by 15 spaces to align the first line` where | 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 string rName now will follow the cursor. Hope this can help understanding my question.
    – HRC
    Jun 12 at 19:18
  • I have a doubt about input being a local variable used in a global mapping. Did you tried with vim.api.nvim_input instead? Jun 12 at 20:28
  • 1
    @VivianDeSmedt Yes, Even though I use vim.api.nvim_input() in the function and vim.keymap.set() for setting the mapping, it skips the for-loop (*) (which can be verified by commenting out the last line input('<Esc>') and after typing <C-j> we will be in insert mode).
    – HRC
    Jun 13 at 15:24


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.

Browse other questions tagged or ask your own question.