Thursday, January 1, 2015

Re: In Vim, / (search in normal mode) and search function return different results

On Thursday, 1 January 2015 13:02:45 UTC, Christian Brabandt wrote:
> Hi Jeff!
>
> On Do, 01 Jan 2015, Jeff Baxter wrote:
>
> > Just asked this question on stackoverflow but the comment shows it seems to be a version-specific problem.
> >
> > I have a simple text file:
> >
> > blah.
> > [{}]
> > [{foo}]
> >
> > My cursor is at the beginning of the file. I want to search for (possibly empty) strings embraced with [{}].
> >
> > So the regex pattern is "\[{.*}\]",right?
> >
> > When I use the normal search
> >
> > /\[{.*}\]
> >
> > The cursor is relocated at the second line, good!
> >
> > But if I use the search function
> >
> > :echo search("\[{.*}\]")
> >
> > The cursor goes to the final . of the first line and the result of echo is 1, meaning the first match is in line 1.
> >
> > Can you reproduce this? And if yes, why and how can I get the same result as the normal search using a function?
>
> You are using double quotes in the first search argument. The backslash
> in the double quote has a special meaning and basically just means to
> use the character after it. In your case the search function sees at it
> first argument the pattern [{.*}] which happens to match in the last
> column of line 1.
>
> The solution is, to either use single quotes or escape the character
> twice, if you use double quote.
>
> This is explained at the help at :h expr-quote
>
> Best,
> Christian
> --
> Alles Unbekannte gilt für groß.
> -- Publius Cornelius Tacitus (55-120)

Hi Christian,

Thanks for your answer - it works and makes good sense now. I've tested the single quote on egrep and it works the same way.

Jeff

P.S. as an off-topic subject, I still feel like there are some inconsistencies in the shell in this matter, which potentially will confuse me some time.

An example that in shell the quotes work differently:
$ echo "\n"
\n
$ echo '\n'
\n
$ echo -e "\n"


$ echo -e '\n'


I understand in shell single quotes also means literal strings, but the above example seems to contradict this fact.

A possible explanation is, the option -e forces the expansion in both cases. Anyway this does not bother me for now...

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