0

On my Linux machine I have used a nice zsh feature to complete multipart paths with a single tab press:

$ cd /h/p<tab>
  ->
$ cd /home/plato 

I am currently developing in a Windows environment, using Git Bash.

Is there any way I can replicate this behavior, complete paths without typing out the first parts completely?

dbc
  • 104,963
  • 20
  • 228
  • 340
Plato
  • 10,812
  • 2
  • 41
  • 61

2 Answers2

1

You can use the complete command to run a custom function when you press tab. Add these lines to .bashrc:

# Solution written by izabera on freenode. tyvm!
#.bashrc
myfunction () {
  local path oldpath ng=$(shopt -p nullglob)
  shopt -s nullglob
  printf -v path %q "${COMP_WORDS[COMP_CWORD]}"
  oldpath=$path
  path=${path//\//*/} path=${path#\*}
  eval "COMPREPLY=($path*)"
  if (( ${#COMPREPLY[@]} == 0 )); then COMPREPLY=("$oldpath"); fi
  eval "$ng"
}

complete -D -F myfunction -o bashdefault -o default -o filenames

edit: the -D flag was introduced in bash 4.1, the current bash version used by git-bash is 3.1.1. The flag sets the completion function as the default, for any command that does not already have a completion function defined.

On 3.1.1, the best you can do is configure specific commands:

#.bashrc
myfunction () { ... }
complete -F myfunction -o bashdefault -o default -o filenames cd ls cp mv node npm
Plato
  • 10,812
  • 2
  • 41
  • 61
0

Disclosure: I am the author of the project

This project enables the desired completion in bash: https://github.com/sio/bash-complete-partial-path

It works just fine on Windows with Git bash. Hope you'll find it useful

SIO
  • 404
  • 1
  • 5
  • 11