diff --git a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs index 514eed8ed7c..fbd0c867c57 100644 --- a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs +++ b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs @@ -92,7 +92,12 @@ public class PSConsoleReadlineOptions public const ConsoleColor DefaultEmphasisForegroundColor = ConsoleColor.Cyan; public const ConsoleColor DefaultErrorForegroundColor = ConsoleColor.Red; - public const EditMode DefaultEditMode = EditMode.Windows; + public const EditMode DefaultEditMode = +#if LINUX + EditMode.Emacs; +#else + EditMode.Windows; +#endif public const string DefaultContinuationPrompt = ">> "; diff --git a/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs b/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs index aae97e503ea..b543aef8a16 100644 --- a/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs +++ b/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs @@ -114,6 +114,25 @@ static KeyHandler MakeKeyHandler(Action action, string private Dictionary _dispatchTable; private Dictionary> _chordDispatchTable; + /// + /// Helper to set bindings based on EditMode + /// + void SetDefaultBindings(EditMode editMode) + { + switch (editMode) + { + case EditMode.Emacs: + SetDefaultEmacsBindings(); + break; + case EditMode.Vi: + SetDefaultViBindings(); + break; + case EditMode.Windows: + SetDefaultWindowsBindings(); + break; + } + } + void SetDefaultWindowsBindings() { _dispatchTable = new Dictionary(new ConsoleKeyInfoComparer()) diff --git a/src/Microsoft.PowerShell.PSReadLine/Options.cs b/src/Microsoft.PowerShell.PSReadLine/Options.cs index d8162b743fd..b927282e277 100644 --- a/src/Microsoft.PowerShell.PSReadLine/Options.cs +++ b/src/Microsoft.PowerShell.PSReadLine/Options.cs @@ -96,18 +96,7 @@ private void SetOptionsInternal(SetPSReadlineOption options) // Switching/resetting modes - clear out chord dispatch table _chordDispatchTable.Clear(); - switch (options._editMode) - { - case EditMode.Emacs: - SetDefaultEmacsBindings(); - break; - case EditMode.Vi: - SetDefaultViBindings(); - break; - case EditMode.Windows: - SetDefaultWindowsBindings(); - break; - } + SetDefaultBindings(Options.EditMode); } if (options._showToolTips.HasValue) { diff --git a/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs b/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs index c2848f05463..75654e21d1a 100644 --- a/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs +++ b/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs @@ -485,8 +485,6 @@ private PSConsoleReadLine() _console = new ConhostConsole(); #endif - SetDefaultWindowsBindings(); - _buffer = new StringBuilder(8 * 1024); _statusBuffer = new StringBuilder(256); _savedCurrentLine = new HistoryItem(); @@ -516,6 +514,7 @@ private PSConsoleReadLine() hostName = "PSReadline"; } _options = new PSConsoleReadlineOptions(hostName); + SetDefaultBindings(_options.EditMode); } private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics) diff --git a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 index 7821fca3ee9..0173b91695a 100644 --- a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 +++ b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 @@ -13,6 +13,16 @@ Describe "PSReadLine" { $module.Version | Should Be "1.2" } + It "Should use Emacs Bindings on Linux and OS X" -skip:$IsWindows { + (Get-PSReadLineOption).EditMode | Should Be Emacs + (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be BeginningOfLine + } + + It "Should use Windows Bindings on Windows" -skip:(-not $IsWindows) { + (Get-PSReadLineOption).EditMode | Should Be Windows + (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be SelectAll + } + It "Should set the edit mode" { Set-PSReadlineOption -EditMode Windows (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be SelectAll