The BAT recommended in this issue is similar to cat (1), with many features such as git integration and syntax highlighting.
major function
syntax on
BAT provides high language syntax for programming language highlighting and annotation:
Git Integration
BAT can retrieve file modifications from Git and display them in the sidebar (see figure below):
Non printable display printing visualization
Adding the – A/– show all parameter can include non printable characters in the file:
Collate
BAT will generally output the content displayed within a larger screen display range to a paging device (pager, such as less).
You can add the — paging=never parameter when calling to bat without using a paginator (just like cat). If you want to use the bat alias for cat, you can add alias cat=’bat — paging=never ‘in the shell configuration file (shell configuration).
Intelligent output
BAT can pipeline while setting paginator options, allowing non exemplary clients to use the current file BAT or pipeline image, and output the content to the venue through the pipeline at once without considering the parameter content.
How to use
View a file in the terminal
> bat README.md
Display multiple files
> bat src/*.rs
Read streaming annotations from STDIN, add syntax highlighting to the input content based on the first item, and automatically recognize whether the language of the input content is correct, usually using the shebang tag on the content line, #! Bin/sh as
> curl -s https://sh.rustup.rs | bat
Language explicitly specifies the input of STDIN
> yaml2json .travis.yml | json_pp | bat -l json
Display non printable characters
> bat -A /etc/hosts
Similar to cat
bat > note.md # Create an empty file
bat header.md content.md footer.md > document.md
bat -n main.rs # Only display line numbers
bat f - g # Output f, followed by the standard input stream, and finally g
tool
fzf
You can use bat as the fzf previewer. This requires loading the — color=always option after bat, as well as adding the — line range option to limit the number of large files.
fzf --preview 'bat --color=always --style=numbers --line-range=:500 {}'
Find or fd
You can use the – exec option of find to search for results using bat:
find … -exec bat {} +
Alternatively, when using fd, add the – X/– exec batch option:
fd … -X bat
ripgrep
Batgrep can also display search results for ripgrep.
batgrep needle src/
tail -f
When paired with tail-f, bat can continuously monitor file content and highlight it using different syntax.
tail -f /var/log/pacman.log | bat --paging=never -l log
git
Of course, bat can also accept output from git show and directly customize syntax highlighting (syntax also needs to be manually specified):
git show v0.6.0:src/main.rs | bat -l rs
man
Bat also requires setting the output MANPAGER environment variable for man:
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
man 2 select
Ubuntu (using apt)
If you are unable to use one installation method or need to use the latest version of bat, you can download the latest. deb package from the release page and install it using the following method:
sudo dpkg -i bat_0.18.3_amd64.deb # adapt version number and architecture
Arch Linux
You can install the bat package from the official source using the following command:
pacman -S bat
Funtoo Linux
You can install the bat package from dev kit:
emerge sys-apps/bat
Gentoo Linux
You can use the following command to install the bat package from the official source:
emerge sys-apps/bat
customize
Grammar Highlighting Theme
Using the syntax bat — list topics, also — theme=TwoDark, highlight using the specified theme as TwoDark. You can configure the theme selection by setting the BAT_THEME environment. Add export BAT_THEME=”TwoDark” to the shell startup script (shell startup file) to achieve a permanent effect. Or use bat files
To view the display of all themes in one file, you can use the following command (fzf needs to be installed):
bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file"
By default, bat can add excellent effects on black theme backgrounds. If your terminal uses a bright background, you can add GitHub or OneHalfLight to the theme to add custom themes.
8 themes
Bat comes with three 8-bit color themes:
- Ansi is suitable for most scenarios. It uses 3-bit colors: black, red, yellow, blue, magenta, indigo, cyan, and white.
- Base16 is designed specifically for Base16 terminals. It uses 4-bit color (free 3-bit color). Make according to the Base16 styling guide.
- Base16-25 should be designed for base16 shell. It replaces some bright colors with 8-bit colors. Please do not directly use the theme if your 256 color terminal is using base16 shell.
These themes have unique colors, but some real themes do not have:
- All terminal tools support 3-bit color.
- Meet the terminal theme.
- Visually more coordinated with other terminal tools.
Add new language and grammar
When the existing bat does not support your own language or syntax, you can add it.
Bat uses the syntax library to support syntax highlighting, which utilizes Sublime Text.subrime-syntax syntax files and themes.
When you find a grammar file, follow the following steps:
1. Create a directory containing syntax description files:
mkdir -p "$(bat --config-dir)/syntaxes"
cd "$(bat --config-dir)/syntaxes"
# Put new '.sublime-syntax' language definition files
# in this folder (or its subdirectories), for example:
git clone https://github.com/tellnobody1/sublime-purescript-syntax
2. Function call instructions to convert files into cache:
bat cache --build
3. bat — list languages: Finally, check if the new syntax has been successfully imported. To roll back to the initial state, use:
bat cache --clear
Use custom paginator
BAT uses a paginator defined by the PAGER environment variable, and if not defined, uses less. Bat provides the BAT-PAGER environment variable to specifically select a paginator for bat (prioritizing PAGER).
Note: When PAGER is set to more or most, bat can replace less to ensure color support.
export BAT_PAGER="less -RF"
In addition to using environment variables to change the paginator used by bat, the — pager parameter can also be provided in the configuration file.
Note: The parameter parameters need to be specified in the ANSI standard color description: bat/- R, — RAW-Control-CHARS/- F, and — quit if one screen/- X (– no int only applies to other interpretations). When the terminal is vertically sized, it can be played normally on one screen. Support mouse scroll wheel cancellation function.
-R-Flessq-X-Fless-Rquit-if-one-screen。
configuration file
bat --config-file
You can also use BATVNet PATH to bat configuration files that specify custom locations:
export BAT_CONFIG_PATH="/path/to/bat.conf"
Using the — generate config file parameter to call bat will generate a default bat configuration file at the specified location:
bat --generate-config-file
—END—
Open Source Protocol: LICENSE-2.0