Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Absolute path tab completion on Windows




On Thu, Nov 7, 2024 at 9:49 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
Here's a .zshrc-level workaround to try:

functions -c -- _normal _normal@c:drive
_normal () {
  if compset -P /c/
  then
    local opwd=$PWD ret
    cd /c || return 1
    _normal@c:drive "$@"
    ret=$?
    cd $opwd
    return ret
  else
    _normal@c:drive "$@"
  fi
}

Works, thank you Bart!

I used Claude to extend this code to apply to all drive letters, code below. One problem with this code is that it only autocompletes the paths when they are an argument to a command, and not when they are the first thing that's typed in the shell, like you might do to `cd` into a folder without typing `cd`. If anyone knows how to fix that, let me know. The code:

get_drive_letters() {
    mount | grep -i '^[a-z]:' | cut -d':' -f1 | tr '[:upper:]' '[:lower:]' | sort -u
}

# Store the original completion function
functions -c -- _normal _normal@original

# Create completion functions for each drive
for drive in $(mount | grep -i '^[a-z]:' | cut -d':' -f1 | tr '[:upper:]' '[:lower:]' | sort -u); do
    # Create a drive-specific completion function
    eval "functions -c -- _normal@original _normal@${drive}:drive"
done

_normal () {
    for drive in $(mount | grep -i '^[a-z]:' | cut -d':' -f1 | tr '[:upper:]' '[:lower:]' | sort -u); do
        if compset -P "/${drive}/"
        then
            local opwd=$PWD ret
            cd /${drive} || return 1
            _normal@${drive}:drive "$@"
            ret=$?
            cd $opwd
            return ret
        fi
    done
    _normal@original "$@"
}




Messages sorted by: Reverse Date, Date, Thread, Author