もた日記

くだらないことを真面目にやる

Linuxメモ : bat(シンタックスハイライト可能なcat)を試してみる

bat

github.com

batは"A cat(1) clone with syntax highlighting and Git integration."と説明されているcatの代わりとして使えるコマンドで以下のような特徴がある。

  • シンタックスハイライト
  • Git連携(変更点の表示)
  • non-printing character(スペース、改行など)の表示
  • 画面に収まらない場合は自動でPAGER(デフォルトはless)起動

f:id:wonder-wall:20190227210448p:plain

インストール

各環境でのインストール方法はこのページに書いてある。
バイナリはこのページからダウンロードでき、 Rust製なのでcargo installでインストールできる。

$ cargo install bat

ヘルプメッセージ。

$ bat --help
bat 0.10.0
A cat(1) clone with syntax highlighting and Git integration.

USAGE:
    bat [OPTIONS] [FILE]...
    bat <SUBCOMMAND>

OPTIONS:
    -l, --language <language>
            Explicitly set the language for syntax highlighting. The language can be specified as a
            name (like 'C++' or 'LaTeX') or possible file extension (like 'cpp', 'hpp' or 'md'). Use
            '--list-languages' to show all supported language names and file extensions.
        --list-languages
            Display a list of supported languages for syntax highlighting.

    -m, --map-syntax <from:to>...
            Map a file extension or file name to an existing syntax. For example, to highlight
            *.conf files with the INI syntax, use '-m conf:ini'. To highlight files named
            '.myignore' with the Git Ignore syntax, use '-m .myignore:gitignore'.
        --theme <theme>
            Set the theme for syntax highlighting. Use '--list-themes' to see all available themes.
            To set a default theme, add the '--theme="..."' option to the configuration file or
            export the BAT_THEME environment variable (e.g.: export BAT_THEME="...").
        --list-themes
            Display a list of supported themes for syntax highlighting.

        --style <style-components>
            Configure which elements (line numbers, file headers, grid borders, Git modifications,
            ..) to display in addition to the file contents. The argument is a comma-separated list
            of components to display (e.g. 'numbers,changes,grid') or a pre-defined style ('full').
            To set a default style, add the '--style=".."' option to the configuration file or
            export the BAT_STYLE environment variable (e.g.: export BAT_STYLE=".."). Possible
            values: *auto*, full, plain, changes, header, grid, numbers.
    -p, --plain
            Only show plain style, no decorations. This is an alias for '--style=plain'

    -n, --number
            Only show line numbers, no other decorations. This is an alias for '--style=numbers'

    -A, --show-all
            Show non-printable characters like space, tab or newline. Use '--tabs' to control the
            width of the tab-placeholders.
    -r, --line-range <N:M>...
            Only print the specified range of lines for each file. For example:
              '--line-range 30:40' prints lines 30 to 40
              '--line-range :40' prints lines 1 to 40
              '--line-range 40:' prints lines 40 to the end of the file
    -H, --highlight-line <N>...
            Highlight the N-th line with a different background color

        --color <when>
            Specify when to use colored output. The automatic mode only enables colors if an
            interactive terminal is detected. Possible values: *auto*, never, always.
        --italic-text <when>
            Specify when to use ANSI sequences for italic text in the output. Possible values:
            always, *never*.
        --decorations <when>
            Specify when to use the decorations that have been specified via '--style'. The
            automatic mode only enables decorations if an interactive terminal is detected. Possible
            values: *auto*, never, always.
        --paging <when>
            Specify when to use the pager. To control which pager is used, set the PAGER or
            BAT_PAGER environment variables (the latter takes precedence) or use the '--pager'
            option. To disable the pager permanently, set BAT_PAGER to an empty string or set
            '--paging=never' in the configuration file. Possible values: *auto*, never, always.
        --pager <command>
            Determine which pager is used. This option will overwrite the PAGER and BAT_PAGER
            environment variables. The default pager is 'less'. To disable the pager completely, use
            the '--paging' option. Example: '--pager "less -RF"'.
        --wrap <mode>
            Specify the text-wrapping mode (*auto*, never, character).

        --tabs <T>
            Set the tab width to T spaces. Use a width of 0 to pass tabs through directly

    -u, --unbuffered
            This option exists for POSIX-compliance reasons ('u' is for 'unbuffered'). The output is
            always unbuffered - this option is simply ignored.
        --terminal-width <width>
            Explicitly set the width of the terminal instead of determining it automatically. If
            prefixed with '+' or '-', the value will be treated as an offset to the actual terminal
            width.
    -h, --help
            Print this help message.

    -V, --version
            Show version information.


ARGS:
    <FILE>...
            File(s) to print / concatenate. Use a dash ('-') or no argument at all to read from
            standard input.

SUBCOMMANDS:
    cache    Modify the syntax-definition and theme cache

使い方

基本的にはcatと同じように使うことができる。 代表的なオプションとしては、

  • -rで表示行を指定
  • -Hでハイライトする行を指定
  • -pで簡易表示
  • -nで行番号付き簡易表
  • -Aでnon-printing character(スペース、改行など)の表示

がある。

f:id:wonder-wall:20190227214437p:plain

言語指定

言語は自動で判別されるようだが明示的に指定する場合は-lオプションを使う。
言語一覧は--list-languagesで確認可能。

f:id:wonder-wall:20190227210555p:plain

カラーテーマ

指定可能なカラーテーマは--list-themesで確認可能。

f:id:wonder-wall:20190227210608p:plain

fzfを使っているなら自分の好きなファイルに対して下記コマンドでプレビューできる。

$ bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file"

f:id:wonder-wall:20190227210637p:plain

カラーテーマは、

$ bat --theme=TwoDark fibonacci.py

のようにオプションで指定するか、

export BAT_THEME="TwoDark"

のように環境変数で指定する。

Git連携

Git管理下にあるファイルに対しては変更点が左端に表示される。

f:id:wonder-wall:20190227210625p:plain