oops, 

instead of getpos() you can use line() of course:

vim9script
def g:Comment(line1: number, line2: number)
    var l1 = min([line1, line2])
    var l2 = max([line1, line2])
    append(l2, ["#endif"])
    append(l1 - 1, ["assert(0);", "#if 0"])
enddef

noremap \cm <cmd>call Comment(line('.'), line('v'))<CR><ESC>


On Tuesday, August 5, 2025 at 1:51:20 PM UTC+10 Maxim Kim wrote:

> You can call Comment withing <cmd>
>
> vim9script
> def g:Comment(m: string)
>     if m == 'v'
>         var l1 = line("'<")
>         append(l1 - 1, ["assert(0);", "#if 0"])
>         var l2 = line("'>")
>         append(l2, ["#endif"])
>     else
>         var l1 = line(".")
>         append(l1 - 1, ["assert(0);", "#if 0"])
>         var l2 = line(".")
>         append(l2, ["#endif"])
>     endif
> enddef
>
> map \cm <CMD>call Comment(mode(1))<CR>
>
> Or this one
>
> vim9script
> def g:Comment(line1: number, line2: number)
>     var l1 = min([line1, line2])
>     var l2 = max([line1, line2])
>     append(l2, ["#endif"])
>     append(l1 - 1, ["assert(0);", "#if 0"])
> enddef
>
> noremap \cm <cmd>call Comment(getpos('.')[1], getpos('v')[1])<CR><ESC>
>
> https://asciinema.org/a/uzzEAJiy2zdtHigNQiikiqz16
>
> On Tuesday, August 5, 2025 at 1:18:46 PM UTC+10 russell shaw wrote:
>
>> On 4/8/25 20:26, Maxim Kim wrote: 
>> > When you do `:<C-U>` you effectively exit visual mode. 
>> > 
>> > Instead try to use `xnoremap <F5> <cmd>echow getpos(".") 
>> getpos("v")<CR>` 
>>
>> Hi, i found out about <cmd> a short time ago. 
>>
>> vim9script 
>> def g:Comment(m: string) 
>> if m == 'v' 
>> var l1 = line("'<") 
>> append(l1 - 1, ["assert(0);", "#if 0"]) 
>> var l2 = line("'>") 
>> append(l2, ["#endif"]) 
>> else 
>> var l1 = line(".") 
>> append(l1 - 1, ["assert(0);", "#if 0"]) 
>> var l2 = line(".") 
>> append(l2, ["#endif"]) 
>> endif 
>> enddef 
>>
>> map \cm <CMD>let m = mode(1)<CR>:<C-U>call Comment(m)<CR> 
>>
>>

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_use+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/vim_use/a210d30e-c4e0-46a2-89a9-885ec60994b3n%40googlegroups.com.

Reply via email to