bat チートシート
bat チートシート
catcatの最新の代替ツールで、シンタックスハイライト、Git統合、自動ページング機能を備えています。
インストール
| プラットフォーム | コマンド |
|---|---|
| Ubuntu/Debian | sudo apt install bat (binary may be named batcat) |
| Arch Linux | sudo pacman -S bat |
| Fedora/RHEL | sudo dnf install bat |
| macOS (Homebrew) | brew install bat |
| macOS (MacPorts) | sudo port install bat |
| Windows (Scoop) | scoop install bat |
| Windows (Chocolatey) | choco install bat |
| Windows (Winget) | winget install sharkdp.bat |
| Rust/Cargo | cargo install --locked bat |
| Docker | docker pull danlynn/bat |
注意: Debian/Ubuntu環境では、必要に応じてエイリアスを作成してください:alias bat='batcat'``alias cat='bat'
基本コマンド
| コマンド | 説明 |
|---|---|
bat file.txt | 構文ハイライトと行番号を使用してファイルを表示 |
bat file1.txt file2.py | ヘッダー付きで複数のファイルを表示 |
bat -n file.txt | 行番号を明示的に表示(デフォルトで有効) |
bat -p file.txt | Plain mode (no decorations, like cat) |
bat -pp file.txt | エクストラプレーンモード(完全にプレーンな出力) |
bat -A file.txt | すべての非印刷可能な文字(タブ、スペース、改行)を表示 |
bat -r 10:20 file.txt | 特定の行範囲を表示(10〜20行目) |
bat -r :50 file.txt | 最初の50行を表示 |
bat -r 100: file.txt | 100行目から最後まで表示 |
| `echo “text” \ | bat` |
| パイプされた入力を構文ハイライト付きで表示 |
| `bat --list-languages` | サポートされているプログラミング言語をすべて表示 |
| `bat --list-themes` | 利用可能なカラーテーマをすべて表示 |
| `bat -l python file.txt` | 特定の言語/構文のハイライト |
| `bat --plain file.txt` | 構文の強調表示を無効にする |
| `bat --paging=never file.txt` | 自動ページング無効化 |batの基本的な使用方法
## 高度な使用法
| コマンド | 説明 |
|---------|-------------|
| `bat --theme="Dracula" file.py` | 特定のカラーテーマを使用する |
| `bat --diff file.txt` | Git の変更を構文ハイライト付きで表示 |
| `bat --no-git file.txt` | Gitの統合マーカーを無効にする |
| `bat --tabs=4 file.py` | カスタムタブ幅を設定 |
| `bat --wrap=never file.txt` | 行の折り返しを無効にする |
| `bat --terminal-width=80 file.txt` | 特定の端末幅を強制 |
| `bat --style=numbers,changes file.txt` | 行番号と Git の変更のみを表示 |
| `bat --style=header,grid file.txt` | ヘッダーとグリッドの区切りのみを表示 |
| `bat --style=plain file.txt` | すべての装飾を削除 |
| `bat --file-name="Custom" file.txt` | ヘッダーにカスタム名を表示 |
| `bat --decorations=never file.txt` | すべての装飾を無効にする(適切な場合でも) |
| `bat --color=always file.txt` | カラー出力を強制する(パイプ時でも) |
| `bat --paging=always file.txt` | 小さなファイルでもページャーを強制的に表示する |
| `bat --pager="less -RF" file.txt` | カスタムページャーをオプション付きで使用する |
| `bat --map-syntax='*.conf:INI' file.conf` | 特定の構文にマップファイルの拡張子 |
| `bat --generate-config-file` | デフォルト設定ファイルを生成する |
| `bat cache --build` | 構文/テーマキャッシュを再構築 |
| `bat cache --clear` | 構文/テーマキャッシュをクリア |高度な機能と使用テクニック
## 設定
### 設定ファイルの場所
| プラットフォーム | パス |
|----------|------|
| Linux/macOS | `~/.config/bat/config` |
| Windows | `%APPDATA%\bat\config` |設定ファイルが配置される標準的なディレクトリ
### サンプル設定ファイル
`~/.config/bat/config`個人の設定に合わせて作成:
```bash
# Set default theme
--theme="Dracula"
# Always show line numbers and changes
--style="numbers,changes,header,grid"
# Use 4-space tabs
--tabs=4
# Automatic paging
--paging=auto
# Show non-printable characters
--show-all
# Enable italic text (if terminal supports)
--italic-text=always
# Custom syntax mappings
--map-syntax="*.conf:INI"
--map-syntax=".ignore:Git Ignore"
--map-syntax="*.jenkinsfile:Groovy"
--map-syntax="*.log:Log"
--map-syntax=".env:Bash"
```設定ファイルの例
### 環境変数
```bash
# Set default theme
export BAT_THEME="Dracula"
# Set default style components
export BAT_STYLE="numbers,changes,header"
# Set default pager
export BAT_PAGER="less -RF"
# Disable paging entirely
export BAT_PAGER=""
# Custom config directory
export BAT_CONFIG_PATH="/custom/path/to/config"
```batの動作に影響を与える環境変数の一覧
### カスタムテーマ
```bash
# Create themes directory
mkdir -p "$(bat --config-dir)/themes"
# Download theme file (.tmTheme format)
cd "$(bat --config-dir)/themes"
wget https://example.com/theme.tmTheme
# Rebuild cache to use new theme
bat cache --build
```テーマのカスタマイズ方法
### スタイルコンポーネント
利用可能なスタイルコンポーネント(カンマ区切り):
| コンポーネント | 説明 |
|-----------|-------------|
| `full` | すべてのコンポーネント(デフォルト) |
| `auto` | 出力に基づく自動 |
| `plain` | 装飾なし |
| `numbers` | 行番号 |
| `changes` | Gitの修正マーカー |
| `header` | ファイル名ヘッダー |
| `grid` | グリッドセパレータ |
| `rule` | 水平線 |
| `snip` | 行範囲のスニップマーカー |スタイルコンポーネントの詳細リスト
## 一般的なユースケース
### ユースケース:Gitインテグレーションによるコードレビュー
```bash
# View file with Git changes highlighted
bat --diff src/main.py
# Compare with previous commit
git show HEAD~1:config.yaml | bat -l yaml --file-name="Previous Version"
# Review multiple changed files
git diff --name-only | xargs bat --diff
```Gitと連携したコード確認の方法
### ユースケース:ログファイル分析
```bash
# View application logs with syntax highlighting
bat --language=log /var/log/application.log
# View specific error section
bat -r 1000:2000 /var/log/syslog | grep ERROR
# Monitor live logs (disable paging)
tail -f /var/log/app.log | bat --paging=never -l log
# Search logs with context
bat /var/log/nginx/error.log | grep -C 5 "ERROR"
```ログファイルの効率的な閲覧と分析
### ユースケース:設定ファイル管理
```bash
# Review system configurations
bat /etc/nginx/nginx.conf /etc/ssh/sshd_config
# Compare configurations side by side
diff <(bat -p config.prod.yml) <(bat -p config.dev.yml)
# Check for hidden characters in config
bat -A .env
# View Kubernetes resources
kubectl get deployment myapp -o yaml | bat -l yaml
```設定ファイルの表示と管理
### ユースケース:Dockerとコンテナワークフロー
```bash
# View Dockerfile with syntax highlighting
bat Dockerfile
# Inspect docker-compose configurations
bat docker-compose.yml docker-compose.override.yml
# View container logs with syntax
docker logs container_name 2>&1 | bat --paging=never -l log
# Display formatted JSON from API
curl -s https://api.github.com/repos/sharkdp/bat | bat -l json
```コンテナ関連のファイル表示
### ユースケース:スクリプト開発とデバッグ
```bash
# View shell script with line numbers
bat -n deploy.sh
# Check specific function (lines 45-78)
bat -r 45:78 automation.sh
# Detect problematic characters
bat -A problematic-script.sh
# View multiple related scripts
bat scripts/*.sh
```スクリプト開発時のbatの活用
## ベストプラクティス
- **スクリプティングには`-p`を使用**: パイピングやスクリプティング時は`bat -p`または`bat --plain`処理を妨げる可能性のある装飾を避けるため`~/.config/bat/config`**デフォルトを設定**: 繰り返しのフラグを避けるため、好みのテーマとスタイルをセットアップ`alias cat='bat --paging=never --style=plain'`シームレスな置き換えのため、シェル設定に追加`--diff`構文ハイライト付きでインラインに変更を確認する際に使用`--map-syntax`プロジェクト固有のファイルタイプ用に設定ファイルで`.jenkinsfile``.conf``--paging=never`対話型ページャーの問題を防ぐため、他のコマンドに出力をパイプする際は常に使用`-l`拡張子のないファイルの適切な構文ハイライトのために、言語を明示的に指定`bat cache --build`カスタムテーマや構文を追加した後に実行し、利用可能にする
## トラブルシューティング
| 問題 | ソリューション |
|-------|----------|
| Binary named `batcat` instead of `bat` | Create alias: `alias bat='batcat'` or symlink: `ln -s /usr/bin/batcat ~/.local/bin/bat` |
| No syntax highlighting | Check language support with `bat --list-languages` and use `-l` to force language |
| Pager interferes with pipes | Use `bat --paging=never` or set `export BAT_PAGER=""` |
| Theme not displaying correctly | Verify terminal supports 256 colors; try different theme with `bat --theme="GitHub"` |
| Custom theme not appearing | Ensure theme file is in `$(bat --config-dir)/themes/` and run `bat cache --build` |
| Line numbers misaligned | Check tab width setting; adjust with `--tabs=4` or in config file |
| Git integration not working | Ensure file is in Git repository; disable with `--no-git` if not needed |
| Config file changes ignored | Verify file location with `bat --config-file`; check for syntax errors in config |
| Performance issues with large files | Use `--paging=never` and pipe to `less`, or use line ranges with `-r` |
| Unicode characters display incorrectly | Ensure terminal uses UTF-8 encoding; check `locale` settings |
## クイックリファレンス: 一般的なフラグ
| 短い | 長い | 目的 |
|-------|------|---------|
| `-p` | `--plain` | プレーンな出力モード |
| `-n` | `--number` | 行番号を表示 |
| `-A` | `--show-all` | 非印刷文字を表示 |
| `-l` | `--language` | 構文言語を設定 |
| `-r` | `--line-range` | 表示ライン範囲 |
| `-H` | `--highlight-line` | 特定の行を強調表示 |
| `-d` | `--diff` | Gitの差分を表示 |
| | `--theme` | カラーテーマを設定 |
| | `--style` | スタイルコンポーネントを設定 |
| | `--paging` | ページャーの動作を制御 |
| | `--tabs` | タブ幅を設定 || | `--wrap`| 行の折り返しを制御 |
Note: Lines 6 and 7 and line 11 were empty in the original text, so I've left them blank in the translation as well.