How to grep #tag?

23 views
Skip to first unread message

Lifepillar

unread,
Oct 10, 2020, 5:09:42 PM10/10/20
to 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

unread,
Oct 11, 2020, 1:11:25 AM10/11/20
to 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

unread,
Oct 11, 2020, 6:26:00 AM10/11/20
to 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

unread,
Oct 11, 2020, 8:23:01 AM10/11/20
to 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

unread,
Oct 11, 2020, 8:28:18 AM10/11/20
to 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

unread,
Oct 11, 2020, 10:25:25 AM10/11/20
to 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

unread,
Oct 11, 2020, 11:19:34 AM10/11/20
to 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

Reply all
Reply to author
Forward
0 new messages