Define regexp

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

Define regexp

Zbigniew Kowalski
Dear vimmers,

Is it possible to "define" a regular expression so that it remembered
(.vimrc?) and i can just refer to the name of the regexp rather than
type the whole thing, eg. something like:
define myregexp = '^  *[1-9][0-9]\{3}-[A-Z0-9\-]*\.  *$'
and then in command line i just do:
:g/myregexp/s//something

best regards

Zbigniew Kowalski
http://zbikow1.webpark.pl/


Reply | Threaded
Open this post in threaded view
|

Re: Define regexp

Tim Chase-2
> Is it possible to "define" a regular expression so that it remembered
> (.vimrc?) and i can just refer to the name of the regexp rather than
> type the whole thing, eg. something like:
> define myregexp = '^  *[1-9][0-9]\{3}-[A-Z0-9\-]*\.  *$'
> and then in command line i just do:
> :g/myregexp/s//something


Well, you can ":let" your regexp into a register (or variable),
and then use control+R to insert that register (or control+R
followed by "=" where you can type the name of the register).

Thus, in your vimrc, you'd have something like this
(untested...escaping may be off)

   let @x = '^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$'

and then you can type

  :%s/<control+R followed by "x">/something

or alternatively, if you put it in a named regexp:

   let thing='^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$'

you can then use

   :%s/<control+R, "=", "thing", enter>/something

(in both cases, the stuff in the "<...>" are instructions for
inserting the value you want).

The former method (using a register) is faster, but consumes one
of your named registers.

Another option would be to do something like

   :cnoremap <f4> ^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$

(again, not 100% sure on the levels of escaping needed).  This
would allow you to just hit <f4> and have it be typed for you.

Three different ways to do it, each with their own plusses and
minuses. :)

-tim


Reply | Threaded
Open this post in threaded view
|

Re: Define regexp

A.J.Mechelynck
Tim Chase wrote:

>> Is it possible to "define" a regular expression so that it remembered
>> (.vimrc?) and i can just refer to the name of the regexp rather than
>> type the whole thing, eg. something like:
>> define myregexp = '^  *[1-9][0-9]\{3}-[A-Z0-9\-]*\.  *$'
>> and then in command line i just do:
>> :g/myregexp/s//something
>
>
> Well, you can ":let" your regexp into a register (or variable), and then
> use control+R to insert that register (or control+R followed by "="
> where you can type the name of the register).
>
> Thus, in your vimrc, you'd have something like this (untested...escaping
> may be off)
>
>   let @x = '^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$'

You don't even need to load it explicitly in the vimrc, registers are
remembered by the viminfo (see ":help viminfo").

>
> and then you can type
>
>  :%s/<control+R followed by "x">/something
>
> or alternatively, if you put it in a named regexp:
>
>   let thing='^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$'
>
> you can then use
>
>   :%s/<control+R, "=", "thing", enter>/something
>
> (in both cases, the stuff in the "<...>" are instructions for inserting
> the value you want).
>
> The former method (using a register) is faster, but consumes one of your
> named registers.
>
> Another option would be to do something like
>
>   :cnoremap <f4> ^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$
>
> (again, not 100% sure on the levels of escaping needed).  This would
> allow you to just hit <f4> and have it be typed for you.
>
> Three different ways to do it, each with their own plusses and minuses. :)
>
> -tim
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Define regexp

Tim Chase-2
>> let @x = '^ \+[1-9][0-9]\{3}-[-A-Z0-9]*. \+$'
>
> You don't even need to load it explicitly in the vimrc,
> registers are remembered by the viminfo (see ":help viminfo").

...as long as your copy of vim is

1) compiled with +viminfo
2) set to use viminfo (non-empty 'viminfo') and
3) the 'viminfo' set to remember registers (s >0)
4) the length of the saved text doesn't exceed either the kb
limit ("s") or the line-length limit ("<") of your viminfo setting

For the example at hand, #3 and #4 aren't likely to be
problematic, but #1 or #2 may.  Several of my boxes have viminfo
disabled because I like to know the state of my vim rather than
try to remember which vim process I last used and what
information it saved in my viminfo.  Not a big deal, but enough
to have been bothered by startup times enough to disable viminfo
on occasion.

However, as Tony says, if your viminfo is set to remember
registers, all you have to do is remember not to tromp over the
saved register, and you're good to go.

Just my $0.02

-tim