I want to get the range of ex-mode command in Lua(eg 1
and 3
in 1;3d
). The range value should be stored as a:firstline
and a:lastline
in vim, but based on the doc, a
is not implemented in the vim
table. How can I get them in Lua?
Update
This is the command I am trying to define:
vim.api.nvim_exec(
"command! -range " .. opts.ex_mode_cmd .. " <line1>,<line2>lua require('commented').toggle_comment('c')", true)
From this I expect I can get the value of a:firstline
and a:lastline
inside toggle_comment
, but they all returned nil
local function toggle_comment()
print('check range', vim.firstline, vim.lastline)
end
a:firstline
anda:lastline
are only ever valid from functions, that are define with therange
attribute, but is not always readily available. So this only exists for custom vimscript functions. But I suppose that is not what you want here, it cannot be used to get arbitrarily ranges from ex commands like:g
or:m
:help :command-range
and then you can use<line1>
and<line2>
in the command definition to pass the range to whatever Lua code you call from there...<line1>
and<line2>
to the function. But since my function is not accepting start and end range as parameter originally, I want to avoid this unless I cannot find any other solution.vim.firstline
andvim.lastline
. In NeoVim, the:lua
command takes a range, but no idea where it actually stashes it...