Loading ...
Sorry, an error occurred while loading the content.
 

Re: Maintaining patterns

Expand Messages
  • A.J.Mechelynck
    ... In Vim you can store the pattern in a variable or in a register. At the keyboard, you can recall a register in Command-line or Insert/Replace modes by
    Message 1 of 5 , Jan 26, 2007
      DervishD wrote:
      > Hi all :)
      >
      > Another weird question O:)
      >
      > Let's suppose that I search frequently for a very complex pattern.
      > This is easy to do using a keymap, for example. But let's assume that I
      > also search frequently using the same pattern preceded by an arbitrary
      > char *AND* followed by the same char. The char varies from search to
      > search.
      >
      > In Perl I would store the complex pattern in some scalar, but I
      > don't know how to do it in VimL. Probably it can be done with "let" and
      > "eval", but it won't work for syntax highlighting, AFAIC.

      In Vim you can store the pattern in a variable or in a register. At the
      keyboard, you can recall a register in Command-line or Insert/Replace modes by
      hitting Ctrl-R followed by the register name (e.g. ^R/ to recall the latest
      search pattern, ^Rw to recall register "w, ^R= followed by an expression then
      <Enter> to evaluate that expression, etc.)

      When interacting with Vim at the keyboard, you can also recall the previously
      used pattern from search history (using <Up> and <Down> after hitting / or ? )
      and modify it on the command-line before hitting <Enter> (or <Esc> to abord)
      on the modified pattern.

      Or you can use ":let" to set @/ (the search register) and immediately do
      searches based on the new value. For instance, to repeat the latest search but
      as a self-standing word:

      :let @/ = '\<' . @/ . '\>'

      >
      > Moreover, if I'm writing a syntax file and have a lot of syntax
      > items that contains the same complex pattern preceded and followed by a
      > character (this is only an example), that's difficult to maintain
      > because each time I have to change the complex pattern I have to change
      > it everywhere. Please note that this cannot be fixed modifying the
      > pattern adding an whatever\? atom at the front and end of it, because
      > the "whatever" MUST be present at BOTH ends.

      Any ex-command (including the ":syntax" command) can be constructed as a
      string expression, argument of the ":exe" command. Part of it can be the
      constant part of the pattern, use the . (concatenate) operator to cancatenate
      strings (any of these can be variables). In ":exe" as in ":echo", if there are
      more than one operands, they are concatenated with intervening spaces. Here's
      a silly example:

      let s:pattern = 'azertyuiopqsdfyghjklmwxcvbn'
      ...
      fun DefineSynMatch(groupname, FirstPart, LastPart)
      exe 'syn match'
      \ a:groupname '"'
      \ . a:FirstPart
      \ . s:pattern
      \ . a:LastPart
      \ . '"'
      endfun

      >
      > For the syntax highlighting, using "contains" solves the issue, but
      > again, this doesn't work for searches.
      >
      > In the end, the problem is to be able to reuse a pattern, no matter
      > if in a search, substitute, syntax highlighting, etc. And not, "\z(\)"
      > is not a solution because it only works in "start=" in syntax regions.
      > Is there a way of storing a pattern and reusing it in searchs,
      > substitutions, syntax highlighting, etc?
      >
      > Thanks a lot in advance :)
      >
      > Raúl Núñez de Arenas Coronado
      >



      Best regards,
      Tony.
    Your message has been successfully submitted and would be delivered to recipients shortly.