Thursday, January 1, 2015

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

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)

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

Post a Comment