Monday, September 13, 2010

Re: Mark selection for replace

On 09/13/10 08:25, Dotan Cohen wrote:
> How does one mark a selection for search / replace in VIM?

In addition to Tom's suggestion of visually selecting the range,
I'll add a few tidbits:

1) Tom's suggestion works for line-wise selections, but _also_
treats block-wise and character-wise selections as line-wise
which can produce somewhat surprising results if you don't expect
it. As such, you can add the "\%V" atom to your search regexp to
limit it to within the bounds defined by your visual-selection.
See :help /\%V

2) You can use the :g[lobal] command to perform replacements on
disjoint lines found by a pattern, such as

:g/foo/s/bar/baz/g

which will perform "s/bar/baz/g" on all lines containing "foo",
while leaving lines with "bar" but not "foo" untouched.

3) the :s command can take any arbitrary range, not limited to
the range '<,'> defined by a visual selection. So you can do
things like

:10,20s/foo/bar/g

which will limit the substitution to lines 10-20. Or, more
complexly:

:5/CHAPTER/,$?APENDIX?-s/foo/bar/g

which defines the range

5 beginning on line 5
/CHAPTER/ search for "CHAPTER" as the range-start
, through
$ the end of the file
?APENDIX? going backwards until "APENDIX" is found
- (implicitly "-1") the line before that

which roughly translates as "from the first 'CHAPTER' after line
5 through the line before the last 'APENDIX', perform the
following :s[ubstitute]".

3) You can combine #2 and #3 for things like

:g/foo/'{,'}s/baz/bar/g

which will look for all instances of "foo" and then find the
paragraph around them ("'{" is the paragraph-break before the
position of interest, and "'}" is the paragraph-break after) and
perform the substitute in that paragraph.

> By the way, while googling this I see alternating use between the
> words "find" and "search". Are the two words synonyms in this context,
> or are amateur journalists (bloggers) getting them confused (not
> restricted to the VIM community)?

I think for the most part they're synonyms and I use them fairly
interchangeably depending on what I'm trying to describe: the
process of doing the searching (for something that may or may-not
be found), or the accomplished/impending match which is in my
mind a find. But that's just my niggling and often inconsistent,
so I'd just treat them as syns.

-tim


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

No comments: