2

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.

enter image description here

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.

enter image description here

1 Answer 1

0

You mapping look good.

But you have mapped against <leader>n which is by default: \n

Unless you have define: g:mapleader.

You can know the value of g:mapleader by running the following Neovim command:

echo "'" . g:mapleader . "'"

Some people (including myself) and some distributions (including NvChad, LunarVim, LazyVim, AstroVim) defines g:mapleader to Space

In such case <leader>n corresponds to Spacen

The result of the print call should be visible as the output of the following Neovim command

:messages

I you want to see the messages when they are triggered in Visual mode you have to increase the room for them with:

:set cmdheight=2
14
  • Thanks for replying. I've defined <leader> using vim.g.mapleader = ' ' in the previous section. Checking echo "'" . g:mapleader . "'", it gives ' ' meaning that <leader> is <Space>. Furthermore, the result of :verbose xmap <Space>n shows x <Space>n * <Lua {the path of the file setting this mapping} Lua>. I think <leader> should be <Space> as I expect. But when I try this mapping, it still doesn't work and stays in visual mode. 😥
    – HRC
    Commented Sep 12, 2023 at 4:42
  • Thanks for the feedback. When I try on my version it works well. Did you try with with a very simplifier version of init.lua? What if you start Neovim with nvim --clean? Commented Sep 12, 2023 at 5:00
  • 1
    Hi. I've tried your suggestion and updated/edited the post. The conclusion is: it doesn't work. 😵
    – HRC
    Commented Sep 12, 2023 at 5:38
  • 1
    Keystroke v without using mouse.
    – HRC
    Commented Sep 14, 2023 at 2:53
  • 1
    Hmm... so it is necessary to explicitly make the current buffer leaving visual mode (so that the message will not be hidden by -- VISUAL --) ?
    – HRC
    Commented Sep 19, 2023 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.