2

When you use a count to a key mapping it goes down count number of times and resets to column 1.
Actually it already resets to col 1 when entering the function if there is a count. So I can save the current line number in a script variable, but that does not work for the column because the column is 1 if there is a count.

Using a loop does not really help because even if I instantly return from a function the cursor will move count number of times and set the col to 1.
Then I tried using the range keyword with a:firstline and a:lastline but the cursor still resets to col 1.

Is there a way to prevent this behavior of a count or a range?

map <unique> <Leader>c :call Commenter() <CR>

function Commenter()
  let s:curcol=col('.')
  let s:curlineno=line('.')

  echo "the column is " . s:curcol
  echo "the line number is " . s:curlineno

  let l:curlineno=line('.')
  echo "the current line is " . l:curlineno
  let l:curcol=col('.')
  echo "the current col is " . l:curcol

  call cursor(s:curlineno,s:curcol)

endfunction
1
  • Just a question, doesn't the space has an influence? ----- map <unique> <Leader>c :call Commenter() <CR>
    – eyal karni
    Feb 22, 2022 at 21:10

1 Answer 1

1

I found the solution myself.
Using <c-u> after the colon and before call in the key mapping prevents the effect of the count, but fortunatelly you can still read the count with v:count and v:count1.

map <unique> <Leader>c :<c-u>call Commenter() <CR>


Then I made a while loop in my function with the value of v:count1.
map <unique> <Leader>c :<c-u>call Commenter() <CR>

let s:firstrun="true"

function Commenter() abort
  let l:i = v:count1
  while l:i>0
    if s:firstrun=="true"
      let s:startcol=col('.')
      let s:startlineno=line('.')
    endif

    let l:curlineno=line('.')
    let l:curcol=col('.')

    ...do stuff...

    if l:i==1
      call cursor(s:startlineno,s:startcol)
      let s:firstrun="true"
    else
      execute "normal j"
      let s:firstrun="false"
    endif
    let l:i-=1
  endwhile
endfunction

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.