How to grep #tag?

瀏覽次數:23 次
跳到第一則未讀訊息

Lifepillar

未讀,
2020年10月10日 下午5:09:422020/10/10
收件者:vim...@googlegroups.com
How can I grep text with a special symbol, such as #?

I would like to search a bunch of files containing various #tags, but if
I try from the command line:

:grep #sometag

that does not work. Escaping the hash causes grep to exit with an error:

:grep \#sometag

Any idea?

Thanks,
Life.

Shlomi Fish

未讀,
2020年10月11日 凌晨1:11:252020/10/11
收件者:Lifepillar、vim...@googlegroups.com
Hi, Life!
Either:

:grep \\#p

Or:

:grep '\#p'

Seem to work here in gvim (Linux; shell is either bash or zsh).

> Any idea?
>
> Thanks,
> Life.
>



--

Shlomi Fish https://www.shlomifish.org/
The Case for File Swapping - https://shlom.in/file-swap

SGlau: shut up, Mr. Frog! I’m here on a mission from the Grammar God. Herr
Norris and I are loyal servants of Grammar Nazism, which aims to unite Grammar
Europe under the reign of the Third Grammar Reich. Hail Grammar!
https://is.gd/htmOCv

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Lifepillar

未讀,
2020年10月11日 清晨6:26:002020/10/11
收件者:vim...@googlegroups.com
On 2020-10-11, Shlomi Fish <shl...@shlomifish.org> wrote:
> On Sat, 10 Oct 2020 21:09:26 -0000 (UTC)
> Lifepillar <lifep...@lifepillar.me> wrote:
>
>> How can I grep text with a special symbol, such as #?
>
> Either:
>
> :grep \\#p
>
> Or:
>
> :grep '\#p'
>
> Seem to work here in gvim (Linux; shell is either bash or zsh).

That does it! Double-escaping is always tricking me.

Thanks,
Life.


Bram Moolenaar

未讀,
2020年10月11日 上午8:23:012020/10/11
收件者:vim...@googlegroups.com、Lifepillar
You can use any special character to surround the pattern, it's a lot
easier than escaping. I often use ":grep /pattern/ *", unless the
pattern contains a slash.

--
I'm trying to be an optimist, but I don't think it'll work.

/// Bram Moolenaar -- Br...@Moolenaar.net -- 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 ///

@lbutlr

未讀,
2020年10月11日 上午8:28:182020/10/11
收件者:vim...@googlegroups.com
On 11 Oct 2020, at 06:22, Bram Moolenaar <Br...@moolenaar.net> wrote:
> You can use any special character to surround the pattern, it's a lot
> easier than escaping. I often use ":grep /pattern/ *", unless the
> pattern contains a slash.

As soon as I learned you could use any delimiter you wanted I stopped using / and switched to | as I never have to escape pipes.

> --
> I'm trying to be an optimist, but I don't think it'll work.

--
No sense being pessimistic. It wouldn't work anyway.

Lifepillar

未讀,
2020年10月11日 上午10:25:252020/10/11
收件者:vim...@googlegroups.com
On 2020-10-11, @lbutlr <kre...@kreme.com> wrote:
> On 11 Oct 2020, at 06:22, Bram Moolenaar <Br...@moolenaar.net> wrote:
>> You can use any special character to surround the pattern, it's a lot
>> easier than escaping. I often use ":grep /pattern/ *", unless the
>> pattern contains a slash.

> As soon as I learned you could use any delimiter you wanted I stopped
> using / and switched to | as I never have to escape pipes.

You still need to escape # inside the pattern, don't you? As in:

:grep /\#foo/ *

At least, when 'grepprg' is grep (which is the default?). If it's set to
internal, then I can actually search for /#foo/.

Btw, as I was experimenting with grep commands, I have also realised
that a command with -complete=file expands #, but a command without it
does not. So, this is what I've come up with:

command! -nargs=+ Grep call s:grep(<q-args>)

fun s:grep(args)
execute 'silent grep!' shellescape(fnameescape(a:args)) '**/*'
endf

Then, s:grep() will receive '#foo' when I type :Grep #foo. Inside the
function, fnameescape() will escape # again for :grep!, and
shellescape() will escape the input for the shell. I have yet to test
this thoroughly, maybe there are better ways.

The drawback is that the custom command does not expand any path.

Life.

>> --
>> I'm trying to be an optimist, but I don't think it'll work.
>
> --
> No sense being pessimistic. It wouldn't work anyway.
>
> --

--
It's worse than that. It makes things fall apart.

aro...@vex.net

未讀,
2020年10月11日 上午11:19:342020/10/11
收件者:vim...@googlegroups.com

If we're talking about shell grep, it's wise always to quote the search
string, (which kind of quote is preferable depends on which OS/shell),
whether it has special characters or not. If it's redundant, no harm done.
If it isn't, you don't have to guess how the shell molested your string

回覆所有人
回覆作者
轉寄
0 則新訊息