I want to make a visual mode operator for commenting lines. I have achieved this in normal mode by getting lines with nvim_win_get_cursor() and vim.v.count, but how can I get starting and ending lines of range in visual mode? Or do I need to get that range to create an operator?


1 Answer 1


You can get the position of the beginning and end of the visual selection with marks '< and '>. But note that these marks are only available after you leave Visual mode, so in effect they refer to the beginning and end of the last Visual mode selection. But this is the typical situation you're in, since whenever you go to command-line mode or use :call to invoke a function, Vim will have left Visual mode already.

I believe you can use the nvim_buf_get_mark() NeoVim API to get the position of those marks. (In Vimscript, you would use getpos("'<") and getpos("'>").)


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.