This is somewhat similar to the question in https://unix.stackexchange.com/questions/33131/how-to-grep-on-source-code-without-catching-comments - however, what I actually want, is this: provided a C source file like this, myfile.c:

 * foo has changed [...] and is now a 2-parameters function
// foo(24)
int myvar = foo(42, 28);

... and grepping/searching for some string (e.g. "foo") in it - the results should contain both:

  • the search term colored in one color - and
  • the rest of the line colored either with the default terminal text color, or colored differently if it is a comment

So for a pseudocode like:

$ commentgrep 'foo' --match-color="red" --c-comment-color="green" myfile.c

... I would like results like these:


This probably implies that such a tool, like this imagined "commentgrep", should do one pass to look for matches in the file, and if found, do another pass to find and color the comments ...

(and of course, it would be great, if you could also switch language comment styles, so you might have --python-comment-color as well)

Is there such a command line tool available?

1 Answer 1


Well, not exactly the output I imagined, but still looks great - https://lib.rs/crates/hgrep (and can be installed under MSYS2/MINGW64 on Windows using cargo install hgrep):

hgrep output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.