neovim lua visual select range

Mar 13, 2024

-- correct way
vim.fn.getpos('v') -- start line
vim.fn.getpos('.') -- end line

It’s not super easy to find out how to get the visual select range in neovim lua. You might see


-- wrong way
vim.fn.getpos("'<") -- start line
vim.fn.getpos("'>") -- end line

around, but these marks are only updated after you leave visual mode. So either you have to manually exit visual mode when your function runs or you can use the first way provided.

Apparently this is somewhat of a pain point and may be fixed/updated at some point. God bless!


← Back ← Go to all posts