Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Insert a line number, with a space after

Tags:

vim

I need to insert the line number before each line of text using Vim, and there has to be a space after the line number. For example, if this was TestFile:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Morbi nunc enim, vehicula eget, ultricies vel, nonummy in, turpis.

It should look like this

1 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
2 Morbi nunc enim, vehicula eget, ultricies vel, nonummy in, turpis.

I have been using the command :%s/^/\line('.')/ with a number of variations, but I cannot figure out how to get the space at the end.

Any ideas?

like image 553
ncphillips Avatar asked Jan 15 '23 11:01

ncphillips


2 Answers

You were very close!

This substitution will do the job by concatenating the string ' ' to the line number:

%s!^!\=line('.').' '!
like image 102
Johnsyweb Avatar answered Jan 21 '23 17:01

Johnsyweb


This is probably easiest with an external tool:

:%!nl -ba -w1 -s' '
like image 37
William Pursell Avatar answered Jan 21 '23 16:01

William Pursell