zsh: complete hostnames and files in given directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












I have a script myscript which takes two arguments:



  1. hostname

  2. directory

How can I write my own zsh completion, so that whenever I do




mysript <TAB>


it completes from my hosts list (ie same as ssh does) and when I do




mysript host1 <TAB>


it completes from directories in /home/martin/test/ ?












share|improve this question























  • You might take a look at This.
    – AsenM
    Dec 6 at 16:30















up vote
7
down vote

favorite
1












I have a script myscript which takes two arguments:



  1. hostname

  2. directory

How can I write my own zsh completion, so that whenever I do




mysript <TAB>


it completes from my hosts list (ie same as ssh does) and when I do




mysript host1 <TAB>


it completes from directories in /home/martin/test/ ?












share|improve this question























  • You might take a look at This.
    – AsenM
    Dec 6 at 16:30













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





I have a script myscript which takes two arguments:



  1. hostname

  2. directory

How can I write my own zsh completion, so that whenever I do




mysript <TAB>


it completes from my hosts list (ie same as ssh does) and when I do




mysript host1 <TAB>


it completes from directories in /home/martin/test/ ?












share|improve this question















I have a script myscript which takes two arguments:



  1. hostname

  2. directory

How can I write my own zsh completion, so that whenever I do




mysript <TAB>


it completes from my hosts list (ie same as ssh does) and when I do




mysript host1 <TAB>


it completes from directories in /home/martin/test/ ?









zsh autocomplete






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 2 at 11:25









Thomas

3,67161225




3,67161225










asked Dec 2 at 11:14









Martin Vegter

22634119234




22634119234











  • You might take a look at This.
    – AsenM
    Dec 6 at 16:30

















  • You might take a look at This.
    – AsenM
    Dec 6 at 16:30
















You might take a look at This.
– AsenM
Dec 6 at 16:30





You might take a look at This.
– AsenM
Dec 6 at 16:30











1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted
+500










Thank you for the interesting question. I would like to do the same in my scripts. The documentation is dense and not so easy to understand; I have not yet learned to work without actual options in the script. Here's my first attempt at accomplishing the goal with actual options.



First, I created a shell script named myscript.sh that uses options.



#!/usr/bin/env sh
self=$(basename "$0")
hflag=0 # Boolean: hflag is not yet detected
dflag=0 # Boolean: dflag is not yet detected

function usage() -d <directory> ]"


# If no options were given, exit with message and code.
if (($# == 0)); then
usage
exit 1
fi

# Process options and option arguments.
while getopts ":h:d:" option; do
case "$option" in
h ) hflag=1 # The h option was used.
host=$OPTARG # The argument to the h option.
;;
d ) dflag=1 # The d option was used.
dir=$OPTARG # The argument to the d option.
;;
?) # An invalid option was detected.
usage
exit 1
;;
: ) # An option was given without an option argument.
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done

# One of hflag or dflag was missing.
if [ $hflag -eq 0 ] || [ $dflag -eq 0 ]; then
usage
exit 1
fi

# Do something with $host and $dir.
# This is where the actions of your current script should be placed.
# Here, I am just printing them.
echo "$host"
echo "$dir"

# Unset variables used in the script.
unset self
unset hflag
unset dflag


Next, I determined where zsh looks for autocomplete files.



print -rl -- $fpath


I chose one of the directories, /usr/local/share/zsh/site-functions in my case. The filenames that are considered autocomplete files begin with an underscore _ character. I created the file, _myscript, in the directory. The portion after #compdef is the actual script name, above.



#compdef myscript.sh

_myscript()
_arguments '-h[host]:hosts:_hosts' '-d[directory]:directories:_directories'


_myscript "$@"


I then executed compinit to pick up the new autocomplete definition provided by the _myscript file. The result is that I can now use tab completion to specify a host after the -h option and a directory after the -d option while still maintaining some sanity in the parsing of options and option arguments in the script itself. The tab completion presents available options even before invoking myscript.sh as well as making option order irrelevant.



Usage becomes something like the following.



myscript.sh -h <TAB> -d ~/test/<TAB>



Summary Solution



On the second attempt, I created a simple shell script, zscript.sh.



#!/usr/bin/env sh
echo "$1"
echo "$2"


And I created a file, /usr/local/share/zsh/site-functions/_zscript.



#compdef zscript.sh

_zscript()
_arguments '1: :->hostname' '2: :->directory'
case $state in
hostname)
_hosts
;;
directory)
_directories -W $HOME/test/
;;
esac



I executed compinit.






share|improve this answer






















  • thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
    – Martin Vegter
    Dec 7 at 9:00











  • @MartinVegter Does the Summary Solution work well for you?
    – Christopher
    Dec 7 at 15:16










  • completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
    – Martin Vegter
    Dec 7 at 17:16










  • Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
    – Christopher
    Dec 7 at 18:13










  • perfect. Now it works. thaanks.
    – Martin Vegter
    Dec 7 at 20:02










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485470%2fzsh-complete-hostnames-and-files-in-given-directory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted
+500










Thank you for the interesting question. I would like to do the same in my scripts. The documentation is dense and not so easy to understand; I have not yet learned to work without actual options in the script. Here's my first attempt at accomplishing the goal with actual options.



First, I created a shell script named myscript.sh that uses options.



#!/usr/bin/env sh
self=$(basename "$0")
hflag=0 # Boolean: hflag is not yet detected
dflag=0 # Boolean: dflag is not yet detected

function usage() -d <directory> ]"


# If no options were given, exit with message and code.
if (($# == 0)); then
usage
exit 1
fi

# Process options and option arguments.
while getopts ":h:d:" option; do
case "$option" in
h ) hflag=1 # The h option was used.
host=$OPTARG # The argument to the h option.
;;
d ) dflag=1 # The d option was used.
dir=$OPTARG # The argument to the d option.
;;
?) # An invalid option was detected.
usage
exit 1
;;
: ) # An option was given without an option argument.
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done

# One of hflag or dflag was missing.
if [ $hflag -eq 0 ] || [ $dflag -eq 0 ]; then
usage
exit 1
fi

# Do something with $host and $dir.
# This is where the actions of your current script should be placed.
# Here, I am just printing them.
echo "$host"
echo "$dir"

# Unset variables used in the script.
unset self
unset hflag
unset dflag


Next, I determined where zsh looks for autocomplete files.



print -rl -- $fpath


I chose one of the directories, /usr/local/share/zsh/site-functions in my case. The filenames that are considered autocomplete files begin with an underscore _ character. I created the file, _myscript, in the directory. The portion after #compdef is the actual script name, above.



#compdef myscript.sh

_myscript()
_arguments '-h[host]:hosts:_hosts' '-d[directory]:directories:_directories'


_myscript "$@"


I then executed compinit to pick up the new autocomplete definition provided by the _myscript file. The result is that I can now use tab completion to specify a host after the -h option and a directory after the -d option while still maintaining some sanity in the parsing of options and option arguments in the script itself. The tab completion presents available options even before invoking myscript.sh as well as making option order irrelevant.



Usage becomes something like the following.



myscript.sh -h <TAB> -d ~/test/<TAB>



Summary Solution



On the second attempt, I created a simple shell script, zscript.sh.



#!/usr/bin/env sh
echo "$1"
echo "$2"


And I created a file, /usr/local/share/zsh/site-functions/_zscript.



#compdef zscript.sh

_zscript()
_arguments '1: :->hostname' '2: :->directory'
case $state in
hostname)
_hosts
;;
directory)
_directories -W $HOME/test/
;;
esac



I executed compinit.






share|improve this answer






















  • thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
    – Martin Vegter
    Dec 7 at 9:00











  • @MartinVegter Does the Summary Solution work well for you?
    – Christopher
    Dec 7 at 15:16










  • completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
    – Martin Vegter
    Dec 7 at 17:16










  • Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
    – Christopher
    Dec 7 at 18:13










  • perfect. Now it works. thaanks.
    – Martin Vegter
    Dec 7 at 20:02














up vote
3
down vote



accepted
+500










Thank you for the interesting question. I would like to do the same in my scripts. The documentation is dense and not so easy to understand; I have not yet learned to work without actual options in the script. Here's my first attempt at accomplishing the goal with actual options.



First, I created a shell script named myscript.sh that uses options.



#!/usr/bin/env sh
self=$(basename "$0")
hflag=0 # Boolean: hflag is not yet detected
dflag=0 # Boolean: dflag is not yet detected

function usage() -d <directory> ]"


# If no options were given, exit with message and code.
if (($# == 0)); then
usage
exit 1
fi

# Process options and option arguments.
while getopts ":h:d:" option; do
case "$option" in
h ) hflag=1 # The h option was used.
host=$OPTARG # The argument to the h option.
;;
d ) dflag=1 # The d option was used.
dir=$OPTARG # The argument to the d option.
;;
?) # An invalid option was detected.
usage
exit 1
;;
: ) # An option was given without an option argument.
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done

# One of hflag or dflag was missing.
if [ $hflag -eq 0 ] || [ $dflag -eq 0 ]; then
usage
exit 1
fi

# Do something with $host and $dir.
# This is where the actions of your current script should be placed.
# Here, I am just printing them.
echo "$host"
echo "$dir"

# Unset variables used in the script.
unset self
unset hflag
unset dflag


Next, I determined where zsh looks for autocomplete files.



print -rl -- $fpath


I chose one of the directories, /usr/local/share/zsh/site-functions in my case. The filenames that are considered autocomplete files begin with an underscore _ character. I created the file, _myscript, in the directory. The portion after #compdef is the actual script name, above.



#compdef myscript.sh

_myscript()
_arguments '-h[host]:hosts:_hosts' '-d[directory]:directories:_directories'


_myscript "$@"


I then executed compinit to pick up the new autocomplete definition provided by the _myscript file. The result is that I can now use tab completion to specify a host after the -h option and a directory after the -d option while still maintaining some sanity in the parsing of options and option arguments in the script itself. The tab completion presents available options even before invoking myscript.sh as well as making option order irrelevant.



Usage becomes something like the following.



myscript.sh -h <TAB> -d ~/test/<TAB>



Summary Solution



On the second attempt, I created a simple shell script, zscript.sh.



#!/usr/bin/env sh
echo "$1"
echo "$2"


And I created a file, /usr/local/share/zsh/site-functions/_zscript.



#compdef zscript.sh

_zscript()
_arguments '1: :->hostname' '2: :->directory'
case $state in
hostname)
_hosts
;;
directory)
_directories -W $HOME/test/
;;
esac



I executed compinit.






share|improve this answer






















  • thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
    – Martin Vegter
    Dec 7 at 9:00











  • @MartinVegter Does the Summary Solution work well for you?
    – Christopher
    Dec 7 at 15:16










  • completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
    – Martin Vegter
    Dec 7 at 17:16










  • Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
    – Christopher
    Dec 7 at 18:13










  • perfect. Now it works. thaanks.
    – Martin Vegter
    Dec 7 at 20:02












up vote
3
down vote



accepted
+500







up vote
3
down vote



accepted
+500




+500




Thank you for the interesting question. I would like to do the same in my scripts. The documentation is dense and not so easy to understand; I have not yet learned to work without actual options in the script. Here's my first attempt at accomplishing the goal with actual options.



First, I created a shell script named myscript.sh that uses options.



#!/usr/bin/env sh
self=$(basename "$0")
hflag=0 # Boolean: hflag is not yet detected
dflag=0 # Boolean: dflag is not yet detected

function usage() -d <directory> ]"


# If no options were given, exit with message and code.
if (($# == 0)); then
usage
exit 1
fi

# Process options and option arguments.
while getopts ":h:d:" option; do
case "$option" in
h ) hflag=1 # The h option was used.
host=$OPTARG # The argument to the h option.
;;
d ) dflag=1 # The d option was used.
dir=$OPTARG # The argument to the d option.
;;
?) # An invalid option was detected.
usage
exit 1
;;
: ) # An option was given without an option argument.
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done

# One of hflag or dflag was missing.
if [ $hflag -eq 0 ] || [ $dflag -eq 0 ]; then
usage
exit 1
fi

# Do something with $host and $dir.
# This is where the actions of your current script should be placed.
# Here, I am just printing them.
echo "$host"
echo "$dir"

# Unset variables used in the script.
unset self
unset hflag
unset dflag


Next, I determined where zsh looks for autocomplete files.



print -rl -- $fpath


I chose one of the directories, /usr/local/share/zsh/site-functions in my case. The filenames that are considered autocomplete files begin with an underscore _ character. I created the file, _myscript, in the directory. The portion after #compdef is the actual script name, above.



#compdef myscript.sh

_myscript()
_arguments '-h[host]:hosts:_hosts' '-d[directory]:directories:_directories'


_myscript "$@"


I then executed compinit to pick up the new autocomplete definition provided by the _myscript file. The result is that I can now use tab completion to specify a host after the -h option and a directory after the -d option while still maintaining some sanity in the parsing of options and option arguments in the script itself. The tab completion presents available options even before invoking myscript.sh as well as making option order irrelevant.



Usage becomes something like the following.



myscript.sh -h <TAB> -d ~/test/<TAB>



Summary Solution



On the second attempt, I created a simple shell script, zscript.sh.



#!/usr/bin/env sh
echo "$1"
echo "$2"


And I created a file, /usr/local/share/zsh/site-functions/_zscript.



#compdef zscript.sh

_zscript()
_arguments '1: :->hostname' '2: :->directory'
case $state in
hostname)
_hosts
;;
directory)
_directories -W $HOME/test/
;;
esac



I executed compinit.






share|improve this answer














Thank you for the interesting question. I would like to do the same in my scripts. The documentation is dense and not so easy to understand; I have not yet learned to work without actual options in the script. Here's my first attempt at accomplishing the goal with actual options.



First, I created a shell script named myscript.sh that uses options.



#!/usr/bin/env sh
self=$(basename "$0")
hflag=0 # Boolean: hflag is not yet detected
dflag=0 # Boolean: dflag is not yet detected

function usage() -d <directory> ]"


# If no options were given, exit with message and code.
if (($# == 0)); then
usage
exit 1
fi

# Process options and option arguments.
while getopts ":h:d:" option; do
case "$option" in
h ) hflag=1 # The h option was used.
host=$OPTARG # The argument to the h option.
;;
d ) dflag=1 # The d option was used.
dir=$OPTARG # The argument to the d option.
;;
?) # An invalid option was detected.
usage
exit 1
;;
: ) # An option was given without an option argument.
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done

# One of hflag or dflag was missing.
if [ $hflag -eq 0 ] || [ $dflag -eq 0 ]; then
usage
exit 1
fi

# Do something with $host and $dir.
# This is where the actions of your current script should be placed.
# Here, I am just printing them.
echo "$host"
echo "$dir"

# Unset variables used in the script.
unset self
unset hflag
unset dflag


Next, I determined where zsh looks for autocomplete files.



print -rl -- $fpath


I chose one of the directories, /usr/local/share/zsh/site-functions in my case. The filenames that are considered autocomplete files begin with an underscore _ character. I created the file, _myscript, in the directory. The portion after #compdef is the actual script name, above.



#compdef myscript.sh

_myscript()
_arguments '-h[host]:hosts:_hosts' '-d[directory]:directories:_directories'


_myscript "$@"


I then executed compinit to pick up the new autocomplete definition provided by the _myscript file. The result is that I can now use tab completion to specify a host after the -h option and a directory after the -d option while still maintaining some sanity in the parsing of options and option arguments in the script itself. The tab completion presents available options even before invoking myscript.sh as well as making option order irrelevant.



Usage becomes something like the following.



myscript.sh -h <TAB> -d ~/test/<TAB>



Summary Solution



On the second attempt, I created a simple shell script, zscript.sh.



#!/usr/bin/env sh
echo "$1"
echo "$2"


And I created a file, /usr/local/share/zsh/site-functions/_zscript.



#compdef zscript.sh

_zscript()
_arguments '1: :->hostname' '2: :->directory'
case $state in
hostname)
_hosts
;;
directory)
_directories -W $HOME/test/
;;
esac



I executed compinit.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 7 at 17:25

























answered Dec 5 at 17:39









Christopher

10.1k32847




10.1k32847











  • thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
    – Martin Vegter
    Dec 7 at 9:00











  • @MartinVegter Does the Summary Solution work well for you?
    – Christopher
    Dec 7 at 15:16










  • completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
    – Martin Vegter
    Dec 7 at 17:16










  • Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
    – Christopher
    Dec 7 at 18:13










  • perfect. Now it works. thaanks.
    – Martin Vegter
    Dec 7 at 20:02
















  • thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
    – Martin Vegter
    Dec 7 at 9:00











  • @MartinVegter Does the Summary Solution work well for you?
    – Christopher
    Dec 7 at 15:16










  • completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
    – Martin Vegter
    Dec 7 at 17:16










  • Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
    – Christopher
    Dec 7 at 18:13










  • perfect. Now it works. thaanks.
    – Martin Vegter
    Dec 7 at 20:02















thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
– Martin Vegter
Dec 7 at 9:00





thank you, that works. Although not exactly as I imagined. First, as I explained, I need to complete directories form specific path, regardless what my $PWD is currently. Wherever I am in my filesystem tree, I need completion to complete directories from /foo/bar/. Second, is it possible to get rid of the -h and -d switches ? First argument should always be host, second always directory.
– Martin Vegter
Dec 7 at 9:00













@MartinVegter Does the Summary Solution work well for you?
– Christopher
Dec 7 at 15:16




@MartinVegter Does the Summary Solution work well for you?
– Christopher
Dec 7 at 15:16












completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
– Martin Vegter
Dec 7 at 17:16




completing hosts works. But completing the contents of $HOME/test/ does not work. When I press TAB, it simply completes $HOME/test/ path itself and then offers me files in current path. Also, not sure what you mean with -P vs -W. I need to complete the contents of $HOME/test/, not the whole path. If it contains dirs aa1 and aaa2, then tab should offer those two.
– Martin Vegter
Dec 7 at 17:16












Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
– Christopher
Dec 7 at 18:13




Sorry. I can't see your script to know whether to return a full path or just the last directory name. As it is now, it's going to return $2 as, e.g, aa1. Thus, I guess that your script must use the value of $2 in a way such as the following: "$HOME/test/$2" or "/home/martin/test/$2"?
– Christopher
Dec 7 at 18:13












perfect. Now it works. thaanks.
– Martin Vegter
Dec 7 at 20:02




perfect. Now it works. thaanks.
– Martin Vegter
Dec 7 at 20:02

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485470%2fzsh-complete-hostnames-and-files-in-given-directory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay