2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでも使える履歴検索ショートカット:`Ctrl + R` で過去コマンドを一瞬で再利用

Posted at

概要

コマンドラインで作業していると、「あのコマンド、もう一回使いたい」と感じることは多い。
そんなときに便利なのが、履歴検索ショートカット Ctrl + R

過去に入力したコマンドをキーワードでインクリメンタル検索し、即座に再実行可能。
Linuxのbash/zshだけでなく、WindowsのPowerShellでも使えることをご存じだろうか?


対象環境

OS   : Windows 10 / 11  
Shell: PowerShell 5.x / 7.x(標準プロンプト / Windows Terminal)  

基本操作:Ctrl + R

PowerShell上で以下のキー操作を実行:

Ctrl + R

すると、以下のようなプロンプトに切り替わる:

(reverse-i-search)`': _

ここでキーワードを入力すると、履歴から該当コマンドを順次表示してくれる。
Enterでそのまま実行、矢印キーで微調整も可能。


使用例

たとえば以前に以下のようなコマンドを実行していたとする:

Get-Content .\access.log -Tail 50 -Wait

再度使いたい場合は、Ctrl + R を押してから access と入力すれば一瞬で見つかる。

(reverse-i-search)`access': Get-Content .\access.log -Tail 50 -Wait

応用:複数候補の中から選ぶ

キーワードが複数マッチする場合は、Ctrl + R連打して、履歴の古い順に遡れる。

(reverse-i-search)`npm': npm run dev
(reverse-i-search)`npm': npm install
(reverse-i-search)`npm': npm ci

結語

Ctrl + R は、CLI操作を劇的に高速化する最短ルートだ。
PowerShellにおいても問題なく使えるため、GUIで履歴を探すより圧倒的に効率的。

毎回似たようなコマンドを打ち直しているなら、それはもう「時間の浪費」。
指先一つで履歴を呼び出せるこのショートカットは、CLIユーザーにとって最強の武器になる。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?