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

Re: How to complete alias after alias of sudo?



On Thu, 1 Jun 2017 13:43:20 +0800
Han Pingtian <hanpt@xxxxxxxxxxxxxxxxxx> wrote:
> Looks like if change the line 52 of _sudo from
> 
>     52     '(-)1:command: _command_names -e'
> 
> to
> 
>     52     '(-)1:command: _command_names'
> 
> then alias will be listed. But looks like it's not a good idea to
> complete alias after sudo?

With -s or -i (which is what you said yesterday, and it went slightly
over my head, so I missed the point), it is executing a shell command
line.  Then actually I think there *is* an argument for making the
completion more complete. I'm not sure how good the argument is, though
--- the -s and -i arguments are potentially dangerous and ideally the
shell being run is a pruned version that may not have the same aliases
defined.  That said, what you complete isn't determining how safe your
sudo environment is anyway, so maybe this is a non-issue.

Anyway, we could do this.

pws

diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo
index aa7a1a4..0b09ded 100644
--- a/Completion/Unix/Command/_sudo
+++ b/Completion/Unix/Command/_sudo
@@ -41,6 +41,8 @@ if [[ $service = sudoedit ]] || (( $words[(i)-e] < $words[(i)^(*sudo|-[^-]*)] ))
   args=( -A "-*" $args '!(-V --version -h --help)-e' '*:file:_files' )
 else
   cmd="$words[1]"
+  local ext
+  (( ${words[(I)-[is]]} == 0 )) && ext=" -e"
   args+=(
     '(-e --edit 1 *)'{-e,--edit}'[edit files instead of running a command]' \
     '(-s --shell)'{-s,--shell}'[run shell as the target user; a command may also be specified]' \
@@ -49,7 +51,7 @@ else
     '(-E --preserve-env -i --login -s --shell -e --edit)'{-E,--preserve-env}'[preserve user environment when running command]' \
     '(-H --set-home -i --login -s --shell -e --edit)'{-H,--set-home}"[set HOME variable to target user's home dir]" \
     '(-P --preserve-groups -i -login -s --shell -e --edit)'{-P,--preserve-groups}"[preserve group vector instead of setting to target's]" \
-    '(-)1:command: _command_names -e'
+    "(-)1:command: _command_names$ext"
     '*::arguments:{ _comp_priv_prefix=( $cmd -n ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} ) ; _normal }'
   )
 fi



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