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.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
To avoid the blank line at the top (kudos to romainl), use
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
That's useful when you're already familiar with some command-line way to get what you want.
For the record, and definitely not the shortest way (see @Undo's awesome solution), but sequence of keystrokes will do it too:
Let me break that down for you:
i1<Esc>-- insert the number 1, then get back out to command mode
qa-- start recording a macro in register "a"
Y-- copy the current line
p-- paste the current line (cursor will also move to the pasted line)
<Ctrl>a-- increment the next number on the line, making it 2
q-- stop recording the macro (was register "a")
98@a-- replay the macro in register "a" 98 times
I know this question is really old, but there's another way you can do it also. Try this:
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.