DEV Community

vast cow
vast cow

Posted on

Alt-g and Readline shortcuts that make Bash completion a bit more convenient

Let's say you're using Bash and you have a directory like this:

$ ls
aaa-bbb-ccc
Enter fullscreen mode Exit fullscreen mode

You want to move to this directory, but you only remember bbb in the middle of the name, not the leading aaa.

You might be tempted to do:

cd bbb<TAB>
Enter fullscreen mode Exit fullscreen mode

But normal Bash TAB completion is basically prefix-based, so this won't complete aaa-bbb-ccc.

That's where glob comes in.

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

Since * matches any string, aaa-bbb-ccc matches.

However, here you might want a bit more:

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

At the point you've typed this, you want it to expand to:

cd aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

so you can confirm before executing.

That's where Alt-g comes in handy.

Alt-g: Complete using glob

Readline, which handles Bash's command-line editing, has a feature called glob-complete-word.

It's assigned to Alt-g by default, so if you type:

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

and press Alt-g, it can complete using names that match the glob.

For example:

$ cd *bbb*<Alt-g>
Enter fullscreen mode Exit fullscreen mode

becomes:

$ cd aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

This is quite handy when you only remember part of a name.

If you use it alongside normal TAB completion:

cd aaa<TAB>       # when you remember the beginning
cd *bbb*<Alt-g>   # when you only remember the middle
Enter fullscreen mode Exit fullscreen mode

Ctrl-x *: Expand glob in place

Something worth remembering alongside Alt-g is:

Ctrl-x *
Enter fullscreen mode Exit fullscreen mode

This is glob-expand-word, which expands the glob pattern itself into the matched filenames.

For example:

$ echo *bbb*
Enter fullscreen mode Exit fullscreen mode

If you press Ctrl-x *, it becomes:

$ echo aaa-bbb-ccc
Enter fullscreen mode Exit fullscreen mode

If multiple names match, they all get expanded together.

A good way to think about it:

Alt-g      → "complete" using glob
Ctrl-x *   → "expand" using glob
Enter fullscreen mode Exit fullscreen mode

Alt-.: Reuse the last argument of the previous command

One shortcut I personally recommend remembering if you use Bash is Alt-..

For example:

mkdir /tmp/very-long-directory-name
Enter fullscreen mode Exit fullscreen mode

Immediately after running that, if you do:

cd <Alt-.>
Enter fullscreen mode Exit fullscreen mode

You get:

cd /tmp/very-long-directory-name
Enter fullscreen mode Exit fullscreen mode

In other words, it inserts the last argument of the previous command.

This also works for creating a file and then immediately opening it:

touch some-very-long-filename.txt
vim <Alt-.>
Enter fullscreen mode Exit fullscreen mode

If you press Alt-. repeatedly, you can go further back through the last arguments of older commands.

Ctrl-r: Search command history

Many people probably already know this one, but Ctrl-r is also very useful.

Pressing it lets you incrementally search through past commands.

For example, if you previously ran:

docker compose exec app bundle exec rails console
Enter fullscreen mode Exit fullscreen mode

and want to run it again, you don't need to scroll endlessly through history with the arrow keys.

Just press Ctrl-r and type:

rails
Enter fullscreen mode Exit fullscreen mode

and it will find history entries containing that.

This is essential if you frequently reuse long commands.

Ctrl-x Ctrl-e: Edit the current command in an editor

When commands get long, editing on a single line in the shell can become painful.

In that case, press:

Ctrl-x Ctrl-e
Enter fullscreen mode Exit fullscreen mode

This lets you edit the command you're currently typing in $EDITOR.

For example, if you're in the middle of building a long command like:

find . -type f -name '*.log' -mtime +7 ...
Enter fullscreen mode Exit fullscreen mode

you can press:

Ctrl-x Ctrl-e
Enter fullscreen mode Exit fullscreen mode

and edit it in your configured editor such as vim or nano.

When you save and exit, the content is returned to the shell and executed.

This is very handy for complex one-liners.

Other useful Readline operations to remember

Bash uses Emacs-style Readline keybindings by default.

Here's a summary of commonly used ones:

Key Action
Alt-g Complete using a glob pattern
Ctrl-x * Expand glob into actual filenames
Alt-* Insert all completion candidates
Alt-. Insert last argument from previous command
Ctrl-r Search command history
Ctrl-x Ctrl-e Edit current command in an editor
Ctrl-a Move to beginning of line
Ctrl-e Move to end of line
Alt-b Move back one word
Alt-f Move forward one word
Ctrl-w Delete word before cursor
Alt-d Delete word after cursor
Ctrl-u Delete before cursor
Ctrl-k Delete after cursor
Ctrl-_ Undo
Alt-# Comment out current command and add to history

You don't need to memorize all of them.

Just remembering:

Alt-g
Alt-.
Ctrl-r
Ctrl-x Ctrl-e
Ctrl-x *
Enter fullscreen mode Exit fullscreen mode

is enough to make Bash operations considerably easier.

You can also assign TAB to glob completion

By the way, if you change the Readline settings, you can assign glob-complete-word (normally on Alt-g) to TAB.

In ~/.inputrc, add:

"\t": glob-complete-word
Enter fullscreen mode Exit fullscreen mode

Then you can do:

cd *bbb*<TAB>
Enter fullscreen mode Exit fullscreen mode

However, this replaces normal TAB completion.

Since it also affects everyday operations like:

cd aaa<TAB>
Enter fullscreen mode Exit fullscreen mode

I personally prefer keeping TAB as normal completion and using:

normal completion → TAB
glob completion   → Alt-g
Enter fullscreen mode Exit fullscreen mode

as a division of roles, which I find easier to handle.

Summary

The discovery here is simple:

For a directory like:

aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

if you type:

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

and press:

Alt-g
Enter fullscreen mode Exit fullscreen mode

you get glob-based completion.

Bash has quite a few Readline features that you won't easily notice if you only ever use TAB and arrow keys.

In particular, these five:

Alt-g          glob completion
Ctrl-x *       glob expansion
Alt-.          reuse previous argument
Ctrl-r         history search
Ctrl-x Ctrl-e  edit in editor
Enter fullscreen mode Exit fullscreen mode

are worth knowing and will help in everyday shell operations.

If you want to speed up your Bash workflow, beyond just adding commands and tools, looking into Readline keybindings is also recommended.

Bashの補完がちょっと便利になるAlt-gとReadlineショートカット

Bashを使っていて、こんなディレクトリがあるとします。

$ ls
aaa-bbb-ccc
Enter fullscreen mode Exit fullscreen mode

このディレクトリに移動したいけれど、名前の先頭の aaa ではなく、途中の bbb だけ覚えている。

つい、

cd bbb<TAB>
Enter fullscreen mode Exit fullscreen mode

とやりたくなりますが、通常のBashのTAB補完は基本的に前方一致なので、これでは aaa-bbb-ccc は補完されません。

そこで使えるのがglobです。

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

* は任意の文字列にマッチするので、aaa-bbb-ccc が該当します。

ただ、ここでもう一つ欲が出ます。

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

と入力した時点で、

cd aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

に展開して確認してから実行したい。

そんなときに便利なのが Alt-g です。

Alt-g:globを使って補完する

Bashのコマンドライン編集を担当しているReadlineには、glob-complete-word という機能があります。

デフォルトでは Alt-g に割り当てられているので、

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

まで入力して Alt-g を押すと、globにマッチする名前を使って補完できます。

たとえば、

$ cd *bbb*<Alt-g>
Enter fullscreen mode Exit fullscreen mode

から、

$ cd aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

という具合です。

「名前の一部しか覚えていない」という場面でかなり便利です。

通常のTAB補完と使い分けると、

cd aaa<TAB>       # 先頭を覚えている
cd *bbb*<Alt-g>   # 途中だけ覚えている
Enter fullscreen mode Exit fullscreen mode

という操作ができます。

Ctrl-x *:globをその場で展開する

Alt-g と一緒に覚えておきたいのが、

Ctrl-x *
Enter fullscreen mode Exit fullscreen mode

です。

こちらは glob-expand-word で、globパターンそのものをマッチしたファイル名に展開します。

たとえば、

$ echo *bbb*
Enter fullscreen mode Exit fullscreen mode

と入力して Ctrl-x * を押すと、

$ echo aaa-bbb-ccc
Enter fullscreen mode Exit fullscreen mode

のようになります。

複数マッチする場合には、それらがまとめて展開されます。

感覚的には、

Alt-g      → globを使って「補完」
Ctrl-x *   → globを使って「展開」
Enter fullscreen mode Exit fullscreen mode

と覚えておくと分かりやすいです。

Alt-.:直前のコマンドの最後の引数を再利用する

個人的にBashを使うなら覚えておきたいショートカットの一つが Alt-. です。

たとえば、

mkdir /tmp/very-long-directory-name
Enter fullscreen mode Exit fullscreen mode

を実行した直後に、

cd <Alt-.>
Enter fullscreen mode Exit fullscreen mode

とすると、

cd /tmp/very-long-directory-name
Enter fullscreen mode Exit fullscreen mode

になります。

つまり、直前のコマンドの最後の引数を挿入してくれます。

ファイルを作ってすぐ開く場合などにも使えます。

touch some-very-long-filename.txt
vim <Alt-.>
Enter fullscreen mode Exit fullscreen mode

Alt-. を繰り返し押せば、さらに古いコマンドの末尾引数を遡ることもできます。

Ctrl-r:コマンド履歴を検索する

これは知っている人も多いと思いますが、Ctrl-r も非常に便利です。

押すと過去のコマンドをインクリメンタル検索できます。

たとえば以前実行した、

docker compose exec app bundle exec rails console
Enter fullscreen mode Exit fullscreen mode

をもう一度使いたい場合、矢印キーで延々と履歴を遡る必要はありません。

Ctrl-r を押して、

rails
Enter fullscreen mode Exit fullscreen mode

などと入力すれば、それを含む履歴を探せます。

長いコマンドを頻繁に再利用するなら必須級です。

Ctrl-x Ctrl-e:入力中のコマンドをエディタで編集する

コマンドが長くなってくると、シェルの1行編集ではつらくなることがあります。

そんなときは、

Ctrl-x Ctrl-e
Enter fullscreen mode Exit fullscreen mode

を押します。

すると現在入力しているコマンドを $EDITOR で編集できます。

たとえば、

find . -type f -name '*.log' -mtime +7 ...
Enter fullscreen mode Exit fullscreen mode

のような長いコマンドを組み立てている途中で、

Ctrl-x Ctrl-e
Enter fullscreen mode Exit fullscreen mode

を押せば、設定されている vimnano などのエディタで編集できます。

保存して終了すると、その内容がシェルに戻されて実行されます。

複雑なワンライナーではかなり便利です。

その他、覚えておくと便利なReadline操作

BashではEmacs風のReadlineキーバインドがデフォルトで使われています。

よく使うものをまとめると次の通りです。

キー 動作
Alt-g globパターンを使って補完
Ctrl-x * globを実際のファイル名に展開
Alt-* 補完候補をまとめて挿入
Alt-. 過去のコマンドの最後の引数を挿入
Ctrl-r コマンド履歴を検索
Ctrl-x Ctrl-e 現在のコマンドをエディタで編集
Ctrl-a 行頭へ移動
Ctrl-e 行末へ移動
Alt-b 1単語戻る
Alt-f 1単語進む
Ctrl-w カーソルより前の単語を削除
Alt-d カーソルより後ろの単語を削除
Ctrl-u カーソルより前を削除
Ctrl-k カーソルより後ろを削除
Ctrl-_ Undo
Alt-# 入力中のコマンドをコメント化して履歴へ入れる

全部覚える必要はありません。

まずは、

Alt-g
Alt-.
Ctrl-r
Ctrl-x Ctrl-e
Ctrl-x *
Enter fullscreen mode Exit fullscreen mode

あたりを覚えておくだけでも、Bashでの操作がかなり楽になります。

TABをglob補完にすることもできる

ちなみにReadlineの設定を変更すれば、Alt-gglob-complete-word をTABに割り当てることもできます。

~/.inputrc に、

"\t": glob-complete-word
Enter fullscreen mode Exit fullscreen mode

と設定する方法です。

すると、

cd *bbb*<TAB>
Enter fullscreen mode Exit fullscreen mode

という操作が可能になります。

ただし、これは通常のTAB補完を置き換えてしまいます。

普段の、

cd aaa<TAB>
Enter fullscreen mode Exit fullscreen mode

という操作にも影響するため、個人的にはTABは通常補完のまま残し、

通常の補完 → TAB
glob補完    → Alt-g
Enter fullscreen mode Exit fullscreen mode

と使い分ける方が扱いやすいと思います。

まとめ

今回の発見は単純で、

aaa-bbb-ccc/
Enter fullscreen mode Exit fullscreen mode

というディレクトリに対して、

cd *bbb*
Enter fullscreen mode Exit fullscreen mode

まで入力したら、

Alt-g
Enter fullscreen mode Exit fullscreen mode

を押せばglobを利用した補完ができる、というものです。

Bashには、普段TABと矢印キーだけで操作しているとなかなか気づかないReadlineの機能がかなりあります。

特に、

Alt-g          glob補完
Ctrl-x *       glob展開
Alt-.          前の引数を再利用
Ctrl-r         履歴検索
Ctrl-x Ctrl-e  エディタで編集
Enter fullscreen mode Exit fullscreen mode

の5つは、知っておくと日常的なシェル操作で役立ちます。

Bashの操作を速くしたい場合、コマンドやツールを追加するだけでなく、Readlineのキーバインドを調べてみるのもおすすめです。

Top comments (0)