Tuesday, August 18, 2015

Re: Search in selection - difference in behaviour between ':s' and '/' (or ':/')

Le mardi 18 août 2015 à 03:05, trismarck a écrit:
> This is based on: http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection
> and was supposed not to be about \%V, which just works.
>
> Lets suppose that one does the following:
> - V, select a few lines
> - ':', followed by either 's' or '/': ':s' or ':/'
> - type a search pattern [and a replacement text for 's']
> If that's the case, then typing ':s/<pattern>/<replacement>' will replace text _within selection_ and typing ':/<pattern>' will search the text within _the entire file_. I don't understand why those two behave differently.
> 1. Could you describe why there is a difference in behaviour between the above.

:s[ubstitute] is a command that can operate on a range, here the
selection. :/<pattern> isn't a command at all, it is a line specifier:
it means the range is bounded by the next occurrence of <pattern>.
There should be a command after that: :/pattern/dosomething.

> 5. Why doesn't ":'<,'>/<pattern>" work 'as expected' - search for <pattern> within selection (selection specified by the special 'selection' range)?

It works as expected, but not as / (not :/) which is the proper search
command. If you want to search within a range, see ":help search-range".

> 6. What would it mean if after selecting a few lines, one would just type '/' (instead of ':/') (~so that the ranges '< and '> do not appear) - is the selection ignored entirely in this case and a search on a file is just performed?

The selection is ignored, but / in visual mode is used to extend the
selection, so it's normal it searches the whole file.

In other words: if you want to search inside the selection, do the
following: go into visual mode, leave it, and search with
"/\%V<pattern>\%V". If you want to move the cursor of the visual area
with a search, use / in visual mode. As for ":/", I guess you'll
understand it better when you really need it: apply a command on a
range delimited by a pattern.

Best,
Paul

--
--
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.
For more options, visit https://groups.google.com/d/optout.

No comments: