Cgrep

Cgrep: a context-aware grep for source codes

Download as .zip Download as .tar.gz View on GitHub

CGrep: Context-aware grep for source codes

Hackage Join the chat at https://gitter.im/awgn/cgrep

Version 9.2.1 - A powerful, context-aware search tool designed specifically for source code.

CGrep extends the capabilities of traditional grep by understanding the structure and semantics of source code across multiple programming languages. It allows developers to search within specific contexts like code, comments, or string literals, and provides advanced pattern matching with semantic awareness.


🚀 What’s New in Version 9.2


✨ Features


📦 Installation

From Hackage

cabal update
cabal install cgrep

From Source

git clone https://github.com/awgn/cgrep.git
cd cgrep
cabal install
# or using stack:
# stack build && stack install

💡 Quick Examples

Context-Aware Searching

Search for a pattern in source code only (excluding comments and strings):

cgrep -c "malloc" *.c

Search only in comments:

cgrep -m "TODO" -r src/

Search using semantic wildcards to find variable assignments with numeric literals:

cgrep -S "_ = NUM" *.c

Test Filtering (New!)

Search in production code only, excluding all test files:

cgrep --no-tests "function" -r src/

Search only in test files:

cgrep --tests-only "mock" -r tests/

File Type Filters

Search recursively in C++ files:

cgrep --type=Cpp -r "vector" src/

🧪 Test Framework Support

Version 9 introduces intelligent test code filtering across 28+ programming languages. When using the -T flag, cgrep can automatically detect and filter test code based on language-specific conventions and testing frameworks.

Language Testing Frameworks Detected Detection Patterns
Rust Built-in, cargo test #[test], #[rstest], #[test_case] modules
Go Built-in testing func Test*, func Benchmark*, func Example*, func Fuzz*
Java / Kotlin JUnit, TestNG @Test, @ParameterizedTest, @BeforeEach, @AfterEach annotations
C / C++ Google Test, Catch2 TEST(), TEST_F(), TEST_CASE(), SECTION()
Python pytest, unittest test_* functions, Test* classes, @pytest
JS / TS Mocha, Jasmine, Jest describe(), it(), test()
Haskell HSpec, Tasty, QuickCheck describe, it, prop_* functions

(And many more, including C#, F#, Dart, Elixir, Ruby, PHP, Swift, R, Julia, OCaml…)


🌐 Supported Languages

CGrep supports a wide range of programming languages and file formats:

Programming Languages: C, C++, C#, Java, Kotlin, Scala, JavaScript, TypeScript, CoffeeScript, Python, Ruby, Perl, PHP, Go, Rust, Haskell, OCaml, F#, Erlang, Elixir, Clojure, Lisp, Scheme, Lua, R, Julia, Dart, Nim, Zig, D, Swift, Objective-C, Chapel, Awk, Shell scripts (Bash, Fish)

Markup & Config: HTML, XML, LaTeX, Markdown, YAML, JSON, TOML, INI, Dhall, CMake, Makefile, Cabal


⚙️ Configuration

CGrep can be configured using a configuration file located at ~/.cgreprc (or $XDG_CONFIG_HOME/cgrep/cgreprc).

Example configuration:

colors: true
file-types:
  - +Cpp
  - +Haskell
  - -Test
jobs: 8