公開日: 2023年9月17日

Git 第4回: Tig をカスタマイズしよう

前回は Tig の基本的な操作方法を説明しました。今回は Tig のカスタマイズに関する話題です。

設定ファイルの作成と読み込み

Tig のデフォルト設定は /usr/local/etc/tigrc に書かれています。自分でカスタマイズするには、ホームディレクトリに .tigrc を作成し、デフォルト設定を上書きしていくことになります。

~/.tigrc を変更したら、Tig を再起動するか、[:] キーでプロンプトを開いて source コマンドで読み込んでください (もしかしたら、そのあと [R] キーでビューを再読み込みする必要があるかも)。

:source ~/.tigrc

ビューの表示項目に関するカスタマイズ

ビューに表示する情報の変更はsetコマンドで行います。構文は次のとおりです。
set variables = value
main ビューから見ていきましょう。デフォルト設定を /usr/local/etc/tigrc で確認すると次のようになっています。
/usr/local/etc/tigrc
set main-view   = line-number:no,interval=5 id:no date:default,format="%Y-%m-%d" author:full commit-title:yes,graph,refs,overflow=no

この意味はだいたい次のようになります (より詳しくは tigrc(5) のマニュアルを参照)。

  • line-number:no,interval=5

    行番号を表示しない (表示する場合 (line-number:yes の場合) は 5 行間隔で表示)。

  • id:no

    コミットハッシュを表示しない。

  • date:default,format="%Y-%m-%d"

    日付を「2023-08-21 13:18」のように表示する。

  • author:full

    作成者名をフルネームで表示する。

  • comit-title:yes,graph,refs,overflow=no

    コミットタイトル、コミットグラフ、参照 (ブランチ名、タグ、リモートブランチ) を表示する、決められた幅からはみ出たテキストをハイライトしない。

実際の見た目は図 1 (a) のようになります。

カスタマイズの取っ掛かりとして、main ビューの各行にコミットハッシュを表示させましょう。/usr/local/etc/tigrc からコピーして id:noid:yesに書き換えます。

~/.tigrc
set main-view = line-number:no,interval=5 id:yes date:default,format="%Y-%m-%d" author:full commit-title:yes,graph,refs,overflow=no

または次のような書き方も可能です。

~/.tigrc (別解)
set main-view-id = yes

この書き方により、id:noの部分だけid:yesに上書きしてくれます。

Tig を再起動するか、source コマンドで ~/.tigrc を読み込み直すかすると、図 1 (b) のように左端にコミットハッシュの列が追加されます。

図 1 (a)
図 1 (a)
図 1 (b)
図 1 (b)

他のビューもこんな調子で、マニュアル片手にいろいろやってみましょう。私は blame ビューでコミットハッシュに色がつくのが気になったので、それをやめてみました (図 2 (a),(b))。どこが原因なのか探すのに苦労しましたが、結局 /usr/local/etc/tigrc の次の行でした。

/usr/local/etc/tigrc
set blame-view  = id:yes,color file-name:auto author:full date:default,format="%Y-%m-%d" line-number:yes,interval=1 text

この行を ~/.tigrc にコピペしてid:yes,colorid:yesに書き換えます。

~/.tigrc
set blame-view = id:yes file-name:auto author:full date:default,format="%Y-%m-%d" line-number:yes,interval=1 text

ちなみに、先ほどの流れでいくと

set blame-view-id = yes

と書いてもよさそうな気がしますが、だめでした。カンマ以降の記述は無視されるっぽい?です。

図 2 (a): コミットハッシュが謎な基準で色分けされている…
図 2 (a): 左端が謎な基準で色分けされている…
図 2 (b): だったらこれでいいよもう
図 2 (b): だったらこれでいいよもう

ビューの設定例:

~/.tigrc
set main-view-id = yes
set blame-view = id:yes file-name:auto author:full date:default,format="%Y-%m-%d" line-number:yes,interval=1 text

キーバインドのカスタマイズ

キーバインドの変更はbindコマンドで行います。構文は次のとおりです。
bind keymap key action
例えば、[G] キーには main ビューでコミットグラフの有無を切り替える機能が割り当てられていますが、「そこは最終行に移動だろ」と思われる方も一定数おられると思います。そんなときは次のように書いておきます。
~/.tigrc
bind main G move-last-line
bind generic G move-last-line

keymapには各ビューに共通のgeneric、または特定のビューに限定するmaindifflogrefloghelppagerstatusstagetreeblobblamerefsstashgrepの中からいずれかを選んで指定します。

であれば上の例ではgenericの行だけ書いておけば十分な気がしますが、特定のビューの設定はgenericの設定に優先するので、maingenericをともに指定する必要があります。もしgenericの行だけだと、/usr/local/etc/tigrc に

bind main G :toggle commit-title-graph

と書かれているので、そちらが優先されてしまいます。

actionにはmove-last-lineを指定しています。その他にどんなアクションがあるかは tigrc(5) マニュアルの Action names の節を参照してください。

また、Tig の持つ機能だけでなく、Git コマンドをキーにバインドすることもできます。

~/.tigrc
bind main C ?git cherry-pick -x %(commit)
bind main T !git tag -a "%(prompt tag name: )" %(commit)

この例では main ビューで [C] キーを押すと、カーソル位置のコミットをチェリーピックします。また、[T] キーを押すとカーソル位置のコミット時点でのリポジトリにタグを打ちます。

?」と「!」のちがいは、「?」のほうではコマンドを実行する前に本当に実行するかどうかの確認が行われます。

%(prompt)」と書くと、ユーザー入力を促すプロンプトが表示され、入力された内容がコマンドに反映されます。上の例のように「%(prompt)」に続けて文字列を書くと、その文字列がプロンプトとして表示されます。

キーバインドの設定例:

次の表のように設定します。
ビューキー機能
generic

; (セミコロン)

デバッグ用にいろいろ表示する

generic

g

1 行目に移動

generic

G

最終行に移動

generic

Ctrl-k

半ページ上へ

generic

Ctrl-j

半ページ下へ

generic

?

grep ビュー

generic

p

文字列検索時、ひとつ前のマッチ文字列へ移動

generic

Backspace

ひとつ前のビューに戻る

generic

S

スタッシュ,未追跡ファイルを含める,コメントなし

generic

$

スタッシュ,未追跡ファイルを含める,コメントあり

main

G

最終行に移動

main

C

チェリーピック (やり方は前回の記事を参照)

main

T

カーソル位置のコミットにタグを打つ

main

<

一時的にカーソル位置のコミットまで巻き戻す (時間軸を左に進むイメージ)

main

>

巻き戻す前の位置に復帰する

refs

<

カーソル位置のブランチに切り替える,確認なし

refs

B

カーソル位置のブランチから新しいブランチを生やす

refs

M

カーソル位置のブランチを現在のブランチにマージする

stash

A

スタッシュを復旧,ステージエリアの状態も含む

stash

P

スタッシュを復旧 & 削除,ステージエリアの状態も含む

実際の ~/.tigrc は次のとおりです。
~/.tigrc
bind generic ; !sh -c "echo %(refname)"
bind generic g move-first-line
bind generic G move-last-line
bind generic <Ctrl-k> move-half-page-up
bind generic <Ctrl-j> move-half-page-down
bind generic ? view-grep
bind generic p find-prev
bind generic <Backspace> back
bind generic S ?git stash -u
bind generic $ ?git stash save "%(prompt comment: )" -u
bind main G move-last-line
bind main C ?git cherry-pick -x %(commit)
bind main T !git tag -a "%(prompt tag name: )" %(commit)
bind main <LessThan> !git switch -d %(commit)
bind main > !git switch -
bind refs <LessThen> !git switch %(branch)
bind refs B !git branch "%(prompt branch name: )" %(branch)
bind refs M ?git merge %(branch)
bind stash A ?git stash apply %(stash) --index
bind stash P ?git stash pop %(stash) --index

色のカスタマイズ

カーソル色の変更や、ハイライト表示の設定はcolorコマンドで行います。構文は次のとおりです。
color area fgcolor bgcolor [attributes]
例えば次のようにすると、カーソルの文字の色が白、背景色が緑、フォントはボールド体になります。
color cursor white green bold

areaとして指定できるものについては /usr/local/etc/tigrc を覗いてみてください。実にたくさんあるので、とりあえずデフォルトのまま使ってみて、気に入らないところがあれば /usr/local/etc/tigrc から該当する設定を見つけ出して ~/.tigrc で修正していく、というやり方が良いと思います。

また、色の設定は使用する端末エミュレータに依存すると思います。他はよく知りませんが、rxvt-unicode の場合、Tig 側の設定が同じでも rxvt-unicode 側のカラースキームが異なると、印象ががらりと変わります。rxvt-unicode のカラースキームについては、別記事で検討しているので参考にしてください。

私の場合は次の表のように設定しています。カラースキームの具体的な設定は ~/.Xresources に書くのですが、これも詳しくは上の記事リンクを参照してください。「Black といいながらオレンジじゃないか」と思われたかもしれませんが、そのあたりの事情も書いてあります。

インデックス色名RGB実際の色

(文字色)

#F7FCFE

(背景色)

#203744

color0

Black

#F08300

color1

Red

#EE827C

color2

Green

#82AE46

color3

Yellow

#F7C114

color4

Blue

#53727D

color5

Magenta

#A59ACA

color6

Cyan

#455765

color7

White

#9FA0A0

color8

Bright Black

#2B2B2B

color9

Bright Red

#A25768

color10

Bright Green

#93CA76

color11

Bright Yellow

#0D0015

color12

Bright Blue

#008899

color13

Bright Magenta

#68699B

color14

Bright Cyan

#59B9C6

color15

Bright White

#F7FCFE

以上をふまえて、設定例を提示します。どんな具合になるかは、前回や今回の記事に使っているスクリーンショットを参考にしてください。すべて上に示したカラースキームにもとづいています。

色の設定例:

~/.tigrc
set git-colors = no    # git config のカラー設定を読み込まない

#     area                           fgcolor   bgcolor   attributes
color "---"                          default   default
color "diff --"                      yellow    default
color "--- "                         default   default
color "+++ "                         default   default
color "@@"                           blue      default
color "index "                       default   default
color "old file mode "               magenta   default
color "new file mode "               magenta   default
color "deleted file mode "           magenta   default
color "copy from "                   magenta   default
color "copy to "                     magenta   default
color "rename from "                 magenta   default
color "rename to "                   magenta   default
color "similarity "                  magenta   default
color "dissimilarity "               magenta   default
color "\ No newline at end of file"  magenta   default
color "Author: "                     default   default
color "Commit: "                     default   default
color "Merge: "                      default   default
color "Date: "                       default   default
color "AuthorDate: "                 default   default
color "CommitDate: "                 default   default
color "TaggerDate: "                 default   default
color "Refs: "                       default   default
color "commit "                      black     default
color cursor                         default   blue
color status                         default   default
color date                           white     default
color mode                           white     default
color id                             white     default
color header                         white     default
color section                        white     default
color author                         white     default
color grep.file                      default   default
color file-size                      white     default
color line-number                    default   default
color title-blur                     default   cyan
color title-focus                    default   cyan
color main-tag                       yellow    default
color main-local-tag                 yellow    default
color main-remote                    yellow    default
color main-replace                   yellow    default
color main-tracked                   yellow    default
color main-ref                       yellow    default
color main-head                      black     default
color diff-stat                      default   default
color graph-commit                   white     default

その他

知っておいたら役に立つかもしれない、その他の設定を挙げておきます。
~/.tigrc
# タブの幅
set tab-size = 4

# コミットグラフに用いるキャラクタ
set line-graphics = default    # (ascii|default|utf-8|auto)

# 画面を垂直方向に分割するかどうか (yes|no|auto)
set vertical-split = auto

# 検索時に大文字小文字を無視するかどうか (true|false)
set ignore-case = true

# マウスを有効にする … とのことだが無反応なのでコメントアウト
#set mouse = true
#set mouse-scroll = 3
#set mouse-wheel-cursor = yes

今回はここまで

ここまで読んで「Tig よさげだけど、うち Windows なんだよね…」と思われたそこのあなた、Windows でも使えます!詳細は次回。