Saturday, September 27, 2014

How to insert line numbers with s[ubstitute] command?

Hi,
using Vim 7.4 I have the following text:

aaa
bbb
ccc
<many more rows>

and I would like to add some text before each of line and number the line, so end result should look like:

AAA1BBBaaa
AAA2BBBbbb
AAA3BBBccc
<many more rows>

I tried doing it with two substitute commands:
:%s/^/AAA!BBB/g
First adding some text and adding ! character instead of line number, to get:

AAA!BBBaaa
AAA!BBBbbb
AAA!BBBccc

And then replace ! with line number:
:%s/!/\=line('.')/g

to get final result:

AA1BBBaaa
AAA2BBBbbb
AAA3BBBccc

How to do above operations with one command?

I tried with:
:%s/^/AAA\=line('.')BBB/g

but got:

AAA=line('.')BBBaaa
AAA=line('.')BBBbbb
AAA=line('.')BBBccc

So getting text displayed instead of line number. How to get this problem solved with one single command?

Thanks

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: