Skip to main content
edited body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function generates 
    # completions for myfunc."
    # The combination of that line, plus the file name
    # starting with an `_`, plus having this file's 
    # parent dir in your `$fpath`, ensures this file 
    # will be autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get 
    # loaded immediately, even if you have not called 
    # this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef="$( type -f myfunc )"
    
    # Get the part that matches `case*esac`, then split
    # it on whitespace and put the resulting words in an 
    # array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end 
    # with `)`.
    # Even if you used the `case` syntax with only the 
    # closing `)`s, `type -f` will show your cases with
    # both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, 
    # that is, you are 
    #       allowed to specify `myfunc -rm`.
    # If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any 
    # position.
    _arguments -s \
        {-r,--readonly}'[description for "readonly"]' \
        {-m,--mount}'[description for "mount"]' \
        "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function generates 
    # completions for myfunc."
    # The combination of that line, plus the file name
    # starting with an `_`, plus having this file's 
    # parent dir in your `$fpath`, ensures this file 
    # will be autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get 
    # loaded immediately, even if you have not called 
    # this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef="$( type -f myfunc )"
    
    # Get the part that matches `case*esac`, then split
    # it on whitespace and put the resulting words in an 
    # array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end 
    # with `)`.
    # Even if you used the `case` syntax with only the 
    # closing `)`s, `type -f` will show your cases with
    # both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function generates 
    # completions for myfunc."
    # The combination of that line, plus the file name
    # starting with an `_`, plus having this file's 
    # parent dir in your `$fpath`, ensures this file 
    # will be autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get 
    # loaded immediately, even if you have not called 
    # this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef="$( type -f myfunc )"
    
    # Get the part that matches `case*esac`, then split
    # it on whitespace and put the resulting words in an 
    # array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end 
    # with `)`.
    # Even if you used the `case` syntax with only the 
    # closing `)`s, `type -f` will show your cases with
    # both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, 
    # that is, you are allowed to specify `myfunc -rm`.
    # If not, remove the `-s` option.
    # `*:`: Let this argument be completed in any 
    # position.
    _arguments -s \
        {-r,--readonly}'[description for "readonly"]' \
        {-m,--mount}'[description for "mount"]' \
        "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
deleted 8 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in its own file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/Functions, add this to your ~/.zshrc file:
    fpath+=( ~/Functions )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code 
    # less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == 
    #    `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function definesgenerates 
    # completions for myfunc."
    # The combination of that line, plus the file name
    # starting with an 
    # `_`, plus having this file's 
    # parent dir in your `$fpath`, ensures 
    # this file 
    # will getbe autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get 
    # loaded immediately, 
    # even if you have not called 
    # this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef=$funcdef="$( type -f myfunc )"
    
    # Get the part that matches `case*esac`, then split
    # it on whitespace 
    # and put the resulting words in an 
    # array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end 
    # with `)`.
    # Even if you used the `case` syntax with only the 
    # closing `)`s,
    # in `type -f`, yourwill casesshow willyour appearcases with
    # both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in its own file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/Functions, add this to your ~/.zshrc file:
    fpath+=( ~/Functions )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function defines completions for myfunc."
    # The combination of that line, plus the file name starting with an 
    # `_`, plus having this file's parent dir in your `$fpath`, ensures 
    # this file will get autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get loaded immediately, 
    # even if you have not called this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef=$(type -f myfunc)
    
    # Get the part that matches `case*esac`, then split it on whitespace 
    # and put the resulting words in an array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end with `)`.
    # Even if you used the `case` syntax with only the closing `)`s,
    # in `type -f`, your cases will appear with both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in its own file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/Functions, add this to your ~/.zshrc file:
    fpath+=( ~/Functions )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code 
    # less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == 
    #    `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function generates 
    # completions for myfunc."
    # The combination of that line, plus the file name
    # starting with an `_`, plus having this file's 
    # parent dir in your `$fpath`, ensures this file 
    # will be autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get 
    # loaded immediately, even if you have not called 
    # this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef="$( type -f myfunc )"
    
    # Get the part that matches `case*esac`, then split
    # it on whitespace and put the resulting words in an 
    # array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end 
    # with `)`.
    # Even if you used the `case` syntax with only the 
    # closing `)`s, `type -f` will show your cases with
    # both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
deleted 8 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in an autoloadableits own file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/funcFunctions, add this to your ~/.zshrc file:
    fpath+=( ~/funcFunctions )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put in tointo this file:
    #compdef myfunc
    
    # The line above means "This function defines completions for myfunc."
    # The combination of that line, plus the file name starting with an 
    # `_`, plus having this file's parent dir in your `$fpath`, ensures 
    # this file will get autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get loaded immediately, 
    # even if you have not called this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef=$(type -f myfunc)
    
    # Get the part that matches `case*esac`, then split it on whitespace 
    # and put the resulting words in an array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end with `)`.
    # Even if you used the `case` syntax with only the closing `)`s,
    # in `type -f`, your cases will appear with both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in an autoloadable file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/func, add this to your ~/.zshrc file:
    fpath+=( ~/func )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put in to this file:
    #compdef myfunc
    
    # The line above means "This function defines completions for myfunc."
    # The combination of that line, plus the file name starting with an 
    # `_`, plus having this file's parent dir in your `$fpath`, ensures 
    # this file will get autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get loaded immediately, 
    # even if you have not called this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef=$(type -f myfunc)
    
    # Get the part that matches `case*esac`, then split it on whitespace 
    # and put the resulting words in an array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end with `)`.
    # Even if you used the `case` syntax with only the closing `)`s,
    # in `type -f`, your cases will appear with both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
  1. Put your function in a file called myfunc. Note:
    • This does not end in .zsh or .sh.
    • When you put a function in its own file, you do not need to add funcname() {…} boilerplate.
  2. Make sure that the dir containing the file myfunc is in your $fpath. For example, if the file myfunc is located in ~/Functions, add this to your ~/.zshrc file:
    fpath+=( ~/Functions )
    
  3. Finally, autoload myfunc in your ~/.zshrc file:
    # We pass a couple of options that make the code less likely to break:
    # -U suppresses alias expansion
    # -z marks the function for zsh-style autoloading == `unsetopt KSH_AUTOLOAD`
    autoload -Uz myfunc
    
  1. Create a file called _myfunc.
  2. Put into this file:
    #compdef myfunc
    
    # The line above means "This function defines completions for myfunc."
    # The combination of that line, plus the file name starting with an 
    # `_`, plus having this file's parent dir in your `$fpath`, ensures 
    # this file will get autoloaded when you call `compinit`.
    
    # `+X` makes sure `myfunc`'s definition will get loaded immediately, 
    # even if you have not called this function yet.
    autoload +X -Uz myfunc
    
    # Get the definition of `myfunc` in string form.
    local funcdef=$(type -f myfunc)
    
    # Get the part that matches `case*esac`, then split it on whitespace 
    # and put the resulting words in an array.
    local -a words=( ${=funcdef[(r)case,(r)esac]} )
    
    # Keep only the words that start with `(` and end with `)`.
    # Even if you used the `case` syntax with only the closing `)`s,
    # in `type -f`, your cases will appear with both `(` and `)`.
    local -a required=( ${(M)words:#'('*')'} )
    
    # `-s`: Allow options to `myfunc ` to be stacked, that is, you are 
    #       allowed to specify `myfunc -rm`. If not, remove the `-s` 
    #       option.
    # `*:`: Let this argument be completed in any position.
    _arguments -s \
      {-r,--readonly}'[description for "readonly"]' \
      {-m,--mount}'[description for "mount"]' \
      "*:required argument:( ${required//[()]/}  )"
    
    • Replace required argument with whatever you want to call your argument.
    • Fill out descriptions for the options.
  3. Again, make sure the directory in which this file is located is in your $fpath.
  4. Make sure you do autoload -Uz compinit; compinit in your .zshrc file and make sure it runs after the dir above has been added to your $fpath.
  5. Restart your shell with exec zsh or close your terminal window and open a new one.
deleted 8 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
deleted 9 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
deleted 6 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
Clean up some details
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
deleted 7 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
added 274 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
added 9 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
Correct `#compdef _myfunc` to `#compdef myfunc`
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
added 1 character in body; deleted 7 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
added 1 character in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
added 569 characters in body
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading
Source Link
Marlon Richert
  • 4.3k
  • 11
  • 38
Loading