Correctly get select line range when using command and key mappings? #26092
Replies: 2 comments
-
I have written two plugins, both need to detect the visual selection. Use-case-1In the gitlinker.nvim's GitLink, I want the command In normal mode, it select the current line on cursor. In visual mode, it select the line ranges. For now I just merge two results from both Use-case-2In the fzfx.nvim's FzfxLiveGrepV, I need the command But I only allow user to mapping this command in visual mode (there's a technical limitation, I will discuss it later). So the implementation is like: vim.cmd([[ execute "normal! \<ESC>" ]])
local mode = vim.fn.visualmode()
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>") since user is required to only mapping it to visual mode, so it always first execute But just like I mentioned before, this command only allow user to map to visual mode. I had to provide another command I wish I could merge these two commands into one, which seems much more neat and clean. Is there any optimization to do? |
Beta Was this translation helpful? Give feedback.
-
I faced a similar situation where I need to get the range from a xmap, and get back to visual selection.
However,
|
Beta Was this translation helpful? Give feedback.
-
There are two methods to get the select line range in neovim now: use command or key mappings.
here is an example:
first define the command that support range select:
define key mappings on the command:
Now, I have 3 ways to select the lines and execute the command:
<,>:MyCommand
and typeenter
:opts.line1
andopts.line2
(method-1) could work correctly.vim.fn.mode()
(method-2) not work, it doesn't treat it as visual mode.vim.fn.visualmode()
(method-3) works, it's the same with method-1.<leader>ga
(key-1):opts.line1
andopts.line2
(method-1) don't work, it doesn't recognize the line range.vim.fn.mode()
(method-2) works correctly, it catched the visual mode, and useexecute "normal! \<ESC>"
to first quit visual mode, go back to normal, then usegetpos()
to get the line range.<leader>gb
(key-2), the result is same with the 1st one (e.g. manual enter command).my question is, as a plugin writer, how should I provide a user command, so make sure above 3 use cases working correctly, to support line range?
Beta Was this translation helpful? Give feedback.
All reactions