How to replace characters in visual block selection

108 views
Skip to first unread message

Igor

unread,
Jan 10, 2024, 3:48:12 AMJan 10
to vim_use
I use vim 9.1.0016 on Windows 10.

In second column I would like to replace all A characters with X characters.

Sample:
AAA AAA
AAA AAA


After change:
AAA XXX
AAA XXX


Above is very simple sample. In my case I have text of words, not just letter A.

I did:
My cursor after opening a file is at first character in top left as expected.
1. Move 4 characters to the right: 4l
2. To get into visual block: CTRL+V
3. Select current and line bellow: j
4. Select text to tend of line: $
5. Now second column is selected. I would like do the substitution with: :s/A/X/g
And now I get:
XXX XXX
XXX XXX


How to substitute characters inside visual block?
Regards

Eric Pruitt

unread,
Jan 10, 2024, 3:56:26 AMJan 10
to vim...@googlegroups.com
On Wed, Jan 10, 2024 at 12:48:12AM -0800, Igor wrote:

How to substitute characters inside visual block? Regards

Use the %V modifier to restrict the replacements to the selected area i.e. :s/%VA/X/g

Eric

Igor

unread,
Jan 10, 2024, 4:07:22 AMJan 10
to vim_use
@Eric,  in my case I was required to escape % character with \%
In my case it works: :s/\%VA/X/g
Thanks for help.

aro...@vex.net

unread,
Jan 10, 2024, 9:42:27 AMJan 10
to vim...@googlegroups.com

> In second column I would like to replace all A characters with X
> characters.
>

If your example describes the problem completely, :%s/ AAA/ XXX/ should do
the trick. Note the space to separate the columns.

Eike Rathke

unread,
Jan 10, 2024, 5:24:03 PMJan 10
to vim...@googlegroups.com
Hi Igor,

On Wednesday, 2024-01-10 00:48:12 -0800, Igor wrote:

> I did:
> My cursor after opening a file is at first character in top left as
> expected.
> 1. Move 4 characters to the right: 4l
> 2. To get into visual block: CTRL+V
> 3. Select current and line bellow: j
> 4. Select text to tend of line: $
> 5. Now second column is selected. I would like do the substitution with:
> :s/A/X/g
> And now I get:
> XXX XXX
> XXX XXX
>
> How to substitute characters inside visual block?

Instead, with the block selection still intact, simply press s for
substitute and enter XXX and then Escape or Ctrl+[
It will substitute on all lines of the selected block.

Eike

--
OpenPGP/GnuPG encrypted mail preferred in all private communication.
GPG key 0x6A6CD5B765632D3A - 2265 D7F3 A7B0 95CC 3918 630B 6A6C D5B7 6563 2D3A
Use LibreOffice! https://www.libreoffice.org/
signature.asc

Igbanam Ogbuluijah

unread,
Jan 12, 2024, 6:42:10 AMJan 12
to vim...@googlegroups.com
Vim functions which take ranges take line ranges. So :'<,'>s/A/X/g — which would be the ensuing command — would start from "the beginning line of the selection" to "the last line of the selection". A substitution on that will do what is expected.

For your case, you should try a more targeted regex. I came up with :'<,'>s/\w\+ \zsAAA/XXX but my regex-fu is not at black belt yet.

Hope this helps

--
--
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/ZZ8Y-Rk-qY2RDF-U%40kulungile.erack.de.
Reply all
Reply to author
Forward
0 new messages