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 communityUse :put
and range()
:
:put =range(1,100)
To avoid the blank line at the top (kudos to romainl), use :0put
:
:0put =range(1,100)
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.
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:
i1<Esc>
-- insert the number 1, then get back out to command modeqa
-- start recording a macro in register "a"Y
-- copy the current linep
-- paste the current line (cursor will also move to the pasted line)<Ctrl>a
-- increment the next number on the line, making it 2q
-- stop recording the macro (was register "a")98@a
-- replay the macro in register "a" 98 timesHere 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
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.