How pair <g> </g>

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How pair <g> </g>

Peng Yu
Hi,

I'm writing some xml code in vim. In xml, there are some pair like <g>
</g>. Would you please let me know how to pair them as "{" and "}"
such that I can us % to visit them?

Thanks,
Peng
Reply | Threaded
Open this post in threaded view
|

Re: How pair <g> </g>

Thomas Holder
Peng Yu wrote:
> I'm writing some xml code in vim. In xml, there are some pair like
> <g> </g>. Would you please let me know how to pair them as "{" and
> "}" such that I can us % to visit them?

source $VIMRUNTIME/macros/matchit.vim
let b:match_words = '<g>:</g>'

see $VIMRUNTIME/macros/matchit.txt for help.

Regards,
  Thomas
Reply | Threaded
Open this post in threaded view
|

Re: How pair <g> </g>

iler.ml
On 9/18/06, Thomas Holder <[hidden email]> wrote:
> Peng Yu wrote:
> > I'm writing some xml code in vim. In xml, there are some pair like
> > <g> </g>. Would you please let me know how to pair them as "{" and
> > "}" such that I can us % to visit them?
>
> source $VIMRUNTIME/macros/matchit.vim
> let b:match_words = '<g>:</g>'

Hmm, the set of xml tags that I have is large and
basically open-ended. Do you mean, there is no method
to let plugin handle *any* <...> tag, automatically ?
Isn't it unproductive to add manually each and every tag
to b:match_words ?

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: How pair <g> </g>

Thomas Holder
Yakov Lerner wrote:

> On 9/18/06, Thomas Holder <[hidden email]> wrote:
>> Peng Yu wrote:
>> > I'm writing some xml code in vim. In xml, there are some pair like
>> > <g> </g>. Would you please let me know how to pair them as "{" and
>> > "}" such that I can us % to visit them?
>>
>> source $VIMRUNTIME/macros/matchit.vim
>> let b:match_words = '<g>:</g>'
>
> Hmm, the set of xml tags that I have is large and
> basically open-ended. Do you mean, there is no method
> to let plugin handle *any* <...> tag, automatically ?
> Isn't it unproductive to add manually each and every tag
> to b:match_words ?

Try this:

let b:match_words = '<\(\w\+\)\(\s[^>]*\)\?>:</\1>'

This pattern also allows attributes inside the opening tag after some
space (but not newline). You could figure this out yourself if you would
read the help file for this macro.

Regards,
  Thomas
Reply | Threaded
Open this post in threaded view
|

Re: How pair <g> </g>

A.J.Mechelynck
In reply to this post by Peng Yu
Peng Yu wrote:
> Hi,
>
> I'm writing some xml code in vim. In xml, there are some pair like <g>
> </g>. Would you please let me know how to pair them as "{" and "}"
> such that I can us % to visit them?
>
> Thanks,
> Peng
>

- Install and use the matchit plugin (e.g., add a file
"$VIM/vimfiles/plugin/matchit.vim" with the single line

        runtime macros/matchit.vim

)

- I don't know how the xml.vim ftplugin does it; but HTML has similar
paired tags, and my HTML files have the b:match_words variable set to
'<:>,<\@<=[ou]l\>[^>]*\%(>\|$\):<\@<=li\>:<\@<=/[ou]l>,<\@<=dl\>[^>]*\%(>\|$\):<\@<=d[td]\>:<\@<=/dl>,<\@<=\([^/][^
\t>]*\)[^>]*\%(>\|$\):<\@<=/\1>' (I don't do it, ftplugin/html.vim does it).

This pairs < with >, most <tag>s with the corresponding </tag>, and <ol>
or <ul> with any number of <li> with </ol> or </ul> (the cursor jumps
from < to > or vice-versa when on < or >, from the openiung tag to the
closing tag when on the tag name itself).


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: How pair <g> </g>

Benji Fisher
In reply to this post by Thomas Holder
On Mon, Sep 18, 2006 at 10:23:27PM +0200, Thomas Holder wrote:

> Yakov Lerner wrote:
> > On 9/18/06, Thomas Holder <[hidden email]> wrote:
> >> Peng Yu wrote:
> >> > I'm writing some xml code in vim. In xml, there are some pair like
> >> > <g> </g>. Would you please let me know how to pair them as "{" and
> >> > "}" such that I can us % to visit them?
> >>
> >> source $VIMRUNTIME/macros/matchit.vim
> >> let b:match_words = '<g>:</g>'
> >
> > Hmm, the set of xml tags that I have is large and
> > basically open-ended. Do you mean, there is no method
> > to let plugin handle *any* <...> tag, automatically ?
> > Isn't it unproductive to add manually each and every tag
> > to b:match_words ?
>
> Try this:
>
> let b:match_words = '<\(\w\+\)\(\s[^>]*\)\?>:</\1>'
>
> This pattern also allows attributes inside the opening tag after some
> space (but not newline). You could figure this out yourself if you would
> read the help file for this macro.
>
> Regards,
>   Thomas

     Do not reinvent the wheel!  Assuming that you have filetype support
enabled, i.e., something like

:filetype plugin on

in your vimrc file, $VIMRUNTIME/ftplugin/xml.vim will be :source'd
automatically whenever you edit an xml file.  It already sets
b:match_words to something reasonable.  To test, try

:e foo.xml
:echo b:match_words

     If you want to use matchit rather than default % pairing all the
time, add something like

source $VIMRUNTIME/macros/matchit.vim

or

runtime macros/matchit.vim

to your vimrc file.

     Historical note:  Johannes Zellner suggested adding support for
variable matching pairs (such as matching <anything> with </anything> in
*ML) shortly after I started maintaining the script.  Implementing that
suggestion was complicated enough that I started to think of it as my
script, rather than just something I had tinkered with.

HTH --Benji Fisher