iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📋

Enabling the /copy Command of Claude Code in WSL2 (A Fix for Character Encoding Issues)

に公開

What is Claude Code's /copy command?

The /copy command is a feature that allows you to copy responses from Claude Code to the clipboard.

When copying Claude Code's output by selecting it with the mouse in the terminal, it often requires extra effort to format, as unwanted spaces appear at the beginning or newlines are inserted at line wrap positions. Using /copy eliminates such hassles, allowing you to copy the content cleanly.

Furthermore, from v2.1.59, it became possible to select and copy individual code blocks, not just the entire response. Even if a response contains code in multiple languages, you can pinpoint and copy only the necessary code blocks.

I learned about this from Oikon's post. Thank you as always.

https://x.com/oikon48/status/2026834151840829615

Problem

However, when /copy is executed in a WSL2 environment, Japanese characters and emojis are garbled.

繝・せ繝域律譛ャ隱・

This is not limited to WSL2; the same phenomenon occurs when Japanese text is piped to clip.exe.

echo "テスト日本語" | clip.exe
# -> Pasting with Ctrl+V results in garbled text

Cause

In a WSL2 environment, Claude Code uses Windows' clip.exe to copy to the clipboard. clip.exe interprets input from a pipe as Windows' system default code page (CP932/Shift-JIS in a Japanese environment). Since the WSL2 side is UTF-8, the UTF-8 byte sequence is incorrectly interpreted as Shift-JIS, causing character corruption.

Solution

Place a wrapper script for clip.exe in ~/.local/bin/ so that it is called before the actual clip.exe according to the PATH priority.

Inside the wrapper, data is passed to clip after switching the code page to UTF-8 (65001) via cmd.exe.

1. Create a clip.exe wrapper

~/.local/bin/clip.exe
#!/bin/bash
cmd.exe /c "chcp 65001 >nul & clip" 2>/dev/null
chmod +x ~/.local/bin/clip.exe

2. PATH settings

If ~/.local/bin is not included in your PATH, add the following to ~/.bashrc:

export PATH="$HOME/.local/bin:$PATH"

Adding it to the beginning ensures it takes precedence over /mnt/c/Windows/system32/clip.exe.

3. Check operation

# Confirm that the wrapper is prioritized
which clip.exe
# -> If it displays /home/<user>/.local/bin/clip.exe, it's OK

# Test copying Japanese text
echo "テスト日本語" | clip.exe
# -> If it pastes correctly with Ctrl+V, it's OK

With this, Claude Code's /copy command will now correctly copy responses containing Japanese text and emojis.

Environment

  • Windows 11
  • WSL2 (Ubuntu)
  • Windows Terminal

Discussion