GistTree.Com
Entertainment at it's peak. The news is by your side.

bat, a cat(1) clone with syntax highlighting, Git integration written in Rust

0

bat - a cat clone with wings

Build Status
license
Version info


A cat(1) clone with syntax highlighting and Git integration.

Key Aspects
How To Spend
Set up
Customization
Mission needs, picks


[中文] [日本語] [한국어] [Русский]

Syntax highlighting

bat helps syntax highlighting for an infinite quantity of programming and markup
languages:

Syntax highlighting example

Git integration

bat communicates with git to level to modifications with respect to the index
(search for left side bar):

Git integration example

Display camouflage non-printable characters

You would possibly be ready to use the -A/--level to-all probability to level to and highlight non-printable
characters:

Non-printable character example

Automatic paging

bat can pipe its absorb output to a pager (e.g much less) if the output is too wide for one show camouflage camouflage.

File concatenation

Oh.. you would possibly maybe more than doubtless more than doubtless additionally use it to concatenate recordsdata 😉. Every time
bat detects a non-interactive terminal (i.e. whenever you occur to pipe into one more assignment
or into a file), bat will act as a drop-in substitute for cat and
tumble relief to printing the straightforward file contents.

How one can use

Display camouflage a single file on the terminal

Display camouflage extra than one recordsdata straight away

Learn from stdin, resolve the syntax robotically (present, highlighting will
entirely work if the syntax would possibly maybe additionally be definite from the foremost line of the file,
in overall via a shebang equivalent to #!/bin/sh)

> curl -s https://sh.rustup.rs | bat

Learn from stdin, specify the language explicitly

> yaml2json .travis.yml | json_pp | bat -l json

Display camouflage and highlight non-printable characters:

Spend it as a cat substitute:

bat > present.md  # like a flash create a brand new file

bat header.md enlighten.md footer.md > document.md

bat -n main.rs  # level to line numbers (entirely)

bat f - g  # output 'f', then stdin, then 'g'.

Integration with other tools

salvage or fd

You would possibly be ready to use the -exec probability of salvage to preview all search results with bat:

In the event you occur to make use of fd, you would possibly maybe more than doubtless more than doubtless use the -X/--exec-batch probability to produce the the same:

ripgrep

With batgrep, bat would possibly maybe additionally be primitive as the printer for ripgrep search results.

tail -f

bat would possibly maybe additionally be mixed with tail -f to continually monitor a given file with syntax highlighting.

tail -f /var/log/pacman.log | bat --paging=never -l log

Indicate that we’ve to swap off paging in reveal for this to work. We possess additionally specified the syntax
explicitly (-l log), as it’ll not be auto-detected on this case.

git

You would possibly be ready to combine bat with git level to to seek an older model of a given file with just precise syntax
highlighting:

git level to v0.6.0:src/main.rs | bat -l rs

Indicate that syntax highlighting within diffs is currently not supported. In the event you would possibly maybe additionally be hunting for this, are attempting delta.

xclip

The road numbers and Git modification markers in the output of bat can produce it mighty to repeat
the contents of a file. To pause this, you would possibly maybe more than doubtless more than doubtless call bat with the -p/--straightforward probability or
merely pipe the output into xclip:

bat will detect that the output is being redirected and print the straightforward file contents.

man

bat would possibly maybe additionally be primitive as a colorizing pager for man, by environment the
MANPAGER environment variable:

export MANPAGER="sh -c 'col -bx | bat -l man -p'"
man 2 prefer

It’s a ways going to additionally additionally be main to spot MANROFFOPT="-c" whenever you abilities
formatting complications.

In the event you seize to possess this bundled in a brand new reveal, you would possibly maybe more than doubtless more than doubtless additionally use batman.

Indicate that the Manpage syntax is developed on this repository and peaceful wants some work.

Additionally, present that this would possibly doubtless not work with Mandocs man implementation.

prettier / shfmt / rustfmt

The prettybat script is a wrapper that will structure code and print it with bat.

Set up

Packaging status

On Ubuntu (the usage of just precise)

… and other Debian-based completely mostly Linux distributions.

bat is making its formulation via the Ubuntu and
Debian bundle originate assignment, and is available
for Ubuntu as of Eoan 19.10. On Debian bat is currently entirely available on the unstable
“Sid” branch.

In case your Ubuntu/Debian set up is new enough you would possibly maybe more than doubtless more than doubtless merely speed:

In the event you put in bat this type, please present that the executable would possibly maybe additionally be place in as batcat in spot of bat (on account of a title
conflict with one more bundle
). You would possibly be ready to spot up a bat -> batcat symlink or alias to pause any components that will advance up because of of this and to be in holding with other distributions:

mkdir -p ~/.native/bin
ln -s /usr/bin/batcat ~/.native/bin/bat

On Ubuntu (the usage of most modern .deb programs)

… and other Debian-based completely mostly Linux distributions.

If the bundle has not yet been promoted to your Ubuntu/Debian set up, otherwise you favor
the most modern originate of bat, download the most modern .deb bundle from the
originate page and install it via:

sudo dpkg -i bat_0.16.0_amd64.deb  # adapt model quantity and structure

On Alpine Linux

You would possibly be ready to install the bat bundle
from the legit sources, offered you would possibly maybe possess the precise repository enabled:

On Arch Linux

You would possibly be ready to install the bat bundle
from the legit sources:

On Fedora

You would possibly be ready to install the bat bundle from the legit Fedora Modular repository.

On Gentoo Linux

You would possibly be ready to install the bat bundle
from the legit sources:

On Void Linux

You would possibly be ready to install bat via xbps-install:

On FreeBSD

You would possibly be ready to install a precompiled bat bundle with pkg:

or invent it on your absorb from the FreeBSD ports:

cd /usr/ports/textproc/bat
produce install

By assignment of nix

You would possibly be ready to install bat the usage of the nix bundle supervisor:

On openSUSE

You would possibly be ready to install bat with zypper:

On macOS (or Linux) via Homebrew

You would possibly be ready to install bat with Homebrew on MacOS or Homebrew on Linux:

On macOS via MacPorts

Or install bat with MacPorts:

On Dwelling windows

There are a pair of choices to install bat on Dwelling windows. At the same time as you would possibly maybe possess place in bat,
prefer a learn at the “Using bat on Dwelling windows” half.

Requirements

You would possibly prefer to install the Visible C++ Redistributable bundle.

With Chocolatey

You would possibly be ready to install bat via Chocolatey:

With Scoop

You would possibly be ready to install bat via scoop:

From prebuilt binaries:

You would possibly be ready to download prebuilt binaries from the Unencumber page,

You would possibly prefer to install the Visible C++ Redistributable bundle.

From binaries

Take a look at out the Unencumber page for
prebuilt variations of bat for many assorted architectures. Statically-linked
binaries are additionally available: search for archives with musl in the file title.

From source

In the event you would possibly maybe seize to invent bat from source, you wish Rust 1.40 or
increased. You would possibly be ready to then use cargo to invent everything:

cargo install --locked bat

Indicate that additional recordsdata just like the man page or shell completion
recordsdata can’t be place in on this type. They’ll doubtless be generated by cargo and would possibly maybe merely be available in the cargo target folder (below invent).

Customization

Highlighting theme

Spend bat --listing-issues to get a listing of all available issues for syntax
highlighting. To make a different the TwoDark theme, call bat with the
--theme=TwoDark probability or spot the BAT_THEME environment variable to
TwoDark. Spend export BAT_THEME="TwoDark" in your shell’s startup file to
produce the swap permanent. Alternatively, use bats
configuration file.

In the event you would possibly maybe seize to preview the assorted issues on a custom file, you would possibly maybe more than doubtless more than doubtless use
the next reveal (you wish fzf for this):

bat --listing-issues | fzf --preview="bat --theme={} --color=always /course/to/file"

bat appears to be like staunch on a unlucky background by default. On the opposite hand, in case your terminal uses a
gentle background, some issues like GitHub or OneHalfLight will work better for you.
You would possibly be ready to additionally use a custom theme by following the
‘Along side new issues’ half below.

8-bit issues

bat has four issues that in overall use 8-bit colours,
even when truecolor strengthen is available:

  • ansi-darkish appears to be like first rate on any terminal with a unlucky background. It uses 3-bit colours: dark, crimson,
    green, yellow, blue, magenta, cyan, and white.
  • ansi-gentle is like ansi-darkish, however for terminals with a gentle background.
  • unsuitable16 is designed for unsuitable16 terminal issues. It uses
    4-bit colours (3-bit colours plus sparkling variants) in holding with the
    unsuitable16 styling pointers.
  • unsuitable16-256 is designed for unsuitable16-shell.
    It replaces sure sparkling colours with 8-bit colours from 16 to 21. Originate not use this merely
    because of you would possibly maybe possess a 256-color terminal however are not the usage of unsuitable16-shell.

Even supposing these issues are extra restricted, they’ve two advantages over truecolor issues:

  • They harmonize better with other terminal system the usage of 3-bit or 4-bit colours.
  • Must you swap your terminal theme, bat output already on the show camouflage camouflage will change to compare.

Output type

You would possibly be ready to use the --type probability to protect watch over the appears to be like of bats output.
You would possibly be ready to use --type=numbers,adjustments, as an instance, to level to utterly Git adjustments
and line numbers however no grid and no file header. Set the BAT_STYLE environment
variable to produce these adjustments permanent or use bats
configuration file.

Along side new syntaxes / language definitions

Could well well peaceful you explore that a explicit syntax isn’t available within bat, you would possibly maybe more than doubtless more than doubtless be conscious these
instructions to with out peril add new syntaxes to your fresh bat set up.

bat uses the beautiful syntect
library for syntax highlighting. syntect can read any
Sublime Textual enlighten .sublime-syntax file
and theme.

A staunch helpful resource for discovering Sublime Syntax programs is Kit Defend watch over. At the same time as you stumbled on a
syntax:

  1. Score a folder with syntax definition recordsdata:

    mkdir -p "$(bat --config-dir)/syntaxes"
    cd "$(bat --config-dir)/syntaxes"
    
    # Place new '.sublime-syntax' language definition recordsdata
    # on this folder (or its subdirectories), as an instance: 
    git clone https://github.com/tellnobody1/sublime-purescript-syntax
  2. Now use the next divulge to parse these recordsdata into a binary cache:

  3. Sooner or later, use bat --listing-languages to examine if the new languages will doubtless be found in.

    In the event you ever prefer to return to the default settings, call:

  4. In the event you specialise in that a explicit syntax need to be integrated in bat by default, please
    possess in tips opening a “syntax demand” label after studying the insurance policies and
    instructions right here: Open Syntax Interrogate.

Along side new issues

This works very equivalent to how we add new syntax definitions.

First, create a folder with the new syntax highlighting issues:

mkdir -p "$(bat --config-dir)/issues"
cd "$(bat --config-dir)/issues"

# Bag a theme in '.tmTheme' structure, as an instance: 
git clone https://github.com/greggb/sublime-snazzy

# Update the binary cache
bat cache --invent

Sooner or later, use bat --listing-issues to examine if the new issues will doubtless be found in.

Along side or changing file form associations

You would possibly be ready so that you’ll want to add new (or swap existing) file title patterns the usage of the --map-syntax
reveal line probability. The probability takes an argument of the function pattern:syntax the place
pattern is a glob pattern that is matched in opposition to the file title and
absolutely the file course. The syntax half is the rotund title of a supported language
(use bat --listing-languages for an outline)rotund .

Indicate: You nearly no doubt prefer to make use of this probability as an entry in bats configuration file as an different
of passing it on the reveal line (search for below).

Instance: To use “INI” syntax highlighting for all recordsdata with a .conf file extension, use

--map-syntax='*.conf:INI'

Instance: To begin all recordsdata known as .ignore (staunch match) with the “Git Ignore” syntax, use:

--map-syntax='.ignore:Git Ignore'

Instance: To begin all .conf recordsdata in subfolders of /and loads others/apache2 with the “Apache Conf”
syntax, use (this mapping is already in-constructed):

-map-syntax='/and loads others/apache2//*.conf:Apache Conf'

Using a definite pager

bat uses the pager that is specified in the PAGER environment variable. If this variable isn’t
spot, much less is primitive by default. In the event you would possibly maybe seize to make use of a definite pager, you would possibly maybe more than doubtless more than doubtless both regulate the
PAGER variable or spot the BAT_PAGER environment variable to override what is specified in
PAGER.

In the event you would possibly maybe seize to pass reveal-line arguments to the pager, you would possibly maybe more than doubtless more than doubtless additionally spot them via the
PAGER/BAT_PAGER variables:

export BAT_PAGER="much less -RF"

In spot of the usage of environment variables, you would possibly maybe more than doubtless more than doubtless additionally use bats configuration file to configure the pager (--pager probability).

Indicate: By default, if the pager is determined to much less (and no reveal-line choices are specified),
bat will pass the next reveal line choices to the pager: -R/--RAW-CONTROL-CHARS,
-F/--quit-if-one-show camouflage camouflage and -X/--no-init. The final probability (-X) is entirely primitive for much less
variations older than 530.

The -R probability is wished to interpret ANSI colours accurately. The 2nd probability (-F) instructs
much less to exit straight away if the output measurement is smaller than the vertical measurement of the terminal.
Here is handy for small recordsdata because of you produce not prefer to press q to quit the pager. The
third probability (-X) is wished to repair a malicious program with the --quit-if-one-show camouflage camouflage feature in primitive variations
of much less. Sadly, it additionally breaks mouse-wheel strengthen in much less.

In the event you would possibly maybe seize to permit mouse-wheel scrolling on older variations of much less, you would possibly maybe more than doubtless more than doubtless pass just precise -R (as
in the instance above, this would possibly doubtless disable the quit-if-one-show camouflage camouflage feature). For much less 530 or more moderen,
it need to determine of the sector.

Indentation

bat expands tabs to 4 areas by itself, not relying on the pager. To swap this, merely add the
--tabs argument with the quantity of areas that you must be displayed.

Indicate: Defining tab stops for the pager (via the --pager argument by bat, or via the LESS
environment variable for much less) would possibly maybe additionally not be taken into fable for the reason that pager will already get
expanded areas in spot of tabs. This behaviour is added to steer obvious of indentation components triggered by the
sidebar. Calling bat with --tabs=0 will override it and let tabs be consumed by the pager.

Darkish mode

In the event you produce use of the darkish mode feature in macOS, it is your decision to configure bat to make use of a definite
theme in holding with the OS theme. The next snippet uses the default theme when in the darkish mode
and the GitHub theme when in the gentle mode.

alias cat="bat --theme=$(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo default || echo GitHub)"

Configuration file

bat can additionally be customized with a configuration file. The spot of the file depends
on your working system. To get the default course in your system, call

bat --config-file

Alternatively, you would possibly maybe more than doubtless more than doubtless use the BAT_CONFIG_PATH environment variable to level bat to a
non-default spot of the configuration file:

export BAT_CONFIG_PATH="/course/to/bat.conf"

A default configuration file would possibly maybe additionally be created with the --generate-config-file probability.

bat --generate-config-file

Format

The configuration file is a straightforward listing of reveal line arguments. Spend bat --relief to examine a rotund listing of imaginable choices and values. In addition, you would possibly maybe more than doubtless more than doubtless add feedback by prepending a line with the # persona.

Instance configuration file:

# Set the theme to "TwoDark"
--theme="TwoDark"

# Display camouflage line numbers, Git modifications and file header (however no grid)
--type="numbers,adjustments,header"

# Spend italic text on the terminal (not supported on all terminals)
--italic-text=always

# Spend C++ syntax for .ino recordsdata
--map-syntax "*.ino:C++"

Using bat on Dwelling windows

bat largely works out-of-the-field on Dwelling windows, however a pair of components would possibly maybe additionally need additional configuration.

Requirements

You would possibly prefer to install the Visible C++ Redistributable bundle.

Paging

Dwelling windows entirely entails a truly restricted pager in the function of extra. You would possibly be ready to download a Dwelling windows binary
for much less from its homepage or via
Chocolatey
. To use it, spot the binary in a listing in
your PATH or account for an environment variable. The Chocolatey bundle installs much less robotically.

Colours

Dwelling windows 10 natively helps colours in each conhost.exe (Expose In actual fact helpful) and PowerShell since
v1511, as
neatly as in more moderen variations of bash. On earlier variations of Dwelling windows, you would possibly maybe more than doubtless more than doubtless use
Cmder, which entails ConEmu.

Indicate: The Git and MSYS variations of much less produce not accurately interpret colours on Dwelling windows. In the event you
don’t possess every other pagers place in, you would possibly maybe more than doubtless more than doubtless disable paging completely by passing --paging=never
or by environment BAT_PAGER to an empty string.

Cygwin

bat on Dwelling windows would not natively strengthen Cygwin’s unix-type paths (/cygdrive/*). When handed an absolute cygwin course as an argument, bat will encounter the next error: The system can't salvage the path specified. (os error 3)

This would possibly maybe additionally be solved by making a wrapper or adding the next feature to your .bash_profile file:

bat() {
    native index
    native args=("$@")
    for index in $(seq 0 ${#args[@]}) ; produce
        case "${args[index]}" in
        -*) proceed;;
        *)  [ -e "${args[index]}" ] && args[index]="$(cygpath --dwelling windows "${args[index]}")";;
        esac
    performed
    reveal bat "${args[@]}"
}

Troubleshooting

Terminals & colours

bat handles terminals with and with out truecolor strengthen. On the opposite hand, the colors in most syntax
highlighting issues are not optimized for 8-bit colours. It’s a ways subsequently strongly suggested
that you make use of a terminal with 24-bit truecolor strengthen (terminator, konsole, iTerm2, …),
or use one in every of the foremost 8-bit issues designed for a restricted spot of colours.
Scrutinize this text for additional essential components and a rotund listing of
terminals with truecolor strengthen.

Designate definite your truecolor terminal sets the COLORTERM variable to both truecolor or
24bit. Otherwise, bat isn’t going to be ready to discover whether or not or not 24-bit speed sequences
are supported (and tumble relief to 8-bit colours).

Line numbers and grid are not ceaselessly considered

Please are attempting a definite theme (search for bat --listing-issues for a listing). The OneHalfDark and
OneHalfLight issues provide grid and line colours that are brighter.

File encodings

bat natively helps UTF-8 moreover to UTF-16. For every other file encoding, you would possibly maybe additionally prefer to
convert to UTF-8 first for the reason that encodings can in overall not be auto-detected. You would possibly be ready to iconv
to produce so.
Instance: whenever you would possibly maybe possess a PHP file in Latin-1 (ISO-8859-1) encoding, you would possibly maybe more than doubtless more than doubtless call:

iconv -f ISO-8859-1 -t UTF-8 my-file.php | bat

Indicate: you possess to make use of the -l/--language probability if the syntax can’t be auto-detected
by bat.

Style

# Recursive clone to retrieve all submodules
git clone --recursive https://github.com/sharkdp/bat

# Bring together (debug model)
cd bat
cargo invent --bins

# Traipse unit tests and integration tests
cargo test

# Install (originate model)
cargo install --course . --locked

# Bring together a bat binary with modified syntaxes and issues
bash resources/create.sh
cargo install --course . --locked --force

Maintainers

Mission needs and picks

bat tries to produce the next needs:

  • Provide beautiful, developed syntax highlighting
  • Integrate with Git to level to file modifications
  • Be a drop-in substitute for (POSIX) cat
  • Offer a person-apt reveal-line interface

There are heaps of picks, whenever you would possibly maybe additionally be hunting for identical applications. Scrutinize
this document for a comparability.

License

Copyright (c) 2018-2020 bat-builders.

bat is made available below the terms of both the MIT License or the Apache License 2.0, at your probability.

Scrutinize the LICENSE-APACHE and LICENSE-MIT recordsdata for license essential components.

Read More

Leave A Reply

Your email address will not be published.