2

Question reposted on StackOverflow, where it's receiving more attention.

Given the following function in the .vimrc file,

fu! MyFun(count)
  echo a:count
  echo a:count
  if a:count > 0
    normal ,
    call MyFun(a:count - 1)
  endif
endf

calling :call MyFun(3) generates the following output.

3
3
2
2
1
1
0
0

However, if I define the mapping nn , :<C-U>execute "call MyFun(" . v:count . ")"<CR>, then the call to :call MyFun(3) generates

3
0
2
0
1
0
0
0

I do understand that the mapping of , makes the MyFun function call itself twice (if a:count > 0), however I cannot understand how this can cause a different result of the two successive calls to echo a:count.

  • 3
    Doesnt normal , call the function with a count 0 when you do the map version? Id expect to see 33002200110000 – D. Ben Knoble Sep 26 at 2:18
  • Yes, I expect the same. – Enrico Maria De Angelis Sep 26 at 20:10
  • 1
    Re: Your flag: mods can't move questions either :-( It's possible in some cases but a SE employee would have to do it manually, and I'd rather not add to their workload. It's fine to just ask it again on Stack Overflow if you really want to. – Martin Tournoij Sep 28 at 11:10
2

The problem is all about screen redraw (see :h echo-redraw) in Vim.

Changing echo to echom still produces the same (broken) screen output (3 0 2 0 1 0 0 0), but :mess reveals what is hidden: 3 3 0 0 2 2 0 0 1 1 0 0 0 0.

2

I don't know why it happens, but if you add a \n after the echo it all works and the result is what you would expect:

fu! MyFun(count)
  echo a:count . "\n"
  echo a:count . "\n"
  if a:count > 0
    normal ,
    call MyFun(a:count - 1)
  endif
endf

But maybe you already knew that and was just curious to know why (as I confess I am).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.