首要一點:shell有多種,比如bash、zsh、csh、ksh、sh、tcsh等
因此,制作自動補全功能時,要先搞清楚,你使用的是哪種shell,各個shell制作方法是不同的,網上大部分介紹的是關於bash的。
定義補全腳本后,要重新打開終端或者先執行下腳本:source
一、bash:
涉及命令:
補全命令complete、篩選命令compgen、修改補全命令設置compopt
內置變量:
通過這些變量,可以獲得當前命令的內容、位置等信息,以便判斷下面應該出現的提示命令的內容
除了上面三個命令外,Bash還有幾個內置變量來輔助補全功能,如下:
variable | description |
---|---|
COMP_WORDS | 類型為數組,存放當前命令行中輸入的所有單詞 |
COMP_CWORD | 類型為整數,當前輸入的單詞在COMP_WORDS中的索引 |
COMPREPLY | 類型為數組,候選的補全結果 |
COMP_WORDBREAKS | 類型為字符串,表示單詞之間的分隔符 |
COMP_LINE | 類型為字符串,表示當前的命令行輸入字符 |
COMP_POINT | 類型為整數,表示光標在當前命令行的哪個位置 |
幫助信息:
man complete
補全腳本位置(Mac):
每打開一個新的shell,都會把這個目錄下的腳本執行一遍
/usr/local/etc/bash_completion.d
代碼解讀:
_god() { COMPREPLY=() # 為數組,名字必須是COMPREPLY,在給COMPREPLY賦值之前,最好將它重置清空,避免被其它補全函數干擾 local cur prev _get_comp_words_by_ref cur prev COMPREPLY=( $( compgen -W 'xiaomi noops blog' -- "$cur" ) ) return 0 } complete -F _god god
_god() { COMPREPLY=(xiaomi noops blog) return 0 } complete -F _god god
查看已經有的命令補全:complete
最簡單的使用方式:complete -W "192.168.1.1 192.168.1.2" ssh
這樣在輸入ssh 后,按tab可以提示+補全IP地址
參考:
https://mp.weixin.qq.com/s/nSje0zhcP2vAmBAH05g64w
http://noops.me/?p=1114
https://linux.cn/article-6301-1.html
二、zsh:
涉及命令:compctl
內置變量:沒有bash的那些內置的變量,但是有個內置read函數,可以獲取當前出現指令集、當前的指令內容、前一個指令內容等信息。
幫助信息:
通過這條命令查找zsh的內置函數,找到read的用法:
man zshbuiltins
read函數所用參數解釋:
-A The first name is taken as the name of an array and all words are assigned to it. -n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one. -c -l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the current command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
man zsh
補全腳本位置(Mac):每個新的shell終端都會把這個腳本執行一遍,所以內容太多,一是占用內存,而是新的terminal啟動慢
~/.zshrc
代碼解讀:執行下面這段代碼,終端執行proxy命令,就可以看到提示信息
_proxy(){ local cur cword words # 定義變量,cur表示當前光標下的單詞 read -cn cword # 所有指令集 read -Ac words # 當前指令的索引值 cur="$words[$cword-1]" # 當前指令值 if [ $cur = "proxy" ] # 根據當前不同指令返回對應的提示信息 then reply=(start stop restart connect route help) # 必須是值reply,返回提示信息內容 elif [ $cur = "route" ] then reply=(add delete) else reply=() fi } compctl -K _proxy proxy # -K 表示使用函數
參考:
https://github.com/johan/zsh/blob/master/Misc/compctl-examples
在.zshrc里加入如下語句:意思大概是自動加載fpath路徑下的各種函數,比如自動補全函數。
fpath=(/usr/local/share/zsh-completions $fpath)
autoload -Uz compinit && compinit -u
autoload -U +X bashcompinit && bashcompinit
另外autoload/compinit/bashcompinit是zsh的builtin函數,可以使用man zshbuiltins查看幫助
三、brew search comple
這種模糊搜索可以查到很多工具的命令自動補全腳本:比如pip、bash、docker、zsh等
brew info bash-completion查看安裝信息
四、python shell的自動補全
五、zsh啟動優化
查看zsh啟動時間:2.37s,還是很長的,是否可以優化到0.x秒,這樣感覺就沒有卡頓
time zsh -i -c exit
zsh -i -c exit 2.37s user 1.25s system 98% cpu 3.663 total