Saturday, September 27, 2014

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

Hi Igor!

On Sa, 27 Sep 2014, Igor Forca wrote:

> 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?

You are almost there. The \= needs to be the first char in the
replacement part. So Try this:

:%s/^/\=printf('AAA%dBBB', line('.'))/

Best,
Christian
--
Frage an Radio Eriwan:
"Ist es wahr, daß der liebe Gott Parteigenosse werden kann?"
Antwort:
"Im Prinzip ja, nur müsste er vorher aus der Kirche austreten."

--
--
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:

Post a Comment