Skip to content

Add ctrl+enter key binding for Linux - #3983

Open
Jordan Borean (jborean93) wants to merge 1 commit into
PowerShell:masterfrom
jborean93:linux-paste
Open

Add ctrl+enter key binding for Linux#3983
Jordan Borean (jborean93) wants to merge 1 commit into
PowerShell:masterfrom
jborean93:linux-paste

Conversation

@jborean93

@jborean93 Jordan Borean (jborean93) commented Apr 2, 2024

Copy link
Copy Markdown

PR Summary

Adds the Ctrl+Enter key binding for Linux that defaults to AcceptLine. This allows PSReadLine to work by default on Linux terminals like Alacritty (in tmux) where a newline in a paste comes across as the Ctrl+Enter key. Not all terminals on Linux will send a newline in a paste as this key but some do. Having this key handler means that pasting a string with newlines is preserved properly rather than as

You can replicate how a newline was handled in a tty with a .NET application by running the following Python code on Linux

import os
import pty
import subprocess
import sys

parent, child = pty.openpty()
p = subprocess.Popen(
    ['pwsh', '-Command', '[Console]::ReadKey($true) | ConvertTo-Json -EnumsAsString'],
    preexec_fn=os.setsid,
    stdin=child,
    stdout=sys.stdout,
    stderr=sys.stderr,
)
stdin = os.fdopen(parent, 'wb', 0)
os.close(child)

stdin.write(b"\n")
p.communicate()

The output on PowerShell 7.4/.NET 8 is

{
  "KeyChar": "\n",
  "Key": "Enter",
  "Modifiers": "Control"
}

.NET introduced a rewrite of Console.ReadKey() in .NET 7 dotnet/runtime#72193 that changed how a \n on a tty was handled. In the past this is what it returned

{
  "KeyChar": "\r",
  "Key": "Enter",
  "Modifiers": 0
}

As there were no modifiers the \r was treated as a newline and so just worked.

I'm unsure how you wish for me to test this, any guidance there would be appreciated.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure you've added one or more new tests
  • Make sure you've tested these changes in terminals that PowerShell is commonly used in (i.e. conhost.exe, Windows Terminal, Visual Studio Code Integrated Terminal, etc.)
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed at PowerShell-Docs
      • Doc Issue filed:
Microsoft Reviewers: Open in CodeFlow
Adds the Ctrl+Enter key binding for Linux that defaults to AcceptLine.
This allows PSReadLine to work by default on Linux terminals like
Alacritty where a newline in a paste comes across as the Ctrl+Enter key.
@Manbearpiet

Christian Piet (Manbearpiet) commented Sep 26, 2025

Copy link
Copy Markdown

Finally, I was looking for a fix for months. Added to my profile and haven't looked back.
Works like a charm using tmux and pwsh in Ghostty, excellent! 👌🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants