7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashで実行したコマンドを再度実行する

Last updated at Posted at 2013-11-28

直前に実行したコマンドを実行する

Terminal
$ ls
$ !!
ls

!!で直前に実行したコマンドを実行できます。

番号を指定して実行する

historyコマンドを使うと番号と共に実行したコマンドが表示されます。

Terminal
$ history | tail -n 5
1 ls
2 ls
3 ls
4 ls
5 history | tail -n 5

で、!に続いてこの番号を入れて実行してあげると……

Terminal
$ !5
history | tail -n 5
2 ls
3 ls
4 ls
5 history | tail -n 5
6 history | tail -n 5

再度同じコマンドが実行されます。

履歴から検索する

Ctrl-rを押すと……

Terminal
(reverse-i-search)`': 

と、入力待ちになるので、適当に入力すると履歴からインクリメンタルサーチをしてくれます。以下はhiと入力したところ。

Terminal
(reverse-i-search)`hi': history | tail -n 5

これでEnterを押すとコロン以降に表示されているコマンドが実行されます。

Ctrl-rだと後ろから検索するので最近実行したコマンドから検索されるのですが、C-sだと検索が逆になります。
が、sttyのC-sとキーが被っているので

.bashrc
stty stop undef

で無効化しておかないと使用できません。ついでに、これをしないでC-sを押すと画面への描画が止まって固まったように見えます。C-qで描画を再開できます。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?