> Thank you (and Jürgen too), @@ is an easy first step for
> me.

Glad to have some easy first steps to help you out.

>> Alternatively, problems can often be rephrased in terms
>> of an Ex command that uses the ":%s" or ":g"/":v" to
>> perform changes across the entire file.
>
> [...]
>
>> By changing your thinking to exploit these commands,
>> sometimes you can get easy consistent changes without
>> having to manually touch each bit with a macro.
>
> I admit that I am currently more of a visual "n.n.nn.nn." kind
> of person. I should take some time to get into using Ex.
>
> If I may seek further guidance with a concrete example:

Of course :)

> While editing a file, I decide to rename "someIdentifier" to
> "someIdentifier_" - I will need to append the underscore to
> several (but usually not all) instances of the word.

The typical way to do this would be something like

  :%s/\<someIdentifier\>/&_/g

If you want confirmation, you can use

  :%s/\<someIdentifier\>/&_/gc

which will prompt you for each instance of "someIdentifier"
that it finds, and append an underscore if you answer "y" to
the question.

The "\<" and "\>" ensure that there are word-boundaries (as
defined by your 'iskeyword' setting) which prevent it from
finding and replacing things like "wholesomeIdentifier" and
"someIdentifier2"

The ampersand in the replacement stands for "whatever it was
that I found, drop it here in the replacement".  This is
shorthand for

  :%s/\<someIdentifier\>/someIdentifier_/g

> The same would happen if I want to rename "wonderfulFoo" to
>
> "wonderfulBar".  I tend to type:   *fFceBar<ESC>

Similarly, one would do something like

  :%s/\<wonderfulFoo\>/wonderfulBar/g

(again with the "c"onfirmation flag if you want to confirm
each replacement)

You can read about ":s"ubstitute commands and the various
flags at

  :help :s
  :help :s_flags

This is a jet-fuel-powered version of search-and-replace
that one finds in most editors.  The {pattern} portion has
an incredible degree of complexity for finding precisely the
match you intend, including context, repetitions, and the
like.  Far too much erudition can be found at

  :help pattern

There are also tricks that can be done in the replacement
portion as well:

  :help sub-replace-special


It's prob. more than you want/need at the moment, but after
tapping the power of vim's search&replace, it bugs me to use
s&r in any other app.

> But then I cannot use "n.nn." to repeat (but maybe "n;.nn;.").
> How would I use Ex or another approach to save me some typing
> during the process described above (for example repeatedly
> appending '_' to an identifier) ?

If you have trouble with the above, you can do some
transitional work as long as you don't mind a little mental
arithmetic.  For you first example, you can use the regular
search command as you normally do:

  /someIdentifier

but append

  /someIdentifier/e

which will drop your cursor at the end of the match (where
you can use "a_" followed by <esc> to append the underscore)
rather than at the beginning of it.  This will allow you to
use your "n.nn.n.nn." method, as it will put you in the
right place.  For your second example, you have to do a
little tweaking, as you want to be 3 characters from the
end, you have to use

  /someIdentifier/e-2

(curses on those silly fence-post issues) followed by
"ceBar" followed by <esc> to "c"hange to the "e"nd of the
word.  This too is repeatable with your "n.nn.n.n." pattern.
This is because the "n" and "N" operators remember the
offset as well, so they position you at the right place.

To read more on this, you can hit the help at

  :help search-offset

> And can this trick still be easily applied if only some instances
> of the identifiers are to be replaced ?

If you can identify them by certain factors, then vim can
handle them.

If, for instance, you only wanted to do the above
search&replace on lines containing "function" at the
beginning of the line, you could use

  :g/^function/s/\<wonderfulFoo\>/wonderfulBar/g

Things can get crazy-complex when you start building these,
but the expressive power allows you the freedom to make
surgically precise strikes on your text, for things like
"replace every instance of 'footnoteN' where N is an
arbitrary positive integer, with 3 added to N, but only from
the 314th line of my file through the end of text as
determined by the last line containing the word 'APPENDIX'
from the end of the file" (avert your eyes if you're easily
daunted)

:314,$?APPENDIX?s/footnote\zs\(\d\+\)/\=submatch(0)+3/g

from which you can discern the basic skeleton:

  :{range}s/{pattern}/{replacement}/{flags}

where
  range = 314,$?APPENDIX?
  pattern = footnote\zs\(\d\+\)
  replacement= \=submatch(0)+3
  flags = g

(pedant's note: if APPENDIX falls on the last line, it won't
find it)

> [ wow... I'll be called a nuthead for asking such a question
> anywhere else on the net ... hopefully not here ;) ]

The vim-list is yet one more of Vim's amazing assets.  It's
a friendly bunch, and rather on-topic (barring
semi-off-topic discussions about moving the tips/scripts bit
of vim.org to a wiki).  I've been a regular user of Vim for
nearly 7 years, and still regularly learn things sipping at
the list's firehose.

Hope this helps,

-tim





Reply via email to