[Languages: English (英語) | 日本語]
ble.sh
(Bash Line Editor) はピュア Bash スクリプトで書かれたコマンドラインエディタで、標準の GNU Readline を置き換える形で動作します。
- 構文着色:
fish
やzsh-syntax-highlighting
のような文法構造に従った着色を行います。zsh-syntax-highlighting
のような単純な着色ではなく、構文の入れ子構造や複数のヒアドキュメントなども正しく解析して着色します。 - 補完増強: 補完を大幅に増強します。
文法構造に応じた補完、クォートやパラメータ展開を展開した上でのプログラム補完、曖昧補完に対応しています。
また、候補をカーソルキーや TAB, S-TAB で選択できるメニュー補完、
fish
やzsh-autosuggestions
のような自動補完 (Bash 4.0 以上) の機能もあります。 更に、従来peco
やfzf
を呼び出さなければならなかった補完候補の絞り込みもメニュー絞り込み (Bash 4.0 以上) として自然な形で組み込んでいます。 他に、動的略語展開や、zsh-abbreviations
のような静的略語展開にも対応しています。 - Vim編集モード:
set -o vi
による編集モードを増強します。 挿入・ノーマルモードの他に(行・矩形)ビジュアルモード、置換モードなどの各種モードに対応しています。 テキストオブジェクト・各種レジスタ・オペレータ・キーボードマクロなどにも対応しています。 拡張としてvim-surround
も提供しています。
詳細な使い方はマニュアルを御覧ください。ここでは簡単な使い方を説明します。
このスクリプトは bash-3.0
以降で利用できますが、速度・機能などの観点から Bash 4.0 以降でお使い頂くことがお薦めです。
現時点では、文字コードとして UTF-8
のみの対応です。
このスクリプトは BSD License (3条項 BSD ライセンス) の下で提供されます。
デモ
最新の git repository のソースから生成して使う場合 (バージョン ble-0.4)
ble.sh
を生成する為には gawk
(GNU awk) と gmake
(GNU make) が必要です。
以下のコマンドで生成できます。
GNU make が gmake
という名前でインストールされている場合は、make
の代わりに gmake
として下さい。
$ git clone https://github.com/akinomyoga/ble.sh.git
$ cd ble.sh
$ make
スクリプトファイル ble.sh
がサブディレクトリ ble.sh/out
内に生成されます。
source
コマンドを用いて読み込めます:
$ source out/ble.sh
指定したディレクトリにインストールするには以下のコマンドを使用します。INSDIR
の指定を省略したときは既定の場所 ${XDG_DATA_HOME:-$HOME/.local/share}/blesh
にインストールされます。
$ make INSDIR=/path/to/blesh install
ble.sh
をダウンロードして使う場合 (旧バージョン ble-0.3 alpha 201902版)
wget
を使う場合:
$ wget https://github.com/akinomyoga/ble.sh/releases/download/v0.3.0-alpha1/ble-0.3.0-alpha1.tar.xz
$ tar xJf ble-0.3.0-alpha1.tar.xz
$ source ble-0.3.0-alpha1/ble.sh
curl
を使う場合:
$ curl -LO https://github.com/akinomyoga/ble.sh/releases/download/v0.3.0-alpha1/ble-0.3.0-alpha1.tar.xz
$ tar xJf ble-0.3.0-alpha1.tar.xz
$ source ble-0.3.0-alpha1/ble.sh
指定したディレクトリに ble.sh
を配置するには単に ble-0.1.7
ディレクトリをコピーします。
$ cp -r ble-0.3.0-alpha1 /path/to/blesh
.bashrc
の設定
対話シェルで常用する場合には .bashrc
に設定を行います。以下の様にコードを追加して下さい。
# bashrc
# .bashrc の先頭近くに以下を追加して下さい。
[[ $- == *i* ]] && source /path/to/blesh/ble.sh --noattach
# 間に通常の bashrc の内容を既述します。
# .bashrc の末端近くに以下を追加して下さい。
((_ble_bash)) && ble-attach
アップデート
ble-0.3
以上をお使いの場合は ble.sh
をロードした状態で ble-update
を実行して下さい。
$ ble-update
それ以外の場合には、以下のように git pull
で最新版を入手・インストールできます。
cd ble.sh # ※既に持っている git リポジトリに入る
git pull
make
make INSDIR="$HOME/.local/share/blesh" install
ユーザー設定は Bash スクリプト ~/.blerc
に記述します。ble.sh
をロードする際に自動で source
されます。
他のファイルに設定を記述する時は source ble.sh
の際にオプション --rcfile INITFILE
を指定します。
Vim モード
Vim モードについては Wiki の説明ページ を御覧ください。
曖昧文字幅
設定 char_width_mode
を用いて、曖昧文字幅を持つ文字 (Unicode 参考特性 East_Asian_Width
が A
(Ambiguous) の文字) の幅を制御できます。
現在は 3 つの選択肢 emacs
, west
, east
, auto
が用意されています。
設定値 emacs
を指定した場合、GNU Emacs における既定の文字幅と同じ物を使います。
設定値 west
を指定した場合、全ての曖昧文字幅を 1 (半角) と解釈します。
設定値 east
を指定した場合、全ての曖昧文字幅を 2 (全角) と解釈します。
設定値 auto
を指定した場合、west
か east
かを端末とのやり取りに基づいて自動判定します。
既定値は auto
です。この設定項目は、利用している端末の振る舞いに応じて適切に設定する必要があります。
例えば west
に設定する場合は以下の様にします:
bleopt char_width_mode='west'
文字コード
設定 input_encoding
は入力の文字コードを制御するのに使います。現在 UTF-8
と C
のみに対応しています。
設定値 C
を指定した場合は、受信したバイト値が直接文字コードであると解釈されます。
既定値は UTF-8
です。C
に設定を変更する場合には以下の様にします:
bleopt input_encoding='C'
ベル
設定 edit_abell
と設定 edit_vbell
は、編集関数 bell
の振る舞いを制御します。
edit_abell
が非空白の文字列の場合、音による通知が有効になります (つまり、制御文字の BEL
(0x07) が stderr
に出力されます)。
edit_vbell
が非空白の文字列の場合、画面での通知が有効になります。既定では音による通知が有効で、画面での通知が無効になっています。
設定 vbell_default_message
は画面での通知で使用するメッセージ文字列を指定します。既定値は ' Wuff, -- Wuff!! '
です。
設定 vbell_duration
は画面での通知を表示する時間の長さを指定します。単位はミリ秒です。既定値は 2000
です。
例えば、画面での通知は以下のように設定・有効化できます:
bleopt edit_vbell=1 vbell_default_message=' BEL ' vbell_duration=3000
もう一つの例として、音による通知は以下の様にして無効化できます。
bleopt edit_abell=
着色の設定
構文に従った着色で使用される、各文法要素の色と属性は ble-color-setface
シェル関数で設定します。
既定の設定は以下のコードに対応します:
ble-color-setface region bg=60,fg=white
ble-color-setface region_target bg=153,fg=black
ble-color-setface disabled fg=242
ble-color-setface overwrite_mode fg=black,bg=51
ble-color-setface syntax_default none
ble-color-setface syntax_command fg=brown
ble-color-setface syntax_quoted fg=green
ble-color-setface syntax_quotation fg=green,bold
ble-color-setface syntax_expr fg=26
ble-color-setface syntax_error bg=203,fg=231
ble-color-setface syntax_varname fg=202
ble-color-setface syntax_delimiter bold
ble-color-setface syntax_param_expansion fg=purple
ble-color-setface syntax_history_expansion bg=94,fg=231
ble-color-setface syntax_function_name fg=92,bold
ble-color-setface syntax_comment fg=242
ble-color-setface syntax_glob fg=198,bold
ble-color-setface syntax_brace fg=37,bold
ble-color-setface syntax_tilde fg=navy,bold
ble-color-setface syntax_document fg=94
ble-color-setface syntax_document_begin fg=94,bold
ble-color-setface command_builtin_dot fg=red,bold
ble-color-setface command_builtin fg=red
ble-color-setface command_alias fg=teal
ble-color-setface command_function fg=92
ble-color-setface command_file fg=green
ble-color-setface command_keyword fg=blue
ble-color-setface command_jobs fg=red
ble-color-setface command_directory fg=26,underline
ble-color-setface filename_directory underline,fg=26
ble-color-defface filename_directory_sticky underline,fg=white,bg=26
ble-color-setface filename_link underline,fg=teal
ble-color-defface filename_orphan underline,fg=teal,bg=224
ble-color-setface filename_executable underline,fg=green
ble-color-defface filename_setuid underline,fg=black,bg=220
ble-color-defface filename_setgid underline,fg=black,bg=191
ble-color-setface filename_other underline
ble-color-setface filename_socket underline,fg=cyan,bg=black
ble-color-setface filename_pipe underline,fg=lime,bg=black
ble-color-setface filename_character underline,fg=white,bg=black
ble-color-setface filename_block underline,fg=yellow,bg=black
ble-color-setface filename_warning underline,fg=red
ble-color-defface filename_ls_colors underline
色コードはシェル関数 ble-color-show
(ble.sh
内で定義) で確認できます。
$ ble-color-show
キーバインディング
キーバインディングはシェル関数 ble-bind
を使って変更できます。
例えば C-x h を入力した時に "Hello, world!" と挿入させたければ以下のようにします。
ble-bind -f 'C-x h' 'insert-string "Hello, world!"'
M-c を入力した時にコマンドを実行するには以下のようにします。
ble-bind -c 'M-c' 'my-command'
C-r を入力した時に、ユーザー定義編集関数 (Bash の bind -x
で指定するのと同様の物) を実行するには以下のようにします。
ble-bind -x 'C-r' 'my-edit-function'
既存のキーバインディングは以下のコマンドで確認できます。
$ ble-bind -P
以下のコマンドでキーバインディングに使える編集関数一覧を確認できます。
$ ble-bind -L
複数行モード
コマンドラインに改行が含まれている場合、複数行モード (MULTILINE モード) になります。
C-v C-j または C-q C-j とすると改行をコマンドラインの一部として入力できます。 複数行モードでは、RET (C-m) はコマンドの実行ではなく新しい改行の挿入になります。 複数行モードでは、C-j を用いてコマンドを実行して下さい。
shopt -s cmdhist
が設定されているとき (既定)、もし RET (C-m) を押した時にコマンドラインが構文的に閉じていなければ、コマンドの実行ではなく改行の挿入を行います。
Vim モード
.bashrc
に set -o vi
が設定されているとき、または .inputrc
に set editing-mode vi
が設定されているとき、vim モードが有効になります。
Vim モードの詳細な設定については Wiki のページ (英語) を御覧ください。
自動補完
Bash 4.0 以降では自動補完が有効になり、予測候補が表示されます。 候補を確定するには S-RET を入力します (編集文字列の末尾にいる時は right, C-f または end でも確定できます)。 表示されている候補の初めの単語だけ部分的に確定する時は M-f または M-right を入力します。 現在の候補で確定しそのままコマンドを実行する場合には C-RET (※お使いの端末が対応している時) を入力します。
静的略語展開
特定の単語を静的略語展開に登録することで好きな文字列に展開することができます。
登録済み単語に一致する単語の直後で SP を入力した時に静的略語展開が起きます。
例えば、以下の設定をしておくと command L
まで入力した状態で SP を押した時に、コマンドラインが command | less
に展開されます。
# bashrc (after source ble.sh)
ble-sabbrev L='| less'
- @cmplstofB さまには vi モードの実装のテストをしていただき、またさまざまの提案を頂きました。