Bat Cat技能使用说明
2026-03-29
新闻来源:网淘吧
围观:9
电脑广告
手机广告
bat - 更好的cat
cat具备语法高亮、行号显示和Git集成功能。
快速入门
基本用法
# View file with syntax highlighting
bat README.md
# Multiple files
bat file1.js file2.py
# With line numbers (default)
bat script.sh
# Without line numbers
bat -p script.sh
查看模式
# Plain mode (like cat)
bat -p file.txt
# Show non-printable characters
bat -A file.txt
# Squeeze blank lines
bat -s file.txt
# Paging (auto for large files)
bat --paging=always file.txt
bat --paging=never file.txt
语法高亮
语言检测
# Auto-detect from extension
bat script.py
# Force specific language
bat -l javascript config.txt
# Show all languages
bat --list-languages
主题
# List available themes
bat --list-themes
# Use specific theme
bat --theme="Monokai Extended" file.py
# Set default theme in config
# ~/.config/bat/config: --theme="Dracula"
行范围
# Show specific lines
bat -r 10:20 file.txt
# From line to end
bat -r 100: file.txt
# Start to specific line
bat -r :50 file.txt
# Multiple ranges
bat -r 1:10 -r 50:60 file.txt
Git集成
# Show Git modifications (added/removed/modified lines)
bat --diff file.txt
# Show decorations (Git + file header)
bat --decorations=always file.txt
输出控制
# Output raw (no styling)
bat --style=plain file.txt
# Customize style
bat --style=numbers,changes file.txt
# Available styles: auto, full, plain, changes, header, grid, numbers, snip
bat --style=header,grid,numbers file.txt
常见用例
快速预览文件:

bat file.json
查看带语法高亮的日志:
bat error.log
可视化比较文件:
bat --diff file1.txt
bat file2.txt
编辑前预览:
bat config.yaml && vim config.yaml
管道中的cat替代:
bat -p file.txt | grep "pattern"
查看特定函数:
bat -r 45:67 script.py # If function is on lines 45-67
与其他工具集成
作为man页面的分页器:
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
man grep
与ripgrep配合使用:
rg "pattern" -l | xargs bat
与fzf配合使用:
fzf --preview 'bat --color=always --style=numbers {}'
与diff配合使用:
diff -u file1 file2 | bat -l diff
配置
创建~/.config/bat/config用于设置默认值:
# Set theme
--theme="Dracula"
# Show line numbers, Git modifications and file header, but no grid
--style="numbers,changes,header"
# Use italic text on terminal
--italic-text=always
# Add custom mapping
--map-syntax "*.conf:INI"
性能提示
- 在管道操作时使用
-p以启用纯文本模式 - 当以编程方式使用输出时,使用
--paging=never来禁用分页功能 bat会缓存已解析的文件以加快后续访问速度
技巧
- 别名:
alias cat='bat -p'可替代cat命令 - 分页器:使用以下命令将其设为分页器:
export PAGER="bat" - 在Debian/Ubuntu系统上:命令可能是
batcat而非bat - 自定义语法:添加到
~/.config/bat/syntaxes/ - 性能:对于超大文件,使用
bat --paging=never或普通的cat
常用标志
-p/--plain:纯文本模式(无行号/装饰)-n/--number:仅显示行号-A/--show-all:显示不可打印字符-l/--language:设置语法高亮语言-r/ --line-range : 仅显示特定的行范围 文档 GitHub: https://github.com/sharkdp/bat 手册页: man bat 自定义: https://github.com/sharkdp/bat#customization--line-range: Only show specific line range(s)
Documentation
GitHub:https://github.com/sharkdp/batMan page:man batCustomization:https://github.com/sharkdp/bat#customization
文章底部电脑广告
手机广告位-内容正文底部


微信扫一扫,打赏作者吧~