Managing trailing characters using zsh autoloaded completion
up vote
2
down vote
favorite
I'm trying to create an autoloaded completion file for zsh. To do so, I've added a _myprog
file in a folder that is in my fpath.
This file goes roughly like this:
#compdef myprog
_myprog () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:myprog-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
list|ls)
_arguments : "--limit[Max depth]" "--folders[Print flat list of folders]"
_describe -t commands "myprog list" subcommands
;;
insert)
_arguments : "--force[Overwrite file]" "--append[Append to existing data]"
_describe -t commands "myprog insert" subcommands
_myprog_complete_folders
;;
esac
else
local -a subcommands
subcommands=(
"insert:Insert a new file"
"list:List existing files"
)
_describe -t command 'myprog' subcommands
_arguments : "--yes[Assume yes]"
_myprog_complete_files
fi
}
_myprog_complete_files () {
_values 'files' $(myprog ls)
}
_myprog_complete_folders () {
_values 'folders' $(myprog ls --folders)
}
_myprog
So, my program has two scenarios where I want auto-completion:
When used without subcommand, I'd like to specify a path in a remote location. This is done by the function
_myprog_complete_files
thanks to the_value
function. The file list is provided by runningmyprog ls
, which prints each remote file on a new line.When inserting a new remote file, I'd like to be able to autocomplete the folders name, which is done in the function
_myprog_complete_folders
using_value
too, but this time the folder list is generated usingmyprog ls --folder
instead, which only prints folders.
So far so good... Excepted that zsh is inserting a space after the folder name when I'm using <tab><tab>
to have the value list displayed and I'm trying to type the file name after the folder name.
For example:
$ myprog insert web<tab><tab><tab>
-- folders --
web/ web/foo/ web/bar/
is supposed to select web/foo
and it effectively does so and autocomplete to myprog insert web/foo/
but notice the trailing space! So if I'm trying to type directly the name of the file I want to insert once I've selected the folder I wanted using <tab>
, say I want to call it baz
, I'm ending up with myprog insert web/foo/ baz
which is not my goal at all.
I've tried browsing through the zsh doc, without success.
I've for example tried to set zstyle ':completion::complete:myprog-insert:' add-space false
to no avail, both in my .zshrc and in my _myprog
file. I couldn't even see a difference when having it set or not.
I'm guessing I must be missing something, since it seems to be a desirable feature to be able to type directly after autocompletion in order to specify a parameter or so.
zsh autocomplete
New contributor
add a comment |
up vote
2
down vote
favorite
I'm trying to create an autoloaded completion file for zsh. To do so, I've added a _myprog
file in a folder that is in my fpath.
This file goes roughly like this:
#compdef myprog
_myprog () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:myprog-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
list|ls)
_arguments : "--limit[Max depth]" "--folders[Print flat list of folders]"
_describe -t commands "myprog list" subcommands
;;
insert)
_arguments : "--force[Overwrite file]" "--append[Append to existing data]"
_describe -t commands "myprog insert" subcommands
_myprog_complete_folders
;;
esac
else
local -a subcommands
subcommands=(
"insert:Insert a new file"
"list:List existing files"
)
_describe -t command 'myprog' subcommands
_arguments : "--yes[Assume yes]"
_myprog_complete_files
fi
}
_myprog_complete_files () {
_values 'files' $(myprog ls)
}
_myprog_complete_folders () {
_values 'folders' $(myprog ls --folders)
}
_myprog
So, my program has two scenarios where I want auto-completion:
When used without subcommand, I'd like to specify a path in a remote location. This is done by the function
_myprog_complete_files
thanks to the_value
function. The file list is provided by runningmyprog ls
, which prints each remote file on a new line.When inserting a new remote file, I'd like to be able to autocomplete the folders name, which is done in the function
_myprog_complete_folders
using_value
too, but this time the folder list is generated usingmyprog ls --folder
instead, which only prints folders.
So far so good... Excepted that zsh is inserting a space after the folder name when I'm using <tab><tab>
to have the value list displayed and I'm trying to type the file name after the folder name.
For example:
$ myprog insert web<tab><tab><tab>
-- folders --
web/ web/foo/ web/bar/
is supposed to select web/foo
and it effectively does so and autocomplete to myprog insert web/foo/
but notice the trailing space! So if I'm trying to type directly the name of the file I want to insert once I've selected the folder I wanted using <tab>
, say I want to call it baz
, I'm ending up with myprog insert web/foo/ baz
which is not my goal at all.
I've tried browsing through the zsh doc, without success.
I've for example tried to set zstyle ':completion::complete:myprog-insert:' add-space false
to no avail, both in my .zshrc and in my _myprog
file. I couldn't even see a difference when having it set or not.
I'm guessing I must be missing something, since it seems to be a desirable feature to be able to type directly after autocompletion in order to specify a parameter or so.
zsh autocomplete
New contributor
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to create an autoloaded completion file for zsh. To do so, I've added a _myprog
file in a folder that is in my fpath.
This file goes roughly like this:
#compdef myprog
_myprog () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:myprog-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
list|ls)
_arguments : "--limit[Max depth]" "--folders[Print flat list of folders]"
_describe -t commands "myprog list" subcommands
;;
insert)
_arguments : "--force[Overwrite file]" "--append[Append to existing data]"
_describe -t commands "myprog insert" subcommands
_myprog_complete_folders
;;
esac
else
local -a subcommands
subcommands=(
"insert:Insert a new file"
"list:List existing files"
)
_describe -t command 'myprog' subcommands
_arguments : "--yes[Assume yes]"
_myprog_complete_files
fi
}
_myprog_complete_files () {
_values 'files' $(myprog ls)
}
_myprog_complete_folders () {
_values 'folders' $(myprog ls --folders)
}
_myprog
So, my program has two scenarios where I want auto-completion:
When used without subcommand, I'd like to specify a path in a remote location. This is done by the function
_myprog_complete_files
thanks to the_value
function. The file list is provided by runningmyprog ls
, which prints each remote file on a new line.When inserting a new remote file, I'd like to be able to autocomplete the folders name, which is done in the function
_myprog_complete_folders
using_value
too, but this time the folder list is generated usingmyprog ls --folder
instead, which only prints folders.
So far so good... Excepted that zsh is inserting a space after the folder name when I'm using <tab><tab>
to have the value list displayed and I'm trying to type the file name after the folder name.
For example:
$ myprog insert web<tab><tab><tab>
-- folders --
web/ web/foo/ web/bar/
is supposed to select web/foo
and it effectively does so and autocomplete to myprog insert web/foo/
but notice the trailing space! So if I'm trying to type directly the name of the file I want to insert once I've selected the folder I wanted using <tab>
, say I want to call it baz
, I'm ending up with myprog insert web/foo/ baz
which is not my goal at all.
I've tried browsing through the zsh doc, without success.
I've for example tried to set zstyle ':completion::complete:myprog-insert:' add-space false
to no avail, both in my .zshrc and in my _myprog
file. I couldn't even see a difference when having it set or not.
I'm guessing I must be missing something, since it seems to be a desirable feature to be able to type directly after autocompletion in order to specify a parameter or so.
zsh autocomplete
New contributor
I'm trying to create an autoloaded completion file for zsh. To do so, I've added a _myprog
file in a folder that is in my fpath.
This file goes roughly like this:
#compdef myprog
_myprog () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:myprog-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
list|ls)
_arguments : "--limit[Max depth]" "--folders[Print flat list of folders]"
_describe -t commands "myprog list" subcommands
;;
insert)
_arguments : "--force[Overwrite file]" "--append[Append to existing data]"
_describe -t commands "myprog insert" subcommands
_myprog_complete_folders
;;
esac
else
local -a subcommands
subcommands=(
"insert:Insert a new file"
"list:List existing files"
)
_describe -t command 'myprog' subcommands
_arguments : "--yes[Assume yes]"
_myprog_complete_files
fi
}
_myprog_complete_files () {
_values 'files' $(myprog ls)
}
_myprog_complete_folders () {
_values 'folders' $(myprog ls --folders)
}
_myprog
So, my program has two scenarios where I want auto-completion:
When used without subcommand, I'd like to specify a path in a remote location. This is done by the function
_myprog_complete_files
thanks to the_value
function. The file list is provided by runningmyprog ls
, which prints each remote file on a new line.When inserting a new remote file, I'd like to be able to autocomplete the folders name, which is done in the function
_myprog_complete_folders
using_value
too, but this time the folder list is generated usingmyprog ls --folder
instead, which only prints folders.
So far so good... Excepted that zsh is inserting a space after the folder name when I'm using <tab><tab>
to have the value list displayed and I'm trying to type the file name after the folder name.
For example:
$ myprog insert web<tab><tab><tab>
-- folders --
web/ web/foo/ web/bar/
is supposed to select web/foo
and it effectively does so and autocomplete to myprog insert web/foo/
but notice the trailing space! So if I'm trying to type directly the name of the file I want to insert once I've selected the folder I wanted using <tab>
, say I want to call it baz
, I'm ending up with myprog insert web/foo/ baz
which is not my goal at all.
I've tried browsing through the zsh doc, without success.
I've for example tried to set zstyle ':completion::complete:myprog-insert:' add-space false
to no avail, both in my .zshrc and in my _myprog
file. I couldn't even see a difference when having it set or not.
I'm guessing I must be missing something, since it seems to be a desirable feature to be able to type directly after autocompletion in order to specify a parameter or so.
zsh autocomplete
zsh autocomplete
New contributor
New contributor
edited Nov 17 at 19:59
New contributor
asked Nov 16 at 19:55
Lery
1134
1134
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The add-space
code in $fpath[-1]/_expand
does not appear to be involved with your completion via _values
; this can be checked by setting add-space
to false everywhere, or by calling _complete_debug
and seeing what code the completion went through:
% bindkey -M viins "^t" _complete_debug
% foo
Trace output left in /tmp/zsh438foo2 (up-history to view)
One solution is to use the -s ...
feature of _values
, which can optionally insert a separator that is normally used to join multiple values, and then to disable that feature if the separator has been used:
#compdef foo
local curcontext="$curcontext" state line
choices=(aaa bbb ccc)
_arguments '1:dir:->folders' && return 0
case "$state" in
folders)
if ! compset -P '*/'; then
_values -s / folders $choices
fi
;;
esac
Which on a foo a
tab should append a slash, and then since /
now appears the compset
test should prevent additional completions.
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using-S
(which didn't work) but didn't try using-s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)
– Lery
2 days ago
1
@Lery figured it out,compset
can look for the/
and based on that disable completion
– thrig
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The add-space
code in $fpath[-1]/_expand
does not appear to be involved with your completion via _values
; this can be checked by setting add-space
to false everywhere, or by calling _complete_debug
and seeing what code the completion went through:
% bindkey -M viins "^t" _complete_debug
% foo
Trace output left in /tmp/zsh438foo2 (up-history to view)
One solution is to use the -s ...
feature of _values
, which can optionally insert a separator that is normally used to join multiple values, and then to disable that feature if the separator has been used:
#compdef foo
local curcontext="$curcontext" state line
choices=(aaa bbb ccc)
_arguments '1:dir:->folders' && return 0
case "$state" in
folders)
if ! compset -P '*/'; then
_values -s / folders $choices
fi
;;
esac
Which on a foo a
tab should append a slash, and then since /
now appears the compset
test should prevent additional completions.
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using-S
(which didn't work) but didn't try using-s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)
– Lery
2 days ago
1
@Lery figured it out,compset
can look for the/
and based on that disable completion
– thrig
2 days ago
add a comment |
up vote
2
down vote
accepted
The add-space
code in $fpath[-1]/_expand
does not appear to be involved with your completion via _values
; this can be checked by setting add-space
to false everywhere, or by calling _complete_debug
and seeing what code the completion went through:
% bindkey -M viins "^t" _complete_debug
% foo
Trace output left in /tmp/zsh438foo2 (up-history to view)
One solution is to use the -s ...
feature of _values
, which can optionally insert a separator that is normally used to join multiple values, and then to disable that feature if the separator has been used:
#compdef foo
local curcontext="$curcontext" state line
choices=(aaa bbb ccc)
_arguments '1:dir:->folders' && return 0
case "$state" in
folders)
if ! compset -P '*/'; then
_values -s / folders $choices
fi
;;
esac
Which on a foo a
tab should append a slash, and then since /
now appears the compset
test should prevent additional completions.
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using-S
(which didn't work) but didn't try using-s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)
– Lery
2 days ago
1
@Lery figured it out,compset
can look for the/
and based on that disable completion
– thrig
2 days ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The add-space
code in $fpath[-1]/_expand
does not appear to be involved with your completion via _values
; this can be checked by setting add-space
to false everywhere, or by calling _complete_debug
and seeing what code the completion went through:
% bindkey -M viins "^t" _complete_debug
% foo
Trace output left in /tmp/zsh438foo2 (up-history to view)
One solution is to use the -s ...
feature of _values
, which can optionally insert a separator that is normally used to join multiple values, and then to disable that feature if the separator has been used:
#compdef foo
local curcontext="$curcontext" state line
choices=(aaa bbb ccc)
_arguments '1:dir:->folders' && return 0
case "$state" in
folders)
if ! compset -P '*/'; then
_values -s / folders $choices
fi
;;
esac
Which on a foo a
tab should append a slash, and then since /
now appears the compset
test should prevent additional completions.
The add-space
code in $fpath[-1]/_expand
does not appear to be involved with your completion via _values
; this can be checked by setting add-space
to false everywhere, or by calling _complete_debug
and seeing what code the completion went through:
% bindkey -M viins "^t" _complete_debug
% foo
Trace output left in /tmp/zsh438foo2 (up-history to view)
One solution is to use the -s ...
feature of _values
, which can optionally insert a separator that is normally used to join multiple values, and then to disable that feature if the separator has been used:
#compdef foo
local curcontext="$curcontext" state line
choices=(aaa bbb ccc)
_arguments '1:dir:->folders' && return 0
case "$state" in
folders)
if ! compset -P '*/'; then
_values -s / folders $choices
fi
;;
esac
Which on a foo a
tab should append a slash, and then since /
now appears the compset
test should prevent additional completions.
edited 2 days ago
answered Nov 17 at 15:31
thrig
23.6k12955
23.6k12955
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using-S
(which didn't work) but didn't try using-s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)
– Lery
2 days ago
1
@Lery figured it out,compset
can look for the/
and based on that disable completion
– thrig
2 days ago
add a comment |
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using-S
(which didn't work) but didn't try using-s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)
– Lery
2 days ago
1
@Lery figured it out,compset
can look for the/
and based on that disable completion
– thrig
2 days ago
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using
-S
(which didn't work) but didn't try using -s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)– Lery
2 days ago
While this is effectively not the ideal way to do it, since it keeps trying to complete after typing stuff, it does effectively solve my problem. I tried using
-S
(which didn't work) but didn't try using -s
, since it refused the empty separator... I should have tried using slashes directly (they were currently in my _values already, so I had to remove the trailing slashes for this method to work.)– Lery
2 days ago
1
1
@Lery figured it out,
compset
can look for the /
and based on that disable completion– thrig
2 days ago
@Lery figured it out,
compset
can look for the /
and based on that disable completion– thrig
2 days ago
add a comment |
Lery is a new contributor. Be nice, and check out our Code of Conduct.
Lery is a new contributor. Be nice, and check out our Code of Conduct.
Lery is a new contributor. Be nice, and check out our Code of Conduct.
Lery is a new contributor. Be nice, and check out our Code of Conduct.
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%2f482231%2fmanaging-trailing-characters-using-zsh-autoloaded-completion%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