Treat command like another for completion purposes
Clash Royale CLAN TAG#URR8PPP
My zshrc includes the following function to create a directory and then enter it:
function mcd ()
mkdir -p "$*" && cd "$*"
The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/
and then press Tab, the message
_mtools_drives:3: command not found: mtoolstest
is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir
would be: zsh should offer me the names of existing directories.
How do I tell zsh that for completion purposes, it should treat mcd
the same as mkdir
?
zsh autocomplete
add a comment |
My zshrc includes the following function to create a directory and then enter it:
function mcd ()
mkdir -p "$*" && cd "$*"
The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/
and then press Tab, the message
_mtools_drives:3: command not found: mtoolstest
is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir
would be: zsh should offer me the names of existing directories.
How do I tell zsh that for completion purposes, it should treat mcd
the same as mkdir
?
zsh autocomplete
add a comment |
My zshrc includes the following function to create a directory and then enter it:
function mcd ()
mkdir -p "$*" && cd "$*"
The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/
and then press Tab, the message
_mtools_drives:3: command not found: mtoolstest
is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir
would be: zsh should offer me the names of existing directories.
How do I tell zsh that for completion purposes, it should treat mcd
the same as mkdir
?
zsh autocomplete
My zshrc includes the following function to create a directory and then enter it:
function mcd ()
mkdir -p "$*" && cd "$*"
The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/
and then press Tab, the message
_mtools_drives:3: command not found: mtoolstest
is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir
would be: zsh should offer me the names of existing directories.
How do I tell zsh that for completion purposes, it should treat mcd
the same as mkdir
?
zsh autocomplete
zsh autocomplete
edited Jan 25 at 21:20
Gilles
536k12810821600
536k12810821600
asked Jan 24 at 5:00
bdeshambdesham
313212
313212
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The following snippet causes mcd
to be completed like mkdir
:
compdefas ()
if (($+_comps[$1])); then
compdef $_comps[$1] $^@[2,-1]=$1
fi
compdefas mkdir mcd
The way it works is to look up the current completion setting for mkdir
. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps
. Thus compdef $_comps[mkdir] mcd
declares that mcd
should be completed in the same way that mkdir
is completed right now.
The function above adds a few niceties:
- The test for
(($+_comps[$1]))
ensures that if$1
doesn't have a specified completion method then no completion method is set for the other arguments. $@[2,-1]
is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually$^@[a,-1]
so that the text around the array expansion is replicated for each array element.=$1
sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function_gzip
handles bothgzip
andgunzip
as well aspigz
andunpigz
;compdef _gzip foo
makesfoo
use the default behavior of_gzip
whilecompdef _gzip foo=pigz
makesfoo
use the behavior of_gzip
when it completes forpigz
.
Turning to your specific case, the default completion for mkdir
not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd
as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files
).
compdef _directories mcd
The reason you were getting these bizarre-looking completions for mcd
is that it's the name of a command from a once moderately widespread suite of commands mtools.
If I issue commandmcd
without any argument, then it echosmkdir: cannot create directory ‘’: No such file or directory
.mkdir
will givemkdir: missing operand
which look nicer than ourmcd
, how to have the same behavior asmkdir
?
– Tuyen Pham
Jan 27 at 6:31
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to havecompdefas
? As I test yourmkcd
function, it works well with completion.
– Tuyen Pham
Jan 28 at 2:03
add a comment |
compdef
uses the following form:
compdef [ -ane ] function name
so one idea would be to point the name
of mcd
at the _mkdir
function via the following command:
compdef _mkdir mcd
however this is slightly incorrect as mkdir
completes various flags that should probably not also be given to cd
. More direct would be to complete on directories:
compdef _directories mcd
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496379%2ftreat-command-like-another-for-completion-purposes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following snippet causes mcd
to be completed like mkdir
:
compdefas ()
if (($+_comps[$1])); then
compdef $_comps[$1] $^@[2,-1]=$1
fi
compdefas mkdir mcd
The way it works is to look up the current completion setting for mkdir
. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps
. Thus compdef $_comps[mkdir] mcd
declares that mcd
should be completed in the same way that mkdir
is completed right now.
The function above adds a few niceties:
- The test for
(($+_comps[$1]))
ensures that if$1
doesn't have a specified completion method then no completion method is set for the other arguments. $@[2,-1]
is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually$^@[a,-1]
so that the text around the array expansion is replicated for each array element.=$1
sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function_gzip
handles bothgzip
andgunzip
as well aspigz
andunpigz
;compdef _gzip foo
makesfoo
use the default behavior of_gzip
whilecompdef _gzip foo=pigz
makesfoo
use the behavior of_gzip
when it completes forpigz
.
Turning to your specific case, the default completion for mkdir
not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd
as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files
).
compdef _directories mcd
The reason you were getting these bizarre-looking completions for mcd
is that it's the name of a command from a once moderately widespread suite of commands mtools.
If I issue commandmcd
without any argument, then it echosmkdir: cannot create directory ‘’: No such file or directory
.mkdir
will givemkdir: missing operand
which look nicer than ourmcd
, how to have the same behavior asmkdir
?
– Tuyen Pham
Jan 27 at 6:31
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to havecompdefas
? As I test yourmkcd
function, it works well with completion.
– Tuyen Pham
Jan 28 at 2:03
add a comment |
The following snippet causes mcd
to be completed like mkdir
:
compdefas ()
if (($+_comps[$1])); then
compdef $_comps[$1] $^@[2,-1]=$1
fi
compdefas mkdir mcd
The way it works is to look up the current completion setting for mkdir
. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps
. Thus compdef $_comps[mkdir] mcd
declares that mcd
should be completed in the same way that mkdir
is completed right now.
The function above adds a few niceties:
- The test for
(($+_comps[$1]))
ensures that if$1
doesn't have a specified completion method then no completion method is set for the other arguments. $@[2,-1]
is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually$^@[a,-1]
so that the text around the array expansion is replicated for each array element.=$1
sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function_gzip
handles bothgzip
andgunzip
as well aspigz
andunpigz
;compdef _gzip foo
makesfoo
use the default behavior of_gzip
whilecompdef _gzip foo=pigz
makesfoo
use the behavior of_gzip
when it completes forpigz
.
Turning to your specific case, the default completion for mkdir
not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd
as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files
).
compdef _directories mcd
The reason you were getting these bizarre-looking completions for mcd
is that it's the name of a command from a once moderately widespread suite of commands mtools.
If I issue commandmcd
without any argument, then it echosmkdir: cannot create directory ‘’: No such file or directory
.mkdir
will givemkdir: missing operand
which look nicer than ourmcd
, how to have the same behavior asmkdir
?
– Tuyen Pham
Jan 27 at 6:31
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to havecompdefas
? As I test yourmkcd
function, it works well with completion.
– Tuyen Pham
Jan 28 at 2:03
add a comment |
The following snippet causes mcd
to be completed like mkdir
:
compdefas ()
if (($+_comps[$1])); then
compdef $_comps[$1] $^@[2,-1]=$1
fi
compdefas mkdir mcd
The way it works is to look up the current completion setting for mkdir
. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps
. Thus compdef $_comps[mkdir] mcd
declares that mcd
should be completed in the same way that mkdir
is completed right now.
The function above adds a few niceties:
- The test for
(($+_comps[$1]))
ensures that if$1
doesn't have a specified completion method then no completion method is set for the other arguments. $@[2,-1]
is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually$^@[a,-1]
so that the text around the array expansion is replicated for each array element.=$1
sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function_gzip
handles bothgzip
andgunzip
as well aspigz
andunpigz
;compdef _gzip foo
makesfoo
use the default behavior of_gzip
whilecompdef _gzip foo=pigz
makesfoo
use the behavior of_gzip
when it completes forpigz
.
Turning to your specific case, the default completion for mkdir
not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd
as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files
).
compdef _directories mcd
The reason you were getting these bizarre-looking completions for mcd
is that it's the name of a command from a once moderately widespread suite of commands mtools.
The following snippet causes mcd
to be completed like mkdir
:
compdefas ()
if (($+_comps[$1])); then
compdef $_comps[$1] $^@[2,-1]=$1
fi
compdefas mkdir mcd
The way it works is to look up the current completion setting for mkdir
. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps
. Thus compdef $_comps[mkdir] mcd
declares that mcd
should be completed in the same way that mkdir
is completed right now.
The function above adds a few niceties:
- The test for
(($+_comps[$1]))
ensures that if$1
doesn't have a specified completion method then no completion method is set for the other arguments. $@[2,-1]
is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually$^@[a,-1]
so that the text around the array expansion is replicated for each array element.=$1
sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function_gzip
handles bothgzip
andgunzip
as well aspigz
andunpigz
;compdef _gzip foo
makesfoo
use the default behavior of_gzip
whilecompdef _gzip foo=pigz
makesfoo
use the behavior of_gzip
when it completes forpigz
.
Turning to your specific case, the default completion for mkdir
not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd
as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files
).
compdef _directories mcd
The reason you were getting these bizarre-looking completions for mcd
is that it's the name of a command from a once moderately widespread suite of commands mtools.
answered Jan 25 at 21:57
GillesGilles
536k12810821600
536k12810821600
If I issue commandmcd
without any argument, then it echosmkdir: cannot create directory ‘’: No such file or directory
.mkdir
will givemkdir: missing operand
which look nicer than ourmcd
, how to have the same behavior asmkdir
?
– Tuyen Pham
Jan 27 at 6:31
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to havecompdefas
? As I test yourmkcd
function, it works well with completion.
– Tuyen Pham
Jan 28 at 2:03
add a comment |
If I issue commandmcd
without any argument, then it echosmkdir: cannot create directory ‘’: No such file or directory
.mkdir
will givemkdir: missing operand
which look nicer than ourmcd
, how to have the same behavior asmkdir
?
– Tuyen Pham
Jan 27 at 6:31
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to havecompdefas
? As I test yourmkcd
function, it works well with completion.
– Tuyen Pham
Jan 28 at 2:03
If I issue command
mcd
without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory
. mkdir
will give mkdir: missing operand
which look nicer than our mcd
, how to have the same behavior as mkdir
?– Tuyen Pham
Jan 27 at 6:31
If I issue command
mcd
without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory
. mkdir
will give mkdir: missing operand
which look nicer than our mcd
, how to have the same behavior as mkdir
?– Tuyen Pham
Jan 27 at 6:31
1
1
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
@TuyenPham See if my function inspires you.
– Gilles
Jan 27 at 18:54
Thanks, that's truly mkdir wrapper, but should we still need to have
compdefas
? As I test your mkcd
function, it works well with completion.– Tuyen Pham
Jan 28 at 2:03
Thanks, that's truly mkdir wrapper, but should we still need to have
compdefas
? As I test your mkcd
function, it works well with completion.– Tuyen Pham
Jan 28 at 2:03
add a comment |
compdef
uses the following form:
compdef [ -ane ] function name
so one idea would be to point the name
of mcd
at the _mkdir
function via the following command:
compdef _mkdir mcd
however this is slightly incorrect as mkdir
completes various flags that should probably not also be given to cd
. More direct would be to complete on directories:
compdef _directories mcd
add a comment |
compdef
uses the following form:
compdef [ -ane ] function name
so one idea would be to point the name
of mcd
at the _mkdir
function via the following command:
compdef _mkdir mcd
however this is slightly incorrect as mkdir
completes various flags that should probably not also be given to cd
. More direct would be to complete on directories:
compdef _directories mcd
add a comment |
compdef
uses the following form:
compdef [ -ane ] function name
so one idea would be to point the name
of mcd
at the _mkdir
function via the following command:
compdef _mkdir mcd
however this is slightly incorrect as mkdir
completes various flags that should probably not also be given to cd
. More direct would be to complete on directories:
compdef _directories mcd
compdef
uses the following form:
compdef [ -ane ] function name
so one idea would be to point the name
of mcd
at the _mkdir
function via the following command:
compdef _mkdir mcd
however this is slightly incorrect as mkdir
completes various flags that should probably not also be given to cd
. More direct would be to complete on directories:
compdef _directories mcd
answered Jan 24 at 5:26
thrigthrig
24.9k23157
24.9k23157
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496379%2ftreat-command-like-another-for-completion-purposes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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