Bash command completion tips

Well, so far I just have one tip, for the OS X "defaults" command. You know, the one that requires you to use full paths, except that it requires you to drop the ".plist" suffix? This little recipe does both for you inside of tab completion.


_defaults_plists() {
  case $2 in 
    /*)
      COMPREPLY=( $(compgen -f -X '!*.plist' $2 | sed 's/.plist$//') )
    ;;
    *)
      COMPREPLY=( $(compgen -f -X '!*.plist' $2 | sed 's/.plist$//' | sed "s:^:`pwd`/:") )
    ;;
  esac
  return 0
}

complete -o plusdirs -o filenames -F _defaults_plists defaults

It completes only with filenames that end in .plist, and it drops the .plist from that filename. Also, if the path is a relative path, it automatically prepends the current directory to the beginning (except it seems a bit wonky with ".." relative paths).


Reader Comments (Experimental. Moderated, expect delays. Posts may be edited or ignored. I reserve the right to remove any or all comments, at any time.)

No comments

Add a comment


More Mac OS X Stuff


Tom Fine's Home Send Me Email