Skip to content

feat: add --watch flag for live file reload#3645

Open
konaito wants to merge 4 commits intosharkdp:masterfrom
konaito:feature/watch-mode
Open

feat: add --watch flag for live file reload#3645
konaito wants to merge 4 commits intosharkdp:masterfrom
konaito:feature/watch-mode

Conversation

@konaito
Copy link
Copy Markdown

@konaito konaito commented Mar 20, 2026

Summary

  • Add --watch / -w option that monitors files for changes using the notify crate
  • When a watched file is modified externally (e.g. in VS Code, nano, vim), bat automatically clears the screen and re-displays the updated content with full syntax highlighting
  • Watches parent directories to correctly handle editors that perform atomic saves (write to temp + rename)
  • Includes 100ms debouncing to coalesce rapid filesystem events
  • Paging is automatically disabled in watch mode; press Ctrl+C to stop

Usage

bat --watch src/main.rs       # Watch a single file
bat -w config.yaml            # Short form
bat --watch file1.rs file2.rs # Watch multiple files

Motivation

This addresses a common workflow where developers view a file with bat in one terminal while editing it in another (VS Code, nano, vim, etc). Currently, users must manually re-run bat to see changes. With --watch, the display stays up-to-date automatically — similar to tail -f but for the entire file with syntax highlighting.

Closes #852

Test plan

  • cargo build succeeds
  • cargo test --lib — all 116 tests pass
  • bat --watch file.json displays file, updates on external edit, stops on Ctrl+C
  • bat --watch without file argument shows helpful error
  • bat --help shows --watch flag with description
  • Test with editors using atomic saves (VS Code, vim)
  • Test with multiple files

🤖 Generated with Claude Code

konaito and others added 4 commits March 20, 2026 22:51
Add a `--watch` / `-w` option that monitors files for changes and
automatically clears the screen and re-displays them with syntax
highlighting when modified externally (e.g. in VS Code, nano, vim).

This uses the `notify` crate to watch the parent directory of each file,
which correctly handles editors that perform atomic saves (write to temp
file + rename). Includes 100ms debouncing to coalesce rapid events.

Paging is automatically disabled in watch mode. Press Ctrl+C to stop.

Closes sharkdp#852

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Replace redundant closures with Input::ordinary_file
- Use contains() instead of iter().any() for path matching
- Update short-help.txt and long-help.txt with --watch flag

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant