Wednesday, September 27, 2023

Re: Replace spaces with underscore after some pattern in line

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


On Wed, 27 Sept 2023 at 12:50, 'c.willis111' via vim_use <vim_use@googlegroups.com> wrote:



------ Original Message ------
From: "Igor" <igor2x@gmail.com>
To: "vim_use" <vim_use@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




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

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

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CALe4Hp%3DTJ647Czy3_8f9LNjHzv7s_chvBZznFVgG1E1OgfXOiQ%40mail.gmail.com.

No comments:

Post a Comment