Search code examples
loopsvimwhile-loopinsertion

How to insert text from inside a vimscript loop?


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?


Solution

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