I run a Linux emulator (or subsystem) under Windows 10, and sometimes I need to past Windows paths in the Linux console. Windows paths use backslashes \ to separate directories whereas Linux paths use forward slashes / instead.

To avoid having to replace the \ with / manually, I have tried using an AHK script to do the replacement on any path that I copy, but it doesn't want to work.

This is my script (I have marked in the code ##works until here## the furthest point until where it seems to be working):

^+7:: ; Ctrl+Shift+7 (/)

;Empty the Clipboard.
    Clipboard =
;Copy the select text to the Clipboard.
    SendInput, ^c
;Wait for the Clipboard to fill.

;Perform the RegEx find and replace operation,
;where the needle is what we want to replace.
    haystack := Clipboard
    needle := "\"
    replacement := "/"
    result := RegExReplace(haystack, needle, replacement)

;Empty the Clipboard
    Clipboard =
;Copy the result to the Clipboard.
    Clipboard := result
;##works until here##
;Wait for the Clipboard to fill.

;-- Optional: --
;Send (paste) the contents of the new Clipboard.
    SendInput, %Clipboard%


Thanks in advance for any tips.


It seems the backslash needs to be escaped to be recognized by RegExReplace, like this:

needle := "\\"

cd to windows dir using cdw in WSL

In zsh, use cdw followed by the directory path (fixed) to change the working directory to a Windows folder.

# cd into Win dir
function cdw(){
    # cdw = cd to window dir
    # win dir should look like "g:/demo_dir/app_folder" (note these are forward slashes)
    # Need Autohotky to convert all backward slashes in directory pathes into forward slashes 
    cd "$(wslpath "$1")"

From the Windows part, use the following AHK script to flip all backslashes in the clipboard. This is checked every time that the clipboard refreshes and directories are recognized with :/ as a marker.

; In preamble (before the first return statement)
; Note, the ClipChanged function is premitive: it does not deal with whitespaces properly and it will screw things up. 
; Comment out the line in the preamble when done with it.


; Then, after the first return statement, complete the function with the following:
ClipChanged(Type) {
; This is a very limited function. Only use it when needed
    ; Step 1: take out the trailing backslash
    ClipSave := Clipboard
    haystack := RTrim(ClipSave, "//")
    ; Step 2: swap / to be /
    needle := "//"
    replacement := "/"
    ; haystack := RegExReplace(haystack, needle, replacement)
    Clipboard := RegExReplace(haystack, needle, replacement)

NOTE: the AHK function ClipChanged is only a proof of concept. I turned off the line OnClipboardChange("ClipChanged") after finished populating a Tmux session with 10+ folders.

This is how the whole thing works in action, where I:

  1. Copied the g:\ string from the Run window, and
  2. Issued cdw + mouse-middle-button in a WSL window

Behind the scene, the AHK script fixes the backslash probelm and the zsh console happily admits g:/ as a valid input argument and passes to cdw() function.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.