@@ -483,56 +483,143 @@ LONG DESCRIPTION
483
483
484
484
Scroll the display to the cursor.
485
485
486
- Custom Key Binding Support
487
- --------------------------
486
+ Custom Key Bindings
487
+ -------------------
488
+
489
+ PSReadline supports custom key bindings using the cmdlet Set-PSReadlineKeyHandler. Most
490
+ custom key bindings will call one of the above functions, for example:
491
+
492
+ Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
493
+
494
+ You can bind a ScriptBlock to a key. The ScriptBlock can do pretty much anything you want.
495
+ Some useful examples include:
496
+
497
+ * edit the command line
498
+ * opening a new window (e.g. help)
499
+ * change directories without changing the command line
500
+
501
+ The ScriptBlock is passed two arguments:
502
+
503
+ * $key - A [ConsoleKeyInfo] that is the key that triggered the custom binding. If you bind
504
+ the same ScriptBlock to multiple keys and need to perform different actions depending
505
+ on the key, you can check $key. Many custom bindings ignore this argument.
506
+ * $arg - An arbitrary argument. Most often, this would be an integer argument that the user
507
+ passes from the key bindings DigitArgument. If your binding doesn't accept arguments,
508
+ it's reasonable to ignore this argument.
509
+
510
+ Let's take a look at an example that adds a command line to history without executing it. This is
511
+ useful when you realize you forgot to do something, but don't want to re-enter the command line
512
+ you've already entered.
513
+
514
+ Set-PSReadlineKeyHandler -Key Alt+w `
515
+ -BriefDescription SaveInHistory `
516
+ -LongDescription "Save current line in history but do not execute" `
517
+ -ScriptBlock {
518
+ param($key, $arg) # The arguments are ignored in this example
519
+
520
+ # We need the command line, GetBufferState gives us that (with the cursor position)
521
+ $line = $null
522
+ $cursor = $null
523
+ [PSConsoleUtilities.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
524
+
525
+ # AddToHistory saves the line in history, but does not execute the line.
526
+ [PSConsoleUtilities.PSConsoleReadLine]::AddToHistory($line)
527
+
528
+ # RevertLine is like pressing Escape.
529
+ [PSConsoleUtilities.PSConsoleReadLine]::RevertLine()
530
+ }
531
+
532
+ You can see many more examples in the file SamplePSReadlineProfile.ps1 which is installed in the
533
+ PSReadline module folder.
534
+
535
+ Most key bindings will want to take advantage of some helper functions for editing the command
536
+ line those APIs are documented in the next section.
537
+
538
+ Custom Key Binding Support APIs
539
+ -------------------------------
540
+
541
+ The following functions are public in PSConsoleUtilities.PSConsoleReadline, but cannot be directly
542
+ bound to a key. Most are useful in custom key bindings.
543
+
544
+ void AddToHistory(string command)
545
+
546
+ Add a command line to history without executing it.
488
547
489
- The following functions are public but cannot be directly bound to a key. They are used
490
- in custom key bindings.
548
+ void ClearHistory()
491
549
492
- Ding
550
+ Clear the history.
551
+
552
+ void ClearKillRing()
553
+
554
+ Clear the kill ring. This is mostly used for testing.
555
+
556
+ void Delete(int start, int length)
557
+
558
+ Delete length characters from start. This operation supports undo/redo.
559
+
560
+ void Ding()
493
561
494
562
Perform the Ding action based on the users perference.
495
563
496
- GetBufferState(out string input, out int cursor)
497
- GetBufferState(out Ast ast, out Token[] tokens, out ParseError[] parseErrors, out int cursor)
564
+ void GetBufferState([ref] string input, [ref] int cursor)
565
+ void GetBufferState([ref] Ast ast, [ref] Token[] tokens, [ref] ParseError[] parseErrors, [ref] int cursor)
498
566
499
567
These two functions retrieve useful information about the current state of
500
568
the input buffer. The first is more commonly used for simple cases. The
501
569
second is used if your binding is doing something more advanced with the Ast.
502
570
503
- Insert(char c)
504
- Insert(string s)
571
+ IEnumerable[PSConsoleUtilities.KeyHandler] GetKeyHandlers(bool includeBound, bool includeUnbound)
572
+
573
+ This function is used by Get-PSReadlineKeyHandler and probably isn't useful in a custom
574
+ key binding.
575
+
576
+ PSConsoleUtilities.PSConsoleReadlineOptions GetOptions()
577
+
578
+ This function is used by Get-PSReadlineOption and probably isn't too useful in a custom
579
+ key binding.
580
+
581
+ void GetSelectionState([ref] int start, [ref] int length)
582
+
583
+ If there is no selection on the command line, -1 will be returned in both start and length.
584
+ If there is a selection on the command line, the start and length of the selection are returned.
585
+
586
+ void Insert(char c)
587
+ void Insert(string s)
505
588
506
- Insert a character or string in a way that supports undo/redo.
589
+ Insert a character or string at the cursor. This operation supports undo/redo.
507
590
508
- Replace(int start, int length, string replacement)
591
+ string ReadLine(runspace remoteRunspace, System.Management.Automation.EngineIntrinsics engineIntrinsics)
592
+
593
+ This is the main entrypoint to PSReadline. It does not support recursion, so is not useful
594
+ in a custom key binding.
595
+
596
+ void RemoveKeyHandler(string[] key)
597
+
598
+ This function is used by Remove-PSReadlineKeyHandler and probably isn't too useful in a
599
+ custom key binding.
600
+
601
+ void Replace(int start, int length, string replacement)
509
602
510
- Replace some of the input in a way that supports undo/redo as a single action.
511
- This is preferred over Delete followed by Insert to better support Undo.
603
+ Replace some of the input. This operation supports undo/redo.
604
+ This is preferred over Delete followed by Insert because it is treated as a single action
605
+ for undo.
512
606
513
- SetCursorPosition(int offset )
607
+ void SetCursorPosition(int cursor )
514
608
515
609
Move the cursor to the given offset. Cursor movement is not tracked for undo.
516
610
517
- Custom Key Bindings
518
- -------------------
519
-
520
- PSReadline supports custom key bindings using the cmdlet Set-PSReadlineKeyHandler. Most
521
- custom key bindings will call one of the above functions, for example:
611
+ void SetOptions(PSConsoleUtilities.SetPSReadlineOption options)
522
612
523
- Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
613
+ This function is a helper method used by the cmdlet Set-PSReadlineOption, but might be
614
+ useful to a custom key binding that wants to temporarily change a setting.
524
615
525
- You can bind a ScriptBlock to a key. This ScriptBlock can do pretty much anything you like.
526
- For example, say you wanted to run a specific command after hitting Ctrl+B, you could do this:
616
+ bool TryGetArgAsInt(System.Object arg, [ref] int numericArg, int defaultNumericArg)
527
617
528
- Set-PSReadlineKeyHandler -Key Ctrl+B -BriefDescription build -LongDescription "Build the current directory" -ScriptBlock {
529
- [PSConsoleUtilities.PSConsoleReadLine]::RevertLine()
530
- [PSConsoleUtilities.PSConsoleReadLine]::Insert("build")
531
- [PSConsoleUtilities.PSConsoleReadLine]::AcceptLine()
532
- }
618
+ This helper method is used for custom bindings that honor DigitArgument. A typical call
619
+ looks like:
533
620
534
- In this example, multiple functions are called and the end result is running the 'build' command
535
- by entering Ctrl+B.
621
+ [int]$numericArg = 0
622
+ [PSConsoleUtilities.PSConsoleReadLine]::TryGetArgAsInt($arg, [ref]$numericArg, 1)
536
623
537
624
POWERSHELL COMPATIBILITY
538
625
0 commit comments