Shell – Bash function not working in Zsh

functionshellzsh

I have been slowly migrating from Bash to Zsh and have got to the point where everything I have moved across is working well, with one exception.

I have a couple of functions in my .bashrc that I use dozens of times a day and two of them do not work under Zsh. The three functions comprise a basic note taking facility.

They are currently in .config/zsh/functions:

function n() { 
local arg files=(); for arg; do files+=( ~/".notes/$arg" ); done
${EDITOR:-vi} "${files[@]}" 
}

function nls() {
tree -CR --noreport $HOME/.notes | awk '{ 
    if (NF==1) print $1; 
    else if (NF==2) print $2; 
    else if (NF==3) printf "  %s\n", $3 
    }'
}

# TAB completion for notes
function _notes() {
local files=($HOME/.notes/**/"$2"*)
    [[ -e ${files[0]} ]] && COMPREPLY=( "${files[@]##~/.notes/}" )
}
complete -o default -F _notes n

Which I source from .zshrc like so:

autoload bashcompinit
bashcompinit
# source zshrc functions file
source "$HOME/.config/zsh/functions"

nls works as expected, but neither n nor Tab completion work.

I read man zshcompsys where it says:

The function bashcompinit provides compatibility with bash's programmable completion system. When run it will define the functions, compgen and complete which correspond to the bash builtins with the same names. It will then be possible to use completion specifications and functions written for bash.

However, when I try Tab completion, nothing happens and when I enter n notename, Vim opens my /home in file browser mode – not quite the expected behaviour.

All of the other functions defined work well. How do I migrate these functions to work under Zsh?

Best Answer

  • local is a builtin, not a keyword, so local files=(…) isn't parsed as an array assignment but as a string assignment. Write the assignment separately from the declaration. (Already found by llua, but note that you need to initialize files to the empty array or declare the variable with typeset -a, otherwise the array starts with a spurious empty element.)
  • Zsh arrays are numbered from 1, not from 0 like in bash and ksh, so ${files[0]} must be written $files[1]. Alternatively, tell zsh to behave in a way that's more compatible with ksh and bash: put emulate -L ksh at the beginning of the function.
  • Unless you go the emulate route, your _notes function will print zsh: no matches found: foo* if there is no completion for foo, because by default non-matching globs trigger an error. Add the glob qualifier N to get an empty array if there is no match, and test whether the array is empty.
  • There is another error in your _notes function which affects notes in subdirectories: you must strip away the prefix up to the completion, so that if e.g. ~/notes/foo/bar exists and you type n b<TAB>, COMPREPLY is set to contain b, not foo/b.

If you want to keep a file that's readable by both bash and zsh:

type emulate >/dev/null 2>/dev/null || alias emulate=true
function n() {
  emulate -L ksh
  local arg; typeset -a files
  for arg; do files+=( ~/".notes/$arg" ); done
  ${EDITOR:-vi} "${files[@]}" 
}

function nls() {
  tree -CR --noreport $HOME/.notes | awk '{ 
      if (NF==1) print $1; 
      else if (NF==2) print $2; 
      else if (NF==3) printf "  %s\n", $3 
    }'
}

# TAB completion for notes
function _notes() {
  emulate -L ksh
  local x files
  files=($HOME/.notes/**/"$2"*)
  [[ -e ${files[0]} ]] || return 1
  COMPREPLY=()
  for x in "${files[@]}"; do
    COMPREPLY+=("$2${x#$HOME/.notes*/$2}")
  done
}
complete -o default -F _notes n

If you want to port your code to zsh:

function n() {
  local files
  files=(${@/#/~/.notes/})
  ${EDITOR:-vi} $files
}

function nls() {
  tree -CR --noreport $HOME/.notes | awk '{ 
      if (NF==1) print $1; 
      else if (NF==2) print $2; 
      else if (NF==3) printf "  %s\n", $3 
    }'
}

# TAB completion for notes
function _notes() {
  setopt local_options bare_glob_qual
  local files
  files=(~/.notes/**/$2*(N))
  ((#files)) && COMPREPLY=($2${^files##~/.notes*/$2})
}
complete -o default -F _notes n
Related Question