Skip to content

Support copy command on windows and wsl#1153

Merged
tompng merged 4 commits into
ruby:masterfrom
hogelog:irb-copy-win
Jan 8, 2026
Merged

Support copy command on windows and wsl#1153
tompng merged 4 commits into
ruby:masterfrom
hogelog:irb-copy-win

Conversation

@hogelog

@hogelog hogelog commented Dec 27, 2025

Copy link
Copy Markdown
Contributor

Enable the copy command on Windows and WSL environments.

The copy command now works both on Windows Ruby and on WSL, copying the evaluated result to the system clipboard.

Examples

Windows (mingw-ucrt)

PS C:\Ruby\repos\irb> ruby -v
ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [x64-mingw-ucrt]
PS C:\Ruby\repos\irb> ruby -Ilib .\exe\irb
irb(main):001> RUBY_PLATFORM
=> "x64-mingw-ucrt"
irb(main):002> copy 2**11
Copied to system clipboard
irb(main):003> 2048 # <- Ctrl-V
=> 2048

WSL

hogelog@lenovo:/mnt/c/Ruby/repos/irb$ ruby -Ilib ./exe/irb
irb(main):001> RUBY_PLATFORM
=> "x86_64-linux"
irb(main):002> copy 2**10
Copied to system clipboard
irb(main):003> 1024 # <- Ctrl-V
=> 1024

Windows workaround

On Windows, passing text via stdin does not work reliably in IRB. After calling IO.popen, IRB’s input handling becomes unstable, but I have not fully identified the root cause yet.

To work around this, the Windows implementation invokes PowerShell’s Set-Clipboard and passes the text via command-line arguments instead of stdin:

powershell.exe -NoProfile -Command Set-Clipboard -Value <text>

This approach works reliably on native Windows Ruby.

This issue is likely not limited to the copy command, and there may be other cases where IRB does not behave correctly. If this problem can be resolved, it should be possible to simply call clip.exe even on Windows. I plan to investigate this separately.

Comment thread lib/irb/command/copy.rb Outdated
def copy_to_clipboard(text)
IO.popen(clipboard_program, 'w') do |io|
io.write(text)
if Gem.win_platform?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's a flag --disable-gems

$ ruby --disable-gems -e binding.irb      
irb(main):001> Gem
uninitialized constant Gem (NameError)

I think it's better to add a guard like defined?(Gem) &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had not considered running IRB in an environment where Gem is not loaded. But I think a simple Windows check is sufficient here. How about this diff?
d97375e

Comment thread lib/irb/command/copy.rb Outdated
IO.popen(clipboard_program, 'w') do |io|
io.write(text)
if Gem.win_platform?
Kernel.system("powershell.exe", "-NoProfile", "-Command", "Set-Clipboard", "-Value", text)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On Windows, passing text via stdin does not work reliably in IRB

This might be fixed in ruby/reline#875
Although I also think adding this workaround for a while is worth. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had not noticed ruby/reline#875. With this fix, the workaround seems unnecessary, and it looks like we can remove the unnatural branching, so I deleted it e90574a

Comment thread lib/irb/command/copy.rb Outdated

def clipboard_available?
!!clipboard_program
windows? || !!clipboard_program

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think windows? || part is not needed. clipboard_program now checks executable?("clip.exe") with the correct command "where #{command} > NUL 2>&1" and returns true if clip.exe exists.

Other part of this pull request looks good 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed! I fixed it 2471ee4

@tompng tompng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you 👍

@tompng tompng merged commit f5124c5 into ruby:master Jan 8, 2026
37 checks passed
@hogelog hogelog deleted the irb-copy-win branch January 9, 2026 02:51
@st0012 st0012 added the bug Something isn't working label Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants