zsh completion for umount: limit completion to one argument

when I use zsh completion for the umount command:

umount /mnt/t<TAB>

it completes the mounted filesystem /mnt/tmp.

But when I accidentally press tab too many times, it tries to be too helpful, and suggests other mountpoints, which leads to nonsense completions such as:

umount /mnt/tmp /

how can I limit umount completion to one mountpoint only, so that further tabs will not add any further arguments?

I think I have located the relevant section in /usr/share/zsh/functions/Completion/Unix/_mount:

  # Completion for umount.
      local excl='-V --version -h --help'
    '(- *)'{-h,--help}'[show help]'
    '(- *)'{-V,--version}'[show version]'
    "($excl -v --verbose)"{-v,--verbose}'[verbose mode]'
    "($excl -n --no-mtab)"{-n,--no-mtab}"[don't write /etc/mtab]"
    "($excl)-r[remount read-only on failure]"
    "($excl)-d[for loopback mount, free loop device]"
    "($excl)-i[don't call /sbin/umount.<fs> helper]"
    "($excl *)-a[unmount all file systems from /etc/mtab]"
    "($excl)-t[specify file system type]:file system type:_file_systems"
    "($excl *)-O[with -a, restrict filesystems by options]:file system option:->fsopt"
    "($excl)-f[force unmount]"
    "($excl)-l[lazy unmount]"
    '*:dev or dir:->udevordir'

  _arguments -C -s "$args[@]" && ret=0

what do I need to modify?

Asked By: Martin Vegter


_arguments is a complicated beast… The part that handles non-option arguments is

    '*:dev or dir:->udevordir'

This is an instance of *:message:action in the manual, with * meaning that it applies to all non-option arguments. Replace * by 1, which applies only to the first non-option argument (an instance of n:message:action).

    '1:dev or dir:->udevordir'

I guess zsh’s completion is more liberal because umount accepts multiple filesystems on some platforms.

To also stop completing options after the filesystem, pass -A "-*" to _arguments, i.e. change its invocation to

  _arguments -C -A "-*" -s "$args[@]" && ret=0
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.