166

I'm trying to use GitHub's code search to search for some lines of code containing characters like =, +, etc.

I understand from https://help.github.com/articles/searching-code/#considerations-for-code-search that these characters are wildcards and have to be escaped, otherwise they will be ignored.

Here's an example: "if \(done is True\)", and another: "if (done is True)".

Both return the same "your query contains a character that is ignored" warning on the right side of the search bar. My escaping doesn't seem to be doing anything and the results I get are with the characters ignored.

Piper
  • 1,266
  • 3
  • 15
  • 26
Eli Rose
  • 6,788
  • 8
  • 35
  • 55

4 Answers4

180

I dont think you can escape characters for github

From searching code doc:

You can't use the following wildcard characters as part of your search query: . , : ; / \ ` ' " = * ! ? # $ & + ^ | ~ < > ( ) { } [ ]. The search will simply ignore these symbols.

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • 23
    Right, I saw that. Is there no way to escape these characters? Meaning that if I want to search my repo for `x += 2` there's no way to do that? – Eli Rose Dec 12 '15 at 20:15
  • 1
    nope, I think github has configured elasticsearch in that way only to reduce the complexity – ChintanShah25 Dec 12 '15 at 20:49
  • 26
    What about `@`? – User Nov 22 '16 at 00:57
  • 65
    ran into this; *useless* code search! I need to find all instances of `:ok` and `:locked` in a Ruby on Rails codebase. Github's crappy search doesn't allow this. –  Apr 20 '18 at 15:31
  • 3
    @User: According to [this issue](https://github.com/isaacs/github/issues/402#issuecomment-397061234), `@` has been mentioned as not on the list, but no response as of yet. – Quantum Mechanic Sep 06 '18 at 14:11
  • 1
    Now that Microsoft has acquired github, perhaps they can be persuaded to fix this... – NetMage Aug 13 '19 at 21:05
  • They just don't want people to crash their servers with regex vulnerabilities huh .... – matanster Aug 22 '19 at 05:57
  • @matanster maybe, but allowing ? or ! in a non-regex search should not be a problem, and it's exactly what I'm trying to do :( – David Aldridge Sep 07 '19 at 09:55
  • Ugh... Need to search for method uses of `update()` in an organization with over 200 repositories. Can't even narrow my search to select repositories. As you can imagine, `update` is pretty commonly used. Lucene based search engines have no trouble here as long as things are escaped property, so no idea why it's not supported. Might write my own thing using their Search API that does some post processing. – Christopher Schneider Oct 25 '19 at 16:10
  • 1
    what a pity, searching any `JVM` flag that has a defined pattern as `-XX:+...` will not work :| – Eugene Dec 18 '19 at 04:22
  • 1
    GitHub search is not useful. I use google search instead. Search query - `site:github.com "$ref"` will search for `$ref` in entire github (not just code). But I can live with this without pragmatic help from GitHub. – Arun Avanathan Jul 19 '20 at 22:56
  • 1
    https://grep.app – 6infinity8 Oct 29 '22 at 15:35
17

Update: Use github.dev/...., this is a official solution from github team. Just replace .com with .dev in the Url or pressing the dot ( . ) key in any repo to open in github.dev. It even works with private repo.

GitHub natively doesn't support advanced code search like using special characters or excluding or including specific folders, We can use GitHub1s for searching a code in a repo.

GitHub1s will open a repo in VSCode online (so no cloning process is required) and we can use the advanced code search provided by VSCode to search.

To open a repo in GitHub1s just replace github.com/.... with github1s.com/... to view the repo in VSCode online (In other words, add 1s before .com).

Important note: Code search in Github1s only works on public repo

enter image description here

enter image description here

Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
  • This however only searches in the opened files. So while it works, there is a big limitation. – maulik13 Jun 09 '21 at 12:15
  • 1
    @maulik13 No it will work with all files (including not opened), they have added this feature in 0.05 and above. You can even see in my screenshot, it shows result from non opened files. More info: https://github.com/conwnet/github1s/pull/199 and https://github.com/conwnet/github1s/issues/13 – Vijay Nirmal Jun 09 '21 at 14:43
  • Strange, when I tried yesterday it only search in opened files. – maulik13 Jun 10 '21 at 10:22
  • 2
    @maulik13 Is it a public repo? this only works on public repo – Vijay Nirmal Jun 11 '21 at 06:12
  • That makes sense, my repo is private. Thanks for clarifying that. – maulik13 Jun 11 '21 at 12:06
  • Can you provide a link for the claim that this is an official solution from the github team? – Mark Rucker Aug 18 '21 at 17:25
  • 1
    @MarkRucker This is from VSCode team in collab with GitHub Team. https://www.youtube.com/watch?v=c3hHhRME_XI https://code.visualstudio.com/docs/remote/codespaces#_browserbased-editor https://docs.github.com/en/codespaces/developing-in-codespaces/web-based-editor#how-to-use-the-web-based-editor – Vijay Nirmal Aug 20 '21 at 05:43
  • @VijayNirmal That's super cool! Thanks! I didn't know about this. – Mark Rucker Aug 20 '21 at 13:04
  • Wow, so instead of just supporting _some_ kind of character escaping, their solution is to open the whole shebang in an embedded VSCode which just has a normal working search? That's ridiculous. – Nyerguds Mar 23 '23 at 13:16
9

You will need to do this locally. Here are some options, from https://stackoverflow.com/a/38288679/362202:

These strategies would not work for github-wide searches, which is a shame.

Ran Ever-Hadani
  • 312
  • 2
  • 11
  • 33
    What if you have 1000+ repositories to search through? – hfm Nov 21 '19 at 16:26
  • 5
    What if I wanted to share the results with someone else....as one expects from a site like github that is supposedly for collaboration? – Christian Jun 29 '20 at 23:19
6

there is one more (some might say shameful) semi-solution/workaround: Using google...

In the search field enter site:repo-url in addition to your keywords.

I was searching for action$ in the redux-logic repo and was at least able to filter out all the other unwanted action (without the dollar-sign) results which were showing up in the github search. In Google i typed (pay attention to the quotes):

site:https://github.com/jeffbski/redux-logic "action$"

Unfortunately though, google won't list all results. That's why i wrote semi at the beginning. So you can try your luck quickly with google, if you don't find what you were looking for, fallback to checking out the git repo + search with offline-tools like vscode, intellij or cmd-line-tools as suggested by @Ran Ever-Hadani

I hope github will soon come up with a brand-new search feature, which its users can love instead of hate.

farukg
  • 523
  • 4
  • 12