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?
2 Answers 2
local
is a builtin, not a keyword, solocal 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 initializefiles
to the empty array or declare the variable withtypeset -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: putemulate -L ksh
at the beginning of the function. - Unless you go the
emulate
route, your_notes
function will printzsh: no matches found: foo*
if there is no completion forfoo
, because by default non-matching globs trigger an error. Add the glob qualifierN
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 typen b<TAB>
,COMPREPLY
is set to containb
, notfoo/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
-
Thanks Gilles. With a minor edit, the zsh-ported n() function works; but the _notes() function still fails with no relevant matches. The emulation doesn't work at all (opens the Vim file browser). Do I need some other configuration in my
.zshrc
? I feel, given the quality of the two answers, that I must be missing something.jasonwryan– jasonwryan2013年01月01日 01:52:29 +00:00Commented Jan 1, 2013 at 1:52 -
@jasonwryan I tested
n
and running_notes
manually (I didn't test with bash completion emulation) inzsh -f
, and both seem to work. Post a trace of runningn
and_notes
withset -x
.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2013年01月01日 02:01:19 +00:00Commented Jan 1, 2013 at 2:01 -
Trace file: I am not sure it is that helpful... Although it does show the array picking up all the files; the Tab completion still fails.jasonwryan– jasonwryan2013年01月01日 02:33:06 +00:00Commented Jan 1, 2013 at 2:33
-
@jasonwryan The trace from
n
looks correct, what's wrong with it?_notes
looks at its second argument, so you need to test it with_notes _ irc
.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2013年01月01日 02:36:33 +00:00Commented Jan 1, 2013 at 2:36 -
2"Zsh arrays are numbered from 1, not from 0 like in bash and ksh, so ${files[0]} must be written $files[1]" Oh, the horror ;(yPhil– yPhil2016年03月12日 02:38:26 +00:00Commented Mar 12, 2016 at 2:38
zsh's typeset(local) command can't define arrays with it's syntax. You can create arrays, but you can't also set values all in one command.
function n() {
local arg files; for arg; do files+=( ~/.notes/$arg ); done
vim ${files[@]}
}
is one way to fix it.
-
Upvoted: thank you. Any suggestion as to how to get the completion script to work in zsh?jasonwryan– jasonwryan2012年12月31日 02:46:53 +00:00Commented Dec 31, 2012 at 2:46
-
the n function could also read
n() { $EDITOR "${@/#/.notes/}"; }
and prevent a loop, while working in bash and zshllua– llua2013年01月09日 03:45:01 +00:00Commented Jan 9, 2013 at 3:45