Skip to content

Commit 81a1574

Browse files
committed
Update readme, add a new example
1 parent 973eb11 commit 81a1574

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ It provides:
1313
* Bash style completion (optional in Cmd mode, default in Emacs mode)
1414
* Emacs yank/kill ring
1515
* PowerShell token based "word" movement and kill
16+
* Undo/redo
1617

1718
Many planned features are not yet implemented, but in it's current state, the module is very usable.
1819

@@ -55,11 +56,43 @@ To enable bash style completion without using Emacs mode, you can use:
5556
Set-PSReadlineKeyHandler -Key Tab -Function Complete
5657
```
5758

59+
Here is a more interesting example of what is possible:
60+
61+
```powershell
62+
Set-PSReadlineKeyHandler -Chord 'Oem7','Shift+Oem7' `
63+
-BriefDescription SmartInsertQuote `
64+
-LongDescription "Insert paired quotes if not already on a quote" `
65+
-ScriptBlock {
66+
param($key, $arg)
67+
68+
$line = $null
69+
$cursor = $null
70+
[PSConsoleUtilities.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)
71+
72+
if ($line[$cursor] -eq $key.KeyChar) {
73+
# Just move the cursor
74+
[PSConsoleUtilities.PSConsoleReadline]::SetCursorPosition($cursor + 1)
75+
}
76+
else {
77+
# Insert matching quotes, move cursor to be in between the quotes
78+
[PSConsoleUtilities.PSConsoleReadline]::Insert("$($key.KeyChar)" * 2)
79+
[PSConsoleUtilities.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)
80+
[PSConsoleUtilities.PSConsoleReadline]::SetCursorPosition($cursor - 1)
81+
}
82+
}
83+
```
84+
85+
In this example, when you type a single quote or double quote, there are two things that can happen. If the character following the cursor is not the quote typed, then a matched pair of quotes is inserted and the cursor is placed inside the the matched quotes. If the character following the cursor is the quote typed, the cursor is simply moved past the quote without inserting anything. If you use Resharper or another smart editor, this experience will be familiar.
86+
87+
Note that with the handler written this way, it correctly handles Undo - both quotes will be undone with one undo.
88+
5889
See the public methods of [PSConsoleUtilities.PSConsoleReadLine] to see what other built-in functionality you can modify.
5990

6091
If you want to change the command line in some unimplmented way in your custom key binding, you can use the methods:
6192

6293
```powershell
6394
[PSConsoleUtilities.PSConsoleReadLine]::GetBufferState
64-
[PSConsoleUtilities.PSConsoleReadLine]::SetBufferState
95+
[PSConsoleUtilities.PSConsoleReadLine]::Insert
96+
[PSConsoleUtilities.PSConsoleReadLine]::Replace
97+
[PSConsoleUtilities.PSConsoleReadLine]::SetCursorPosition
6598
```

0 commit comments

Comments
 (0)