getpos()

44 views
Skip to first unread message

russell shaw

unread,
Jul 27, 2025, 5:55:33 AMJul 27
to vim...@googlegroups.com
Hi,

vim9script
vmap <F5> :<C-U>echo getpos(".") getpos("v")<CR>

When i select a visual area of text, i get the same coordinate reported for both
getpos(".") and getpos("v"). Why is that ?

Maxim Kim

unread,
Aug 4, 2025, 6:26:06 AMAug 4
to vim_use
When you do `:<C-U>` you effectively exit visual mode.

Instead try to use `xnoremap <F5> <cmd>echow getpos(".") getpos("v")<CR>`

russell shaw

unread,
Aug 4, 2025, 11:18:46 PMAug 4
to vim...@googlegroups.com
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>

Maxim Kim

unread,
Aug 4, 2025, 11:51:20 PMAug 4
to vim_use
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

Maxim Kim

unread,
Aug 4, 2025, 11:53:08 PMAug 4
to vim_use
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>

Russell Shaw

unread,
Aug 5, 2025, 2:48:55 AMAug 5
to vim...@googlegroups.com
On 5/8/25 13:53, Maxim Kim wrote:
> 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>

Hi, that one is much more efficient because l2 is appended before l1 :)

Maxim Kim

unread,
Aug 5, 2025, 3:20:02 AMAug 5
to vim_use
It wasn't about efficiency, I just didn't want to use l2+2 to accomodate the shift that would happen if I add before l1 first.
Reply all
Reply to author
Forward
0 new messages