2013-04-24

解決zsh中自訂alias過的指令不能用tab補全參數

我的.zshrc都是到處抄來的,很久以前只要設定類似alias ys='yaourt -S'這樣用ys都不能補全yaourt的參數,直到最近用上了oh-my-zsh,在打開他的外掛plugins=(systemd archlinux),這兩個能補全yaourt、pacman、systemctl指令,我很好奇他是怎麼寫的,打開~/.oh-my-zsh/plugins/中的systemd/systemd.plugin.zsh及archlinux/archlinux.plugin.zsh發現明明人家也是alias沒什麼特別的,為何我原本的就不行。


好奇殺死貓,我不信邪,一部份一部關閉alias、functions、setopt、zstyle等等,終於被我找到一個設定會引起我這個問題,只要註解或刪掉setopt complete_aliases這樣自設的alias就沒問題了。

原本這個設定就有其註解其作用:# 補完動作の解釈前にエイリアス展開,我將他修改成這樣以作警惕## 不要打開 補全會不見setopt complete_aliases # 補完動作の解釈前にエイリアス展開


這是systemd.plugin.zsh,寫的好聰明要用commands或sudo_commands都不用自已在加sudo對我這個常常忘記加sudo的蛋來講相當好用。
user_commands=(
list-units is-active status show help list-unit-files
is-enabled list-jobs show-environment)

sudo_commands=(
start stop reload restart try-restart isolate kill
reset-failed enable disable reenable preset mask unmask
link load cancel set-environment unset-environment)

for c in $user_commands; do; alias sc-$c="systemctl $c"; done
for c in $sudo_commands; do; alias sc-$c="sudo systemctl $c"; done