- Latest Update
The following should be enough as
wslpath
has already been implemented so we don't needwpath
(see comments)
function cdw() {
cd "$(wslpath "$1")"
}
This function could parse the Windows path, convert it to the WSL path, and then execute cd to that path.
Here's a simple example of a Bash function you can add to your ~/.bashrc
or ~/.zshrc
file to achieve this. This script checks if the input path is in Windows format and then converts it to the Unix-style path used in WSL:
function wpath() {
local input_path="${1//\\//}" # Convert backslashes to forward slashes
# Normalize input to ensure it ends with a '/' if it's a drive letter only
if [[ "$input_path" =~ ^[a-zA-Z]:$ ]]; then
input_path="${input_path}/" # Append '/' if input is only 'd:'
fi
if [[ "$input_path" =~ ^[a-zA-Z]:/ ]]; then
# Remove the colon and convert drive letter to lowercase
local drive="${input_path:0:1}"
drive=$(echo "$drive" | tr '[:upper:]' '[:lower:]')
# Remove the drive letter and colon, then prepend with '/mnt/'
local path_without_drive="${input_path:2}"
local wslpath="/mnt/$drive$path_without_drive"
echo "$wslpath" || return
else
# If it's not a Windows path, try to cd directly
echo "$input_path" || return
fi
}
function cdw() {
cd "$(wpath "$1")"
}
After updating the function in your .bashrc
or .zshrc
file, make sure to reload the file:
For Bash: source ~/.bashrc
For Zsh: source ~/.zshrc
Try using the cdw command again with a Windows-style path.
Examples:
cdw "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0"
wpath "C:\Program Files (x86)"
file $(wpath 'C:\windows\win.ini')
ls -la $(wpath c:)
ls -la "$(wpath 'C:\Program Files (x86)')"
ChatGPT helped @irsdl to complete this simple task!
non-working code for word completion on the path