Two search patterns

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

Two search patterns

Tim Chase
Per the help at

   :help last-pattern
   """
     Note that two patterns are remembered: One for 'normal'
   search commands and one for the substitute command ":s".
   Each time an empty pattern is given, the previously used
   pattern is used.
   """


There seems to be a peculiarly unidirectional defaulting:

Case #1:

Start Vim (-u NONE) on a document
Search for a pattern with "/"

   /pattern

perform an empty substitute:

   :%s//whatever/g

A blank :s pattern reaches for the most recent "/" pattern.
Nice, I use this regularly.


Case #2:

Start Vim (-u NONE) on a document
Perform a replacement (either a null-op, or just the
first-in-a-line with further matches later in the line, or a
replacement that allows for the pattern to be found again)

   :%s/pattern/&

Hit "n" to find the next instance of the pattern or use "/" with
an empty pattern.  I get

   E35: No previous regular expression

A blank search *doesn't* reach into the most recent ":s" pattern.
  I reach for this occasionally and get stung by it every time.

However, if I do a

   :%s//whatever

it manages to find that pattern.  Likewise if I ":echo @/" it
correctly returns my :s pattern that I want to use for my n/N
command (or "/" with no regexp).


It seems a little inconsistent to find the search pattern in the
first case, but to not find the pattern in the second case.


Is this a Vim bug (calling for n/N to use the search register), a
documentation bug (at a minimum requiring clarification), or just
a "suck it up and deal with the oddity" situation?

-tim




--
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
Reply | Threaded
Open this post in threaded view
|

Re: Two search patterns

Christian Brabandt

On So, 23 Jan 2011, Tim Chase wrote:

> Per the help at
>
>   :help last-pattern
>   """
>     Note that two patterns are remembered: One for 'normal'
>   search commands and one for the substitute command ":s".
>   Each time an empty pattern is given, the previously used
>   pattern is used.
>   """
>
>
> There seems to be a peculiarly unidirectional defaulting:
>
> Case #1:
>
> Start Vim (-u NONE) on a document
> Search for a pattern with "/"
>
>   /pattern
>
> perform an empty substitute:
>
>   :%s//whatever/g
>
> A blank :s pattern reaches for the most recent "/" pattern. Nice, I use
> this regularly.
>
>
> Case #2:
>
> Start Vim (-u NONE) on a document
> Perform a replacement (either a null-op, or just the first-in-a-line with
> further matches later in the line, or a replacement that allows for the
> pattern to be found again)
>
>   :%s/pattern/&
>
> Hit "n" to find the next instance of the pattern or use "/" with an empty
> pattern.  I get
>
>   E35: No previous regular expression
>
> A blank search *doesn't* reach into the most recent ":s" pattern.  I
> reach for this occasionally and get stung by it every time.
>
> However, if I do a
>
>   :%s//whatever
>
> it manages to find that pattern.  Likewise if I ":echo @/" it correctly
> returns my :s pattern that I want to use for my n/N command (or "/" with
> no regexp).
>
>
> It seems a little inconsistent to find the search pattern in the first
> case, but to not find the pattern in the second case.
>
>
> Is this a Vim bug (calling for n/N to use the search register), a  
> documentation bug (at a minimum requiring clarification), or just a "suck
> it up and deal with the oddity" situation?
>
> -tim

Here is a patch:
diff --git a/src/search.c b/src/search.c
--- a/src/search.c
+++ b/src/search.c
@@ -1161,9 +1161,13 @@
        {
            if (spats[RE_SEARCH].pat == NULL)       /* no previous pattern */
            {
-               EMSG(_(e_noprevre));
-               retval = 0;
-               goto end_do_search;
+               pat = last_search_pat();
+               if (pat == NULL)
+               {
+                   EMSG(_(e_noprevre));
+                   retval = 0;
+                   goto end_do_search;
+               }
            }
            /* make search_regcomp() use spats[RE_SEARCH].pat */
            searchstr = (char_u *)"";


regards,
Christian

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Two search patterns

Tim Chase
On 01/24/2011 11:01 AM, Christian Brabandt wrote:

> Here is a patch:
> diff --git a/src/search.c b/src/search.c
> --- a/src/search.c
> +++ b/src/search.c
> @@ -1161,9 +1161,13 @@
>          {
>              if (spats[RE_SEARCH].pat == NULL)       /* no previous pattern */
>              {
> -               EMSG(_(e_noprevre));
> -               retval = 0;
> -               goto end_do_search;
> +               pat = last_search_pat();
> +               if (pat == NULL)
> +               {
> +                   EMSG(_(e_noprevre));
> +                   retval = 0;
> +                   goto end_do_search;
> +               }
>              }
>              /* make search_regcomp() use spats[RE_SEARCH].pat */
>              searchstr = (char_u *)"";

Christian,

Thanks -- I figured it was a mere couple lines of code to fix,
though I guess the meta-question also pertained to "what's the
right behavior?"  My leaning is that the patch is for the better
and should go live (instead of documenting the weirdness).  I see
you CC'd vim-dev (where I don't subscribe, so am not CCing), so
I'd be interested in others' thoughts.

Thanks again!

-tim






--
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
Reply | Threaded
Open this post in threaded view
|

Re: Two search patterns

Christian Brabandt
Hi Tim!

On Di, 25 Jan 2011, Tim Chase wrote:

> Christian,
>
> Thanks -- I figured it was a mere couple lines of code to fix, though I
> guess the meta-question also pertained to "what's the right behavior?"  
> My leaning is that the patch is for the better and should go live
> (instead of documenting the weirdness).  I see you CC'd vim-dev (where I
> don't subscribe, so am not CCing), so I'd be interested in others'
> thoughts.
>
> Thanks again!

I'd like the behaviour. It makes sense and its only a small patch so it
should have no negative impact.

Let's see, what Bram thinks about it. He might as well just reject it or
possible put it into the very long todo list (and then it will hopefully
be integrated into the next major or minor release).

regards,
Christian

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Two search patterns

Antony Scriven-3
In reply to this post by Tim Chase
On 23 January 2011 21:17, Tim Chase wrote:

 > [...]
 >
 > Case #2:
 >
 > Start Vim (-u NONE) on a document
 > Perform a replacement (either a null-op, or just the
 > first-in-a-line with further matches later in the line,
 > or a replacement that allows for the pattern to be found
 > again)
 >
 >  :%s/pattern/&
 >
 > Hit "n" to find the next instance of the pattern or use
 > "/" with an empty pattern.  I get
 >
 >  E35: No previous regular expression
 >
 > [...]

It does indeed seem odd. Interestingly you can prevent
this from happening by starting vim with

   $ vim -u NONE --cmd "set vi='0"

--Antony

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Two search patterns

Bram Moolenaar
In reply to this post by Christian Brabandt

Christian Brabandt wrote:

> On So, 23 Jan 2011, Tim Chase wrote:
> > Per the help at
> >
> >   :help last-pattern
> >   """
> >     Note that two patterns are remembered: One for 'normal'
> >   search commands and one for the substitute command ":s".
> >   Each time an empty pattern is given, the previously used
> >   pattern is used.
> >   """
> >
> >
> > There seems to be a peculiarly unidirectional defaulting:
> >
> > Case #1:
> >
> > Start Vim (-u NONE) on a document
> > Search for a pattern with "/"
> >
> >   /pattern
> >
> > perform an empty substitute:
> >
> >   :%s//whatever/g
> >
> > A blank :s pattern reaches for the most recent "/" pattern. Nice, I use
> > this regularly.
> >
> >
> > Case #2:
> >
> > Start Vim (-u NONE) on a document
> > Perform a replacement (either a null-op, or just the first-in-a-line with
> > further matches later in the line, or a replacement that allows for the
> > pattern to be found again)
> >
> >   :%s/pattern/&
> >
> > Hit "n" to find the next instance of the pattern or use "/" with an empty
> > pattern.  I get
> >
> >   E35: No previous regular expression
> >
> > A blank search *doesn't* reach into the most recent ":s" pattern.  I
> > reach for this occasionally and get stung by it every time.
> >
> > However, if I do a
> >
> >   :%s//whatever
> >
> > it manages to find that pattern.  Likewise if I ":echo @/" it correctly
> > returns my :s pattern that I want to use for my n/N command (or "/" with
> > no regexp).
> >
> >
> > It seems a little inconsistent to find the search pattern in the first
> > case, but to not find the pattern in the second case.
> >
> >
> > Is this a Vim bug (calling for n/N to use the search register), a  
> > documentation bug (at a minimum requiring clarification), or just a "suck
> > it up and deal with the oddity" situation?
> >
> > -tim
>
> Here is a patch:
> diff --git a/src/search.c b/src/search.c
> --- a/src/search.c
> +++ b/src/search.c
> @@ -1161,9 +1161,13 @@
>         {
>             if (spats[RE_SEARCH].pat == NULL)       /* no previous pattern */
>             {
> -               EMSG(_(e_noprevre));
> -               retval = 0;
> -               goto end_do_search;
> +               pat = last_search_pat();
> +               if (pat == NULL)
> +               {
> +                   EMSG(_(e_noprevre));
> +                   retval = 0;
> +                   goto end_do_search;
> +               }
>             }
>             /* make search_regcomp() use spats[RE_SEARCH].pat */
>             searchstr = (char_u *)"";
>

Thanks for the patch.  Using another search pattern instead of failing
probably won't break anything.

--
hundred-and-one symptoms of being an internet addict:
149. You find your computer sexier than your girlfriend

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

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