7

How to make Vim write lines into a file according to a very simple arithmetic pattern?

Example:

foo1
foo2
foo3
...
foo99
foo100

I came up with the following solution using Ex commands

:for i in range(1,100)
:  execute "normal ofoo" . i
:endfor

but I am convinced there must be something more straightforward.

Siméon
  • 173
  • 1
  • 5
  • Does this answer your question? [How to insert text from inside a vimscript loop?](https://stackoverflow.com/questions/28511027/how-to-insert-text-from-inside-a-vimscript-loop) – Biggybi Jan 13 '21 at 16:50

2 Answers2

7

I would do it with macro.

First type one line:

foo1

then

qqYp<c-a>q

finally replay the macro:

98@q
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thanks! I had forgotten about CTRL-a. Although it is not a problem here, the macro is much slower than the loop. – Siméon Mar 31 '15 at 12:19
  • @Siméon from scratch, I would say macro is faster. because you have to write the function/statements first, then call it. :-) – Kent Mar 31 '15 at 13:06
3

Something like:

:let l = map(range(1,100), '"foo".v:val')
:put=l

" Unfortunately put won't accept the expression, append() would though
call append(line('.'), map(range(1,100), '"foo".v:val'))
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83