Skip to main content

Ack - Better Than Grep

· 2 min read
Max Kaido
Architect

Ack is a code-focused search tool designed as an alternative to grep. It's optimized for developers and automatically searches through source code while ignoring version control directories and binary files.

Key Features

  • Automatically ignores VCS directories (.git, .svn)
  • Searches only programmer-relevant files by default
  • Colorized output
  • Smart case-sensitivity (case-insensitive by default, sensitive if pattern contains uppercase)

Common Usage

ack pattern           # Search for pattern
ack -i pattern # Case-insensitive search
ack -w pattern # Match whole words only

File Type Filtering

ack --ts pattern      # Search only TypeScript files
ack --js pattern # Search only JavaScript files
ack --python pattern # Search only Python files
ack --list-types # Show all supported file types

Context Control

ack pattern -C 2      # Show 2 lines before and after match
ack pattern -B 2 # Show 2 lines before match
ack pattern -A 2 # Show 2 lines after match

Advanced Features

ack -l pattern        # Only list filenames containing matches
ack -L pattern # List files that don't match
ack -c pattern # Count matches in each file
ack -f # List all files that would be searched

File Type Management

ack --type-set=jsx=.jsx            # Define new file type
ack --type-add=js=.jsx # Add extension to existing type
ack --ignore-dir=coverage # Ignore directory

Pro Tips

  1. Custom Types: Create .ackrc for project-specific types:

    --type-set=view=.html.erb,.erb
    --type-set=spec=_spec.rb
  2. Combining with Other Tools:

    ack -l pattern | xargs sed -i 's/old/new/g'  # Find and replace
    ack -f --ts | xargs wc -l # Count lines in TS files
  3. Output Formatting:

    ack --heading pattern    # Group matches by file
    ack --group pattern # Group matches with context

Common Patterns

Finding Function Definitions

ack '^function\s+\w+' --ts     # Find TypeScript functions
ack '^class\s+\w+' --python # Find Python classes

Searching Multiple Patterns

ack 'pattern1|pattern2'        # Match either pattern
ack -e pattern1 -e pattern2 # Match both patterns

Complex Searches

ack 'class.*extends'           # Find class inheritance
ack 'import.*from' # Find module imports
ack 'TODO|FIXME|XXX' # Find code comments

Configuration

Global .ackrc

# ~/.ackrc
--smart-case
--sort-files
--color
--follow
--ignore-dir=node_modules
--ignore-dir=dist
--ignore-dir=coverage

Project-Specific .ackrc

# ./.ackrc
--ignore-dir=vendor
--type-set=view=.html.erb,.haml
--type-set=css=.scss,.sass,.less

Integration with Editors

Vim Integration

" Search and open results in quickfix
:set grepprg=ack\ -k
:grep pattern

VS Code Integration

Install "Ack in Selection" extension for:

  • Search in selected text
  • Search in specific folders
  • Custom ack configurations