I've tried to make a key map as follows:
vim.keymap.set('x', '<leader>n', function() -- Here <leader> is <Space>
line1r, line1c = unpack(vim.api.nvim_buf_get_mark(0, "<"))
line2r, line2c = unpack(vim.api.nvim_buf_get_mark(0, ">"))
print("line1r = " .. line1r .. "; line2r = " .. line2r)
end, { noremap = true })
(Select some lines and print the first line number and the last line number)
When I select some lines in visual mode and type <leader>
, which-key shows n
indicating this map exists.
However, it does not print line 1= <some number>; line2= <some number>
when I type n
, and it still stays in visual mode.
I'm not sure where is the wrong point.
Is there something I missed?
[Updated 1]
I've tried to make a whole new init.lua
which only contains the following setting
vim.g.mapleader = ' ' -- Set leader as <Space>
vim.keymap.set('x', '<leader>n', function()
line1r, _ = unpack(vim.api.nvim_buf_get_mark(0, "<"))
line2r, __ = unpack(vim.api.nvim_buf_get_mark(0, ">"))
print("line1r = " .. line1r .. "; line2r = " .. line2r)
end, { noremap = true })
And it still doesn't work. That's weird.
By the way, my environment info:
- NVIM v0.9.1
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
- OS: Windows 10
[Updated 2]
Using :message
, it does record the printing result(s) it should have shown when the key mapping is triggered. However, these printing result does not appear exactly when the mapping is triggered. :-(
By the way, nvim seems to record the previous position of marks '<
and '>
rather than the "current" position. I think this may be discussed on another new question/post.