1
3

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 3 years have passed since last update.

PSReadLine

Last updated at Posted at 2021-07-23

powershellでのPSReadLineの使い方

 どうも、defaultで設定されているのだが、けっこうカスタマイズすると面白いのでご参考に

デフォルトの動き

get-psreadlinekeyhandler
とすると、かなりのキーが設定されていることがわかる。ちょっと一部だけを表示するとこんな感じ

Basic editing functions
=======================

Key              Function           Description
---              --------           -----------
Enter            AcceptLine         Accept the input or move to the next line if input is missing a closing token.
Shift+Enter      AddLine            Move the cursor to the next line without attempting to execute the input
Backspace        BackwardDeleteChar Delete the character before the cursor
Ctrl+h           BackwardDeleteChar Delete the character before the cursor
Ctrl+Home        BackwardDeleteLine Delete text from the cursor to the start of the line
Ctrl+Backspace   BackwardKillWord   Move the text from the start of the current or previous word to the cursor to the kill ring
Ctrl+w           BackwardKillWord   Move the text from the start of the current or previous word to the cursor to the kill ring
Ctrl+C           Copy               Copy selected region to the system clipboard.  If no region is selected, copy the whole line
Ctrl+c           CopyOrCancelLine   Either copy selected text to the clipboard, or if no text is selected, cancel editing the line with CancelLine.
Ctrl+x           Cut                Delete selected region placing deleted text in the system clipboard
Delete           DeleteChar         Delete the character under the cursor
Ctrl+End         ForwardDeleteLine  Delete text from the cursor to the end of the line
Ctrl+Enter       InsertLineAbove    Inserts a new empty line above the current line without attempting to execute the input
Shift+Ctrl+Enter InsertLineBelow    Inserts a new empty line below the current line without attempting to execute the input
Alt+d            KillWord           Move the text from the cursor to the end of the current or next word to the kill ring
Ctrl+Delete      KillWord           Move the text from the cursor to the end of the current or next word to the kill ring
Ctrl+v           Paste              Paste text from the system clipboard
Shift+Insert     Paste              Paste text from the system clipboard
Ctrl+y           Redo               Redo an undo
Escape           RevertLine         Equivalent to undo all edits (clears the line except lines imported from history)
Ctrl+z           Undo               Undo a previous edit
Alt+.            YankLastArg        Copy the text of the last argument to the input

このようなキーバインドが設定されていて、homeキーだとコマンドの先頭に。endキーだとコマンドの最後に
そして選択として次の部分が有用ではないかと思う。

Selection functions
===================

Key                   Function            Description
---                   --------            -----------
Ctrl+a                SelectAll           Select the entire line. Moves the cursor to the end of the line
Shift+LeftArrow       SelectBackwardChar  Adjust the current selection to include the previous character
Shift+Home            SelectBackwardsLine Adjust the current selection to include from the cursor to the start of the line
Shift+Ctrl+LeftArrow  SelectBackwardWord  Adjust the current selection to include the previous word
Shift+RightArrow      SelectForwardChar   Adjust the current selection to include the next character
Shift+End             SelectLine          Adjust the current selection to include from the cursor to the end of the line
Shift+Ctrl+RightArrow SelectNextWord      Adjust the current selection to include the next word

そして、このkeybindはwindowsの場合であり、viモードにすると、まったくviコマンドのように使えるというのが面白い。またemacsのバインディングも可能である。
なお、viの場合はwとかdとかで編集できてしまうのだが、このモードに入る場合はescを押せばいいというのがなかなかわからなかった。viモードに変更すると上のkeybindはごっそり変わるので、お気をつけて。

prediction

デフォルトでは設定されていないが

set-psreadlineoption -predictionsource history

とすると、予測で次ぎのコマンドを表示し始める。
予測が合っているのであれば→キーを押せば、確定である。

ただ、この予測を表示する色がもの凄く暗いのでよく見えない。これを設定するのは

set-psreadlineoption -colors @{inlineprediction = "darkgreen"}

とかやればok

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?