267

Until today, I wasn't aware of the ability to search for code with special symbols within our normal search. It's actually easy:

Instead of #include, search for code:#include.

(There appear to be some limitations on this, since HTML "tags" appear to be stripped.)

It's clear that nobody really knows about this. Of course, I already answered this question with this solution, but I also think it should be mentioned prominently in the search help, especially the "Advanced Search Tips".


The hascode: operator is also unlisted. It's not as helpful, but you can use it to avoid all the questions with no code, at least now that you know about it.

8
  • 29
    Yeah, this came up here also. This should be documented clearly, but from this comment: That being said, I don't feel it's 1.0 quality at this point, so haven't added it to documentation as I expect there are bugs I'll find with more testing locally... The situation perhaps changed since 2012.
    – Tunaki
    Commented Aug 7, 2016 at 21:40
  • 38
    Your question title is about special characters, your question body is about limiting searches to code. They are two separate things.
    – user743382
    Commented Aug 8, 2016 at 1:49
  • 5
    @hvd True, but I fixed it now. In reality, the operator does search code, but its significance is obviously the ability to search for symbols. While some languages (SQL or AppleScript come to mind) have "search friendly" syntax, many languages don't. (Regex is usually 90+% symbols.)
    – Laurel
    Commented Aug 8, 2016 at 1:56
  • 2
    @tunaki Unfortunately, I'll bet the interruptions for poor Nick haven't ceased since 2012.
    – Cody Gray Mod
    Commented Aug 8, 2016 at 12:20
  • stackoverflow.com/search?q=code%3A%24foo does not work as expected... Commented Aug 8, 2016 at 13:01
  • 6
    @MichaelDibbets With quotes around, it appears to work.
    – Tunaki
    Commented Aug 8, 2016 at 13:02
  • 14
    @Tunaki What if we want code with "$foo" in it, not $foo? This lack of polish clearly indicates why it should not be documented and used. Foo on this feature! Foo I say. Dollar sign. Commented Aug 8, 2016 at 19:11
  • @hvd: The two are not separate things, because code search is handled differently, and it;s this code search which handles non-alphanumeric characters.
    – MSalters
    Commented Aug 10, 2016 at 9:41

2 Answers 2

16

Why aren't we told? Perhaps because this is nowhere near being sufficient.

Plenty of people don't wrap their code in code blocks or backquotes, so this will return fewer results than exist. I personally find it ridiculous that a site devoted to programming doesn't have a search that can, you know, actually find literal code. I would like very much one day to search for [qt] $$files but the built-in search can't do it, symbolhound finds one result out of many, and all other search engines are no better. Let me make it absolutely clear that this is not a synthetic test case, it is a valid, reasonable search for a built-in function of a tool, a function that is impossible to find otherwise (files is not exactly an uncommon word), and there is zero support for executing this search as of now. This sucks.

3
  • 1
    "... and all other search engines are no better". And you expect Stack Overflow to do better than "all other search engines"? When it's not a search engine provider? Commented Apr 10, 2017 at 14:48
  • 1
    Uhh, I'm commenting on SO's built-in search functionality. It being there makes your "it's not a search engine provider" point moot. Presumably the search is there to be useful to the software developer community. I claim that a search that doesn't actually search for things a developer might search for is kinda useless. Commented Apr 10, 2017 at 15:36
  • 1
    @KubaOber I realize that plenty of people don't format their code right (and this limits the effectiveness of the feature), but that's why I edit posts and approve good edits. (Your other option is to use SE Data Explorer.)
    – Laurel
    Commented Apr 10, 2017 at 17:15
6

Some of these features were hinted at in 2012 on Meta.SE:

Quoted phrases are exact matches except for case-sensitivity, for example, you can search for code or symbols.

and here in 2014:

The built-in Elastic Search will allow you to search for operators using their literal symbols, such as * and & instead of using "asterisk" and "ampersand", respectively:

References

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .