Skip to content
Browse files

Add make-dir-complete plugin

This plugin is a dynamic directory completion generator. Given an alias
name and a directory, it creates a complete-enabled alias for the
chosen directory.

Example:

    make_dir_complete src ~/src/

This will create a command `src` that completes with the subdirectories
inside ~/src/. So, if you have something like:

    `-- src/
        |-- oh-my-zsh/
        |   `-- ...
        |-- oh-my-vim/
        |   `-- ...
        `-- whatever/
            `-- ...

Typing

    src oh<TAB>

Will give you a complete menu with "oh-my-zsh" and "oh-my-vim" as
options.

Ref: http://caioromao.com/blog/custom-directory-completion-for-zsh/
  • Loading branch information...
1 parent d05b201 commit 785145dfacb1eb21c7381cd0ebfdf4eabb1dba12 @caio committed
Showing with 39 additions and 0 deletions.
  1. +39 −0 plugins/make-dir-complete/make-dir-complete.plugin.zsh
View
39 plugins/make-dir-complete/make-dir-complete.plugin.zsh
@@ -0,0 +1,39 @@
+#!/bin/zsh
+# Plugin to dynamically create directory-based completions
+# Author: Caio Romão <github@caioromao.com>
+# Url: http://www.caioromao.com/blog/custom-directory-completion-for-zsh/
+
+# Usage:
+# $ ls ~/Desktop/
+# >>> directory1/ directory2/ directory3/ other-dir/ fileA.txt
+#
+# $ make_dir_complete newalias ~/Desktop/
+# $ newalias <TAB><TAB>
+# >>> directory1/ directory2/ directory3/ other-dir/
+# $ newalias d<TAB><TAB>
+# >>> directory1/ directory2/ directory3/
+
+make_dir_complete() {
+ if [ ${#} -ne 2 ]; then
+ echo "usage: make_dir_complete <new-alias-name> <path>" 1>&2
+ return 1
+ fi
+
+ local aliasname=$1
+
+ if type $aliasname >/dev/null 2>&1; then
+ echo "Error: can't overwrite previously defined command \"$1\"" 1>&2
+ return 1
+ fi
+ if [ ! -d $2 ]; then
+ echo "Error: \"$2\" doesn't point to a directory" 1>&2
+ fi
+
+ cd "$2" >/dev/null 2>&1
+ local dirname=$(pwd -P)
+ cd - >/dev/null 2>&1
+
+ FUNC="$aliasname () { cd $dirname/\$@ }"
+ eval $FUNC
+ compctl -/ -W $dirname $aliasname
+}

0 comments on commit 785145d

Please sign in to comment.
Something went wrong with that request. Please try again.