hogashi.*

日記から何から

Bashでスラッシュ単位で削除できるようにするにはunix-filename-ruboutを使う

blog.3qe.us

 Bash で同じことをできないか見たけど、 $WORDCHARS はなかった*1。かわりに、 unix-filename-rubout という readline command があるので、それを好きな keyseq に設定すれば、スラッシュと空白を区切りとして削除ができるようになる*2

bind \\C-n:unix-filename-rubout

 unix-filename-rubout の説明を引用するとこう:

unix-word-rubout (C-w)
Kill the word behind point, using white space as a word boundary. The killed text is saved on the kill-ring.
unix-filename-rubout ()
Kill the word behind point, using white space and the slash character as the word boundaries. The killed text is saved on the kill-ring.
https://www.gnu.org/software/bash/manual/bash.html#Commands-For-Killing

 man bash を見ると色んなコマンドが準備されていることがわかる。 Killing And Yanking が文字の削除とコピペの章で、他にも色々ある。デフォルトでは bind されていないものが多いので、必要に応じて付け替えるとよさそう。 https://www.gnu.org/software/bash/manual/bash.html#Bindable-Readline-Commands

 ちゃんと知らなかったけど、 kill-ring というのが用意されていて、 unix-word-rubout したときなどにはそこに削除されたテキストが入る。最後に入ったものを貼り付けるには C-y の yank を使う。その後に M-y の yank-pop を使うと、 (ring という名前通り) 過去に kill-ring に入れたものを順繰りに貼り付け直すことができる。

3つ unix-filename-rubout してから yank して yank-pop してる様子

 ところで C-w をただ bind で上書きしようとしてもうまくいかない。これは stty というやつの設定があるためらしい。

stackoverflow.com

 見るとたしかに ^Wwerase にあたっている (word erase ということっぽい?)。これを undef に上書きすれば、あとは Bash 側で bind で上書きして動くようになる。

$ stty -a
speed 9600 baud; 32 rows; 150 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
$ stty werase undef
$ stty -a
(略)
        stop = ^S; susp = ^Z; time = 0; werase = <undef>;
$ bind \\C-w:unix-filename-rubout

 stty は man stty によれば「set the options for a terminal device interface」 とのことで、(仮想)端末の入出力の設定ができる、というふんわりした理解だけしている (細かい設定が色々ありそうなんだけどあんまりわかってない)。 stty お前だったのか、いつも C-d で eof を送出してくれていたのは、という感じ。

*1:なので好きな文字を区切りに設定することはできなそう

*2:man bash の bind コマンドのところを参照 https://www.gnu.org/software/bash/manual/bash.html#index-bind