37

Starting from a blank slate, how can I obtain a document that contains

1
2
3
4
5
6
7
8
9
10
…
100

To be clear, I don't want these numbers displayed in the margin; I want them inserted into the document itself.

51

Use :put and range():

:put =range(1,100)

To avoid the blank line at the top (kudos to romainl), use :0put:

:0put =range(1,100)
  • 12
    Use :0put=… to avoid the blank line at the top. – romainl Feb 3 '15 at 17:40
  • 1
    Or in insert mode with <C-r>=range(1,100). – Hotschke Nov 14 '18 at 18:54
18

In addition to Undo's pure-vim :put =range(1,100) (which actually leaves you with a blank line up top), you can, depending on your OS, use one of its commands. E.g., on a Unix/Linux box:

%!seq 1 100

The above works by piping the entire (empty) buffer to seq, which ignores its input and just outputs the numbers 1 to 100. Vim then replaces the entire buffer with seq's output.

That's useful when you're already familiar with some command-line way to get what you want.

  • This "unasks" the question which is why I think it is better than the accepted one. Yes, you can make vim do the job, just as you can use a crescent wrench to stir soup. Vim is a text editor, not an arbitrary text generator; there are much more obvious tools for that. – msw Oct 23 '16 at 3:11
16

For the record, and definitely not the shortest way (see @Undo's awesome solution), but sequence of keystrokes will do it too:

i1EscqaYpCtrl+aq98@a

Let me break that down for you:

  1. i1<Esc> -- insert the number 1, then get back out to command mode
  2. qa -- start recording a macro in register "a"
  3. Y -- copy the current line
  4. p -- paste the current line (cursor will also move to the pasted line)
  5. <Ctrl>a -- increment the next number on the line, making it 2
  6. q -- stop recording the macro (was register "a")
  7. 98@a -- replay the macro in register "a" 98 times
4

Here is a different approach, that needs a newer Vim (something like 7.4.800)

This assumes an empty buffer and '1' in register a. First enter 100 1 into your buffer, "a100P.

Then visually select lines 2 till 100 :2EnterVG.

Now press gCtrl+A.

Read the help at :h v_g_CTRL-A

  • You have to add let @a="1" in your answer. Beginners might get stuck up to find how to store value 1 in register a. – SibiCoder May 28 '16 at 8:52
  • I have seen the gCtrl+A combo in other similar solutions. However I have been unable to find an explanation of what it is doing/how it works? Do you have a reference in the documentation that explains what g does? Thanks! If I find an explanation I will post it. – codingFoo Sep 16 '16 at 17:58
  • 1
    documentation is at :h g_CTRL-A – Christian Brabandt Sep 16 '16 at 20:03
  • I had missed the link to the v_g_CTRL-A section. – codingFoo Sep 16 '16 at 20:13
2

I know this question is really old, but there's another way you can do it also. Try this:

99o<esc>:%s/^/\=line('.')<cr>

99o<esc> will just open up 100 blank lines. Then, we run a substitute command:

:%                  " On every line
  s/                " substitute
    ^/              " the start of the line. (This will always match no matter what)
      \=            " Evaluate:
        line('.')   " The current line.

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.