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?

4
  • 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. Commented Feb 14, 2015 at 1: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. Commented Feb 14, 2015 at 3: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! :) Commented Feb 14, 2015 at 14:35
  • @Tacahiroy Yeah, I think you're both right, don't hesitate to post an answer. :) Commented Feb 14, 2015 at 14:37

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

This did the trick, and also changed my vimscripting philosophy: thank you! ^ ^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.