自动补足算法是什么_如何自定义Shell(Fish版)的自动补全规则?
默認(rèn)fish能自動(dòng)補(bǔ)全的命令已經(jīng)相當(dāng)多了,常見(jiàn)的apt-get,rpm等都沒(méi)問(wèn)題,但今天卻發(fā)現(xiàn)沒(méi)有l(wèi)susb的補(bǔ)全規(guī)則,查看了下文檔,發(fā)現(xiàn)規(guī)則比bash-completion簡(jiǎn)單不少,記錄下~
簡(jiǎn)單補(bǔ)全
1. 建立自動(dòng)補(bǔ)全規(guī)則文件
默認(rèn)自動(dòng)補(bǔ)全路徑由全局變量$fish_complete_path定義
我選取了位置/usr/share/fish/completions,在其中建立lsusb.fish文件
2. 書寫補(bǔ)全規(guī)則
先查看下lsusb有哪些選項(xiàng)
fish自帶complete命令用于定義補(bǔ)全規(guī)則
使用方法是:
complete -c 命令 -s 短選項(xiàng) -l 長(zhǎng)選項(xiàng) --description "描述"
譬如我想有l(wèi)susb的-v(–verbose)選項(xiàng)的自動(dòng)補(bǔ)全,就可以這樣寫:
complete -c lsusb -s v -l verbose --description "Increase verbosity (show descriptors)"
這里:
-c lsusb 是我希望添加補(bǔ)全的命令 -s 后接短選項(xiàng),類似-v形式
-l 后接長(zhǎng)選項(xiàng),類似--verbose形式
--description 是選項(xiàng)的解釋,可有可無(wú)
仿照例子將如下命令添加到lsusb.fish文件中
complete -c lsusb -s v -l verbose --description "Increase verbosity (show descriptors)"
complete -c lsusb -s s --description "Show only devices with specified device and/or bus numbers (in decimal)"
complete -c lsusb -s d --description "Show only devices with the specified vendor and product ID numbers (in hexadecimal)"
complete -c lsusb -s D -l device --description "Selects which device lsusb will examine"
complete -c lsusb -s t -l tree --description "Dump the physical USB device hierarchy as a tree"
complete -c lsusb -s V -l version --description "Show version of program"
complete -c lsusb -s h -l help --description "Show usage and help"
如此輸入lsusb -敲tab鍵就會(huì)自動(dòng)補(bǔ)全了
高級(jí)補(bǔ)全
一些命令不光有基于-或–如此形式的選項(xiàng)補(bǔ)全,還有自身特點(diǎn)特定的補(bǔ)全,如mount的掛載點(diǎn)補(bǔ)全,su的用戶補(bǔ)全,ssh的主機(jī)補(bǔ)全,這是怎么做到的呢? 還以lsusb為例,lsusb -s 001:001 是列出第一個(gè)bus的第一個(gè)device信息,我希望當(dāng)輸入lsusb -s 時(shí),按下tab會(huì)列出當(dāng)前主機(jī)所有bus和device讓我選擇
complete提供了-x和-a來(lái)實(shí)現(xiàn)這樣的高級(jí)補(bǔ)全
complete -x -c lsusb -s s -a '(__fish_complete_usb)' --description "Show only devices with specified device and/or bus numbers (in decimal)"
-x 告訴complete不要用tab默認(rèn)的文件補(bǔ)全,而是要用-a告訴的參數(shù)來(lái)補(bǔ)全
-a “參數(shù)列表” 是一個(gè)列表,里面是complete參數(shù)補(bǔ)全的依據(jù),這里我用__fish_complete_usb 來(lái)實(shí)時(shí)生成,而沒(méi)有寫死
函數(shù)__fish_complete_usb是由/usr/share/fish/functions/__fish_complete_usb.fish定義的,用來(lái)生成設(shè)備列表
function __fish_complete_usb
lsusb | awk '{print $2 ":" $4}'| cut -c1-7
end
如此lsusb -s就能基于總線設(shè)備號(hào)來(lái)補(bǔ)全了
補(bǔ)充:lsusb completion已經(jīng)被fish官方采納,這是我貢獻(xiàn)的第一個(gè)開(kāi)源項(xiàng)目,好高興!
總結(jié)
以上是生活随笔為你收集整理的自动补足算法是什么_如何自定义Shell(Fish版)的自动补全规则?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中国驻基里巴斯大使馆联系电中国驻基里巴斯
- 下一篇: 清理offset_关于 kafka 日志