Skip to content

Fix middle click paste over text in terminal on Linux - #338580

Open
Dominik Menke (dmke) wants to merge 1 commit into
microsoft:mainfrom
dmke:fix/terminal-middle-click-paste-157233
Open

Dominik Menke (dmke) wants to merge 1 commit into
microsoft:mainfrom
dmke:fix/terminal-middle-click-paste-157233

Conversation

@dmke

@dmke Dominik Menke (dmke) commented Sep 28, 2026 •

Copy link
Copy Markdown

Fixes #157233

Problem

On Linux, middle click pastes the selection clipboard (the X primary selection) into the terminal only when clicking empty terminal space. Clicking on text does nothing. This affects both terminal.integrated.middleClickBehavior settings.

Cause

  • xterm.js activates a link on mouseup of any mouse button, not just the primary one (Inserting X selections requires precise mouse positioning xtermjs/xterm.js#4052).
  • TerminalWordLinkDetector makes every word in the terminal a link, so a middle click on text always lands on a "link".
  • TerminalLinkManager's activation handler calls preventDefault() on the event before it checks the modifier key.
  • Chromium skips its built-in paste of the selection clipboard when the middle-button mouseup is default prevented. No paste event ever reaches xterm's textarea.

I confirmed this in a running Code OSS instance by hooking Event.prototype.preventDefault. Over text, the mouseup was prevented from TerminalLink.activate → terminalLinkManager.ts. Over blank space, nothing prevented it.

Fix

Ignore link activations from non-primary buttons before calling preventDefault(). Nothing changes for left-click activation, Ctrl/Alt+click behaviour, or links opened from the quick pick (TerminalLinkQuickPickEvent reports button 0).

A matching upstream fix for xterm.js is in xtermjs/xterm.js#6194. I've kept the guard here as well, because extension-provided link handlers also go through this code path.

How to test

On Linux:

  1. Open a terminal and run echo alpha beta gamma a few times.
  2. Select a word anywhere, in the terminal, the editor or another app. Don't copy it.
  3. Middle click over text in the terminal. The selection is pasted at the prompt. Before this change, nothing happened.
  4. Middle click on empty terminal space. It still pastes.
  5. Ctrl+click (or Alt+click, depending on editor.multiCursorModifier) on a URL or file link. It still opens.

A unit test in terminalLinkManager.test.ts checks that only primary-button activations are default prevented.


Note

This PR was prepared with the assistance of an AI coding agent (Claude Code). The agent did the investigation (reproducing the bug in a live Code OSS instance and tracing the preventDefault() call), the fix and the test. I've reviewed the change and tested it locally.

Copilot AI balanced review requested due to automatic review settings September 28, 2026 23:28
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminalContrib/links/browser/terminalLinkManager.ts
  • src/vs/workbench/contrib/terminalContrib/links/test/browser/terminalLinkManager.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused guard addresses the reported regression while preserving primary-button and quick-pick activation paths, with targeted unit coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes Linux primary-selection paste in the integrated terminal by ignoring non-primary mouse-button link activations before preventing the browser default.

Changes:

  • Guards terminal link activation against middle/right mouse buttons.
  • Adds coverage verifying only primary-button activation calls preventDefault().
  • Adds the product service test dependency needed by link detection.
File Description
src/​vs/​workbench/​contrib/​terminalContrib/​links/​browser/​terminalLinkManager.ts Skips non-primary link activation before default prevention.
src/​vs/​workbench/​contrib/​terminalContrib/​links/​test/​browser/​terminalLinkManager.test.ts Tests default-prevention behavior for primary, middle, and secondary buttons.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dmke

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

xterm.js activates links on mouseup of any mouse button. Since the word
link detector turns every word into a link, middle clicking on text
activated a link, and the activation handler unconditionally called
preventDefault() on the mouseup event. Chromium skips pasting the
selection clipboard when mouseup is default prevented, so middle click
paste only worked on empty terminal space.

Ignore link activations from non-primary buttons before calling
preventDefault().

Fixes microsoft#157233
@dmke
Dominik Menke (dmke) force-pushed the fix/terminal-middle-click-paste-157233 branch from 8c05c6e to e7fade0 Compare October 11, 2026 12:35
@dmke

Copy link
Copy Markdown
Author

Anthony Kim (@anthonykim1): What's missing to get this merged?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrated terminal: Inserting X selections requires precise mouse positioning

3 participants