Wednesday, April 9, 2014

Re: systematic replacement of text

On 9 April 2014, LCD 47 <lcd047@gmail.com> wrote:
> On 9 April 2014, Natércia Fernandes <natercia@eq.uc.pt> wrote:
> > Hi,
> >
> > In a file with many occurrences of something like this:
> >
> > \xxx{a variable number of words that I want to delete}{a variable (diferent)
> > number of words that I want to keep}
> >
> > I would like to eliminate at once in all occurrences in the file the following:
> > \xxx
> > the two pairs of {}
> > the content of the first {} independently of its size (which can be different
> > from occurrence to occurrence)
> >
> > That is, I want to keep only the content of the second pair of {} above.
> > In this case, I would like to obtain:
> > a variable (diferent) number of words that I want to keep
> >
> > \xxx{argument 1}{argument 2} can exist more than once in a line, but never
> > will exist a \xxx in the arguments of another \xxx.
> >
> > If "argument 1" and "argument 2" were kept in all occurrences, I could solve
> > my problem with something like this:
> > :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
> > but my problem is that argument 1 and argument 2 are different from occurrence
> > to occurrence.
> > Don't know if this info is needed or not: both argument 1 and/or argument 2
> > can contain the symbols \ {}.
> >
> > Can someone help me?
>
> Write a macro. Make it recursive to get all matches. Let % take
> care of balancing { and } and handle \ escapes.
>
> qqq - clear out register q
> qq - start recording a macro in register q
> /\\xxx\> - search for \xxx
> ^M - press enter to finish the search
> dt{ - delete to the next {
> d% - delete to the matching } (deletes {argument 1})
> l - move right
> "x - in register x...
> y - ... yank...
> ]} - ... to the matching } (saves argument 2)
> h - move right
> d% - delete to the matching } (deletes {argument 2})
> "xP - paste from register x (pastes argument 2)
> @q - make the macro invoke itself
> q - finish macro recording.
>
> Now run @q and watch.

A shorter version, with a less awkward way to remove the { ... }
around "argument 2":

qqq - clear out register q
qq - start recording a macro in register q
/\\xxx\> - search for \xxx
^M - press enter to finish the search
dt{ - delete to the next {
d% - delete to the matching } (deletes {argument 1})
% - jump to the final }
x - delete the final }
`` - go back to the position before jump
x - delete the {
@q - make the macro invoke itself
q - finish macro recording.

/lcd

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

Post a Comment