4

I just discovered you can't search and replace within a block selection without adding the ugly and unintuitive \%V to the search term. The default behaviour seems to be to ignore the fact that you've done a columnular highlight and operate on whatever lines the highlight touches.

Would it not have made more sense to have '<,'> default to "operate on the highlighted text" - whether columnular or not - , and to allow the user to specify some hack to operate on the whole lines instead of the highlighted block if that is what is desired?

Is the current behaviour there for historical reasons, or is there a rational reason for it? I'm assuming there's no way of getting the behaviour I'm after without a plugin?

3

The current behavior is probably due to historical reason: the visual block seems to have introduced in version 5, and the notes for this version 5 also mentions an update on the marks '</'> behavior, so visual block is newer.

Despite you find the behavior unintuitive, the current implementation allows you to work on both the visual block or the selected lines.

You wouldn't need a plugin to avoid that notation; a simple mapping or abbreviation can be used instead.


Edit:

How might I use a mapping or abbreviation to get the desired behaviour?

The Vim documentation is very comprehensive and well written; you can find a good explanation in :help 05.3 and :help 24.7. One possibility in this case is to include the following in your vimrc:

vnoremap : :s/\%V

Explanation:

  • vnoremap :: override the default action of : when in visual mode; doesn't trigger any additional mapping after insert the alternative keys (the alternative would be vmap)
  • :s/\%V: insert your preferred default for : in visual selection, so the command line will end up with :'<,'>s/\%V
  • How might I use a mapping or abbreviation to get the desired behaviour? – user859 Dec 6 '16 at 11:05
  • That mapping doesn't work for me; I get the error "E10 \ should be followed by /, ? or &" I've got further with "vnoremap : :s/\%V" I'm assuming the intention is to control-v a block of text first then type : – user859 Dec 6 '16 at 14:29
  • 1
    that mapping is nonsense – Christian Brabandt Dec 6 '16 at 17:31
  • @Alex indeed, you are correct. I've updated the answer. – mMontu Dec 7 '16 at 10:30
  • @ChristianBrabandt yep, I made a mistake. Fortunately that was still useful for the OP – mMontu Dec 7 '16 at 10:32
3

This indeed has something to do with history: The command ranges are always defined in terms of entire lines (because that's what the Ex editor could handle, and vi was based on that). So, even though marks record both line and column numbers, when passed to an Ex command (like :s), only the line number information is used.

Of course, one could implement extensions around that (for example, use `<,`> to denote block ranges). But as the internal implementations of Ex commands are based on lines, and there are quite a few of them, and there's still the question how to deal with commands that don't apply [well] to blocks, this huge change hasn't been done yet, and may never be.

Yes, the \%V special atom is one workaround. I would recommend the (almost as old as Vim) vis.vim plugin, though. It offers a simple drop-in replacement :'<,'>S command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy