From 5e55fb4f381046c128e3a6cd3dae3cc2d6df260a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Lindh=C3=A9?= Date: Thu, 21 Mar 2024 15:39:31 +0100 Subject: [PATCH] Update copy.sh for better WSL support --- terminal/copy.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/terminal/copy.sh b/terminal/copy.sh index d5392fe..c0ad40e 100755 --- a/terminal/copy.sh +++ b/terminal/copy.sh @@ -14,6 +14,21 @@ fail() { exit "${2:-1}" } +wsl_copy() { + # This function is superior to Windows' copy.exe, since it does not mess up + # text encoding by asssuming BOM. + # https://github.com/microsoft/WSL/issues/10095#issuecomment-1602833117 + INPUT="$(cat; echo x)" + declare -r INPUT="${INPUT%x}" # https://stackoverflow.com/a/32365596/893211 +# Note that the string terminator '@ must be on a separate line and must not +# have leading whitespace: +# https://devblogs.microsoft.com/scripting/powershell-for-programmers-here-strings-there-strings-everywhere-some-string-strings/ +powershell.exe -c "Set-Clipboard @' +${INPUT} +'@ +" +} + if [[ "$(uname -r)" =~ .*microsoft.* ]]; then declare -r IS_WSL=true else @@ -21,7 +36,7 @@ else fi if [[ "${IS_WSL}" == "true" ]]; then - declare -r CLIPBOARD_CMD='/mnt/c/Windows/System32/clip.exe' + declare -r CLIPBOARD_CMD=wsl_copy else missing_dependencies=false declare -r dependencies=(