Replace spaces with underscore after some pattern in line

145 views
Skip to first unread message

Igor

unread,
Sep 27, 2023, 7:09:41 AM9/27/23
to vim_use
How to replace all of the spaces in each line after XXX string?

Input sample data:
aaaaaa bbbbbb cccc XXX aaaaa bbbbbbb
cccccccc eeeeeeeeee XXX aaa bbbb cccc ddd
aaaa bbbb ddd ee XXX aaa bbb dddd eee


Output sample data:
aaaaaa bbbbbb cccc XXX_aaaaa_bbbbbbb
cccccccc eeeeeeeeee XXX_aaa_bbbb_cccc_ddd
aaaa bbbb ddd ee XXX aaa_bbb_dddd_eee




c.willis111

unread,
Sep 27, 2023, 7:50:38 AM9/27/23
to vim...@googlegroups.com
--
Hi

to my slight surprise this works:


:%s/\(XXX[^ ]*\) /\1_/g


The reason I had doubts is that the successive replacements start at the beginning of the earlier replacement.


(Sorry about layout. I'm not into this add at the end bit).


regards - Chris

c.willis111

unread,
Sep 27, 2023, 7:55:23 AM9/27/23
to vim...@googlegroups.com

Oh no it doesn't. One needs to repeat until all done. The g is pointless. - Chris

Mikhail Velikikh

unread,
Sep 27, 2023, 8:05:36 AM9/27/23
to vim...@googlegroups.com
:%s/\(XXX[^ ]*\) /\1_/g

This one replaces just the first space. E.g. the first line becomes:
aaaaaa bbbbbb cccc XXX_aaaaa bbbbbbb
Rather than (notice the second underscore after 'aaaaa'):
aaaaaa bbbbbb cccc XXX_aaaaa_bbbbbbb

I came up with either:

%norm /XXX^Mv$:s/\%V /_/g^M

%V will limit the substitute to the visual selection

Or:

%s/XXX\zs.*/\=substitute(submatch(0), " ", "_", "g")/

 

BTW, the last line is missing an underscore after XXX. Would love to see a simpler command.


--
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/37ceea77.880a.18ad67928c5.Webtop.99%40btinternet.com.

Christian Brabandt

unread,
Sep 27, 2023, 8:17:01 AM9/27/23
to vim...@googlegroups.com
That is a bit tricky. I would probably use something like this:

:%s/\(XXX.*\)$/\=substitute(submatch(1), ' ', '_', 'g')/

Which basically grabs everything after the 'XXX' and replaces in that
submatch all whitespaces using a sub-replace-expression (see :h
sub-replace-expression and :h sub-replace-special).

Thanks,
Christian
--
Don't go surfing in South Dakota for a while.

Igor

unread,
Sep 28, 2023, 1:27:27 AM9/28/23
to vim_use
All of the solutions work great.
Thanks to you all.

wenxin Wang

unread,
Sep 28, 2023, 2:19:25 AM9/28/23
to "'c.willis111' via vim_use"


On Wed, 27 Sep 2023, "'c.willis111' via vim_use" wrote:
> --
> Hi
>
> to my slight surprise this works:
>
>
> :%s/\(XXX[^ ]*\) /\1_/g
>
>
> The reason I had doubts is that the successive replacements start at the beginning of the earlier replacement.
>
>
> (Sorry about layout. I'm not into this add at the end bit).
>
>
> regards - Chris
>
>
> Oh no it doesn't. One needs to repeat until all done. The g is pointless. - Chris

I am a newbie to vim, but would like to learn it . Please tell me what
exactly does 1_ do in your command ? I knew that you wanted to substitute by underscore, but why
1 was there?

Arun

unread,
Sep 28, 2023, 2:47:10 AM9/28/23
to vim...@googlegroups.com
On Wed, Sep 27, 2023 at 4:50 AM 'c.willis111' via vim_use <vim...@googlegroups.com> wrote:

------ Original Message ------
From: "Igor" <igo...@gmail.com>
To: "vim_use" <vim...@googlegroups.com>
Sent: Wednesday, 27 Sep, 2023 At 12:09
Subject: Replace spaces with underscore after some pattern in line

How to replace all of the spaces in each line after XXX string?

Input sample data:
aaaaaa bbbbbb cccc XXX aaaaa bbbbbbb
cccccccc eeeeeeeeee XXX aaa bbbb cccc ddd
aaaa bbbb ddd ee XXX aaa bbb dddd eee


Output sample data:
aaaaaa bbbbbb cccc XXX_aaaaa_bbbbbbb
cccccccc eeeeeeeeee XXX_aaa_bbbb_cccc_ddd
aaaa bbbb ddd ee XXX aaa_bbb_dddd_eee


 
Zero width patterns are helpful here. Try,

:%s/\(XXX.*\)\@<=\s/_/g

Here, the expression says, any whitespace (\s) that is preceded by "XXX.*" be replaced with "_".

:help \@<=

Regards,
-Arun 

steve

unread,
Sep 28, 2023, 7:25:11 AM9/28/23
to vim...@googlegroups.com
Hi,

I hope I won't say a stupidity…

Le 28-09-2023, à 14:18:22 +0800, wenxin Wang a écrit :

>>:%s/\(XXX[^ ]*\) /\1_/g
>I am a newbie to vim, but would like to learn it . Please tell me what
>exactly does 1_ do in your command ? I knew that you wanted to substitute by underscore, but why
>1 was there?

It's not the '_1' that is important here, it the '\1', which is a back
reference to what is in between the (). So everything in there will be
used in the substitution and to that you add '_'.

Reply all
Reply to author
Forward
0 new messages