4

It appears from the :help while documentation that :

NOTE: The ":append" and ":insert" commands don't work
properly inside a ":while" and ":for" loop.

And I can confirm they don't. But what should I use then to insert text from inside a loop?

iago-lito
  • 3,098
  • 3
  • 29
  • 54
  • 1
    What are you trying to do? But it soundalike you could just build the whole string up in a variable and then insert the contents of that variable. – FDinoff Feb 14 '15 at 01:34
  • 1
    Have you tried setline() function? This should be working if you just want to insert text into a buffer even in while loop I think. Otherwise, you may buffer text in while loop, then insert them after the loop. – Tacahiroy Feb 14 '15 at 03:40
  • @FDinoff I've just wrote a function that can help me navigate from one argument to another inside a headed expression, and I would like to reuse it in reorganizing arguments of a long expression on several lines. I think that working on the whole string in a variable is the right solution indeed. Thanks! :) – iago-lito Feb 14 '15 at 14:35
  • @Tacahiroy Yeah, I think you're both right, don't hesitate to post an answer. :) – iago-lito Feb 14 '15 at 14:37

1 Answers1

11

The :insert and :append commands are mostly meant for interactive use. In a Vimscript, you can instead use the lower-level setline() and append() functions, which do work well in a loop. (They are also easier to use, because you can directly pass a List of lines to them.)

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324