(possibly known) bug with visual block and search&replace

0 views
Skip to first unread message

Richard Hartmann

unread,
Jan 16, 2008, 8:14:03 AM1/16/08
to vim...@vim.org
Hi all,

I know there are some issues with visual block behaving like visual
lines sometimes, so this might just be somewhere in the future 'for
free', but here goes anyway :)


vim -u NONE -U NONE
"type any text over a few lines
<esc>
<ctrl-v>
:s/foo/bar/g

If foo appears on the same line as, but outside of, the visual block,
it is replaced. The expected behaviour is, of course, that only stuff
within the visual block is replaced.


Richard

Jürgen Krämer

unread,
Jan 16, 2008, 8:39:18 AM1/16/08
to vim...@googlegroups.com

Hi,

this is intended and documented behaviour. It's first mentioned below

:help visual-block

You can find the list of operators mentioned there with

:help visual-operators

Have a look at the footnotes of this table (especially footnote (1)).

The behaviour you want can be achieved with an additional script by
Dr. Chip. You can download it from the following url

http://www.vim.org/scripts/script.php?script_id=1195

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

James Vega

unread,
Jan 16, 2008, 9:53:01 AM1/16/08
to vim...@googlegroups.com
On Jan 16, 2008 8:39 AM, Jürgen Krämer <jott...@googlemail.com> wrote:
> The behaviour you want can be achieved with an additional script by
> Dr. Chip. You can download it from the following url

In Vim7 you can also use the \%V operator as described at ":help
/\%V". This will limit the match to only the area selected by the
visual block.

James

Jürgen Krämer

unread,
Jan 16, 2008, 10:26:11 AM1/16/08
to vim...@googlegroups.com

Hi,

which is a bit awkward to use if you want to make sure that the search
text is completely inside the block, e.g.,

+----+
I. this is |foo |text
II. this is| foo| text
III. this i|s fo|o text
+----+

with a 4x3 block (marked by lines)

'<,'>s/\%Vfoo/bar/

replaces all three occurences of foo.

'<,'>s/\%Vfoo\%V/bar/

only replaces the first one, because \%V only anchors the following
char to the visual block -- and the space after foo is outside of the
block.

'<,'>s/\%Vfo\%Vo/bar/

works (i.e., replaces foo two times), but it is not very intuitive and
does not work in all cases, especially with multis:

+---+
I. this is |bar|r text
II. this is| ba|rr text
III. this i|s b|arr text
+---+

:'<,'>s/\%Vba\%Vr\+/foo/

yields

I. this is foo text
II. this is barr text
III. this is barr text

Note that the first barr has been completely replaced by foo, although
the trailing r should have been kept.

You also cannot anchor the search text to the start of the visual block.
I guess special atoms would be needed that match before the first and
after the last column of a visual block.

Charles E Campbell Jr

unread,
Jan 16, 2008, 12:24:30 PM1/16/08
to vim...@googlegroups.com
Jürgen Krämer wrote:

>Hi,
>
>James Vega wrote:
>
>
>>On Jan 16, 2008 8:39 AM, Jürgen Krämer <jott...@googlemail.com> wrote:
>>
>>
>>>The behaviour you want can be achieved with an additional script by
>>>Dr. Chip. You can download it from the following url
>>>
>>>
>>In Vim7 you can also use the \%V operator as described at ":help
>>/\%V". This will limit the match to only the area selected by the
>>visual block.
>>
>>
>
>which is a bit awkward to use if you want to make sure that the search
>text is completely inside the block, e.g.,
>
> +----+
> I. this is |foo |text
> II. this is| foo| text
> III. this i|s fo|o text
> +----+
>
>with a 4x3 block (marked by lines)
>
> '<,'>s/\%Vfoo/bar/
>
>replaces all three occurences of foo.
>
>

However, my plugin does handle this case, replacing only the two "foo"s:

:'<,'>B s/foo/bar/

Regards,
Chip Campbell

Richard Hartmann

unread,
Jan 17, 2008, 5:20:30 AM1/17/08
to vim...@googlegroups.com
On Jan 16, 2008 2:39 PM, Jürgen Krämer <jott...@googlemail.com> wrote:


> this is intended and documented behaviour. It's first mentioned below

> Have a look at the footnotes of this table (especially footnote (1)).

Argh, I totally missed that. Sorry for sending this mail in the first place :/

I could not find the actual rationale for this behaviour, though. Not that I
am trying to argue it, I am just interested in why this is. Possible
explanations would be

1) a good reason that has escaped me
2) a known caveat that is not urgent to handle as Dr Chip has a script,
anyway


> The behaviour you want can be achieved with an additional script by
> Dr. Chip. You can download it from the following url

Thanks a lot :)


Richard

Yegappan Lakshmanan

unread,
Jan 17, 2008, 12:53:00 PM1/17/08
to vim...@googlegroups.com
On Jan 17, 2008 2:20 AM, Richard Hartmann <richih.ma...@gmail.com> wrote:
> On Jan 16, 2008 2:39 PM, Jürgen Krämer <jott...@googlemail.com> wrote:
>
>
> > this is intended and documented behaviour. It's first mentioned below
> > Have a look at the footnotes of this table (especially footnote (1)).
>
> Argh, I totally missed that. Sorry for sending this mail in the first place :/
>
> I could not find the actual rationale for this behaviour, though. Not that I
> am trying to argue it, I am just interested in why this is. Possible
> explanations would be
>
> 1) a good reason that has escaped me
> 2) a known caveat that is not urgent to handle as Dr Chip has a script,
> anyway
>

3) It is in the todo list:

- Add COLUMN NUMBERS to ":" commands ":line1,line2[col1,col2]cmd". Block
can be selected with CTRL-V. Allow '$' (end of line) for col2.
7 CTRL-V :s should substitute only in the block, not to whole lines. (David
Young is working on this)
7 Filtering a block should only apply to the block, not to the whole lines.
When the number of lines is increased, add lines. When decreased, pad with
spaces or delete? Use ":`<,`>" on the command line.
6 ":left" and ":right" should work in Visual block mode.
7 Substitute in a block of text. Use {line}.{column} notation in an Ex
range, e.g.: ":1.3,$.5s" means to substitute from line 1 column 3 to the
last line column 5.
8 Add put command that replaces the text under it. Esp. for blockwise
Visual mode.

This will be a useful feature to add compared to the support for parallel
editing of the same file.

- Yegappan

Richard Hartmann

unread,
Jan 18, 2008, 4:39:05 PM1/18/08
to vim...@googlegroups.com
On Jan 17, 2008 6:53 PM, Yegappan Lakshmanan <yega...@gmail.com> wrote:


> 3) It is in the todo list:

Ah, OK, thanks :)


Richard

Reply all
Reply to author
Forward
0 new messages