1

I know the command :%s/^/\=line('.')/ will insert line number at the start of each line as below.

Before

line1
line2
line3

After

1line1
2line2
3line3

But I want to insert a space after the line number as below.

1 line1
2 line2
3 line3

I tried giving a space after line('.') in the command, but doesn't work.

:%s/^/\=line('.') /

Edit: The question and the answers provided here are different from those of another question.

Community
  • 1
  • 1
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83

2 Answers2

6

You can correct your try like this: :%s/^/\=line('.').' '/

The problem was that it was parsing your space as part of the expression line('.'). But as you are inside an expression, you can use the . (dot) operator to concatenate strings.

yolenoyer
  • 8,797
  • 2
  • 27
  • 61
5

You can do this:

%s/^/\=printf('%d ', line('.'))

This also means you can pad out the integer so that things line up:

%s/^/\=printf('%4d ', line('.'))

or to put a dot or colon:

%s/^/\=printf('%4d. ', line('.'))
ronalchn
  • 12,225
  • 10
  • 51
  • 61