help on %s feature in gvim

0 views
Skip to first unread message

sh.v...@gmail.com

unread,
Nov 24, 2008, 11:34:11 PM11/24/08
to vim_use
In a C Source file,
there are a bunch of variables as

tv[1]
tv[2]
....
tv[234]


I want to replace them such that first '[' is replaced by '_' and
second one deleted. So file has replaced new names as

tv_1
tv_2
...
tv_234

can somebody help me with this?

%s/tv\[/tv_/g
replaces he first but how to distinguish the second from other similar
cases.

Cyril Slobin

unread,
Nov 25, 2008, 12:11:35 AM11/25/08
to vim...@googlegroups.com
On 11/25/08, sh.v...@gmail.com <sh.v...@gmail.com> wrote:

> %s/tv\[/tv_/g
> replaces he first but how to distinguish the second from other
> similar cases.

%s/tv\[\(\d\+\)\]/tv_\1/

--
http://slobin.pp.ru/ `When I use a word,' Humpty Dumpty said,
<cy...@slobin.pp.ru> `it means just what I choose it to mean'

Cyril Slobin

unread,
Nov 25, 2008, 12:12:32 AM11/25/08
to vim...@googlegroups.com
On 11/25/08, Cyril Slobin <cy...@slobin.pp.ru> wrote:

> %s/tv\[\(\d\+\)\]/tv_\1/

Oops! /g modifier forgotten!

%s/tv\[\(\d\+\)\]/tv_\1/g

xulxer

unread,
Nov 25, 2008, 2:43:01 AM11/25/08
to vim...@googlegroups.com
Hi,

i would use:

%s~tv\[\([^\]]*\)\]~tv_\1~g

which means replace "tv" followed by "[" followed by anything except a "]"
and save the content between "[" and "]" in \1 by tv_ and the saved
content in \1.

Kind regards

Chris

Reply all
Reply to author
Forward
0 new messages