Skip to content

chmouel/csd

Repository files navigation

csd - Code Search & Diff

csd is a fast, safe, interactive regex search-and-replace CLI tool for codebases. It has zero external dependencies beyond Go's standard library.

Installation

go install github.com/chmouel/csd@latest

Or build from source:

make build

Usage

csd [OPTIONS] [FILE_PATTERN] SEARCH_PATTERN REPLACEMENT

With three arguments, the first is a regular expression matched against file paths (FILE_PATTERN). With two arguments, all files in the directory tree are searched.

When standard input is piped with two arguments, csd processes the file list provided via stdin (one file path per line).

Examples

# Replace "foo" with "bar" in all files
csd 'foo' 'bar'

# Replace only in .txt files
csd '\.txt$' 'foo' 'bar'

# Replace in a piped list of files
find . -name '*.go' | csd 'old_func' 'new_func'

# Preview changes without modifying files
csd --dry-run 'foo' 'bar'

# Interactively prompt for each change with a colored diff
csd -i 'foo' 'bar'

# Use capture groups ($1 or \1 syntax)
csd '(\w+) (\w+)' '$2 $1'
csd '(\w+) (\w+)' '\2 \1'

Options

Flag Short Description
--interactive -i Prompt for confirmation before applying each change
--quiet -q Suppress normal output messages
--no-ignore -I Do not respect .gitignore or .ignore files
--include-git-dir Include files inside .git directory
--dry-run Show modified files without writing changes
--version Display version information

Shell completion

Generate and source a completion script for the current shell:

# Bash
source <(csd --completion bash)

# Zsh
source <(csd --completion zsh)

Use -c as a short alias for --completion. Add the applicable command to your shell startup file to enable completion in new sessions.

Features

  • Atomic Writes: Preserves original permissions and guarantees safe file modifications via temporary file swap.
  • Ignore Aware: Respects .gitignore and .ignore files by default.
  • Binary File Detection: Automatically skips binary files containing NUL bytes.
  • Zero Dependencies: Built purely with Go standard library (regexp, path/filepath, os, etc.).

Development

make check   # gofmt, golangci-lint, and go test
make build   # build bin/csd

golangci-lint (see .golangci.yml) is required for make check/make lint; install it from https://golangci-lint.run/welcome/install/. pre-commit install wires the same checks into your local git hooks.

License

Apache-2.0

About

Code Search & Replace - Fast, safe, interactive search-and-replace across your codebase

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages