當前位置:
首頁 > 知識 > 針對 Bash 的不完整路徑展開(補全)功能

針對 Bash 的不完整路徑展開(補全)功能

針對 Bash 的不完整路徑展開(補全)功能


編譯自: https://www.linuxuprising.com/2018/07/incomplete-path-expansion-completion.html

作者: Logix

譯者: geekpi

bash-complete-partial-path 通過添加不完整的路徑展開(類似於 Zsh)來增強 Bash(它在 Linux 上,macOS 使用 gnu-sed,Windows 使用 MSYS)中的路徑補全。如果你想在 Bash 中使用這個省時特性,而不必切換到 Zsh,它將非常有用。

這是它如何工作的。當按下 Tab 鍵時,bash-complete-partial-path 假定每個部分都不完整並嘗試展開它。假設你要進入 /usr/share/applications 。你可以輸入 cd /u/s/app,按下 Tab,bash-complete-partial-path 應該把它展開成 cd /usr/share/applications 。如果存在衝突,那麼按 Tab 僅補全沒有衝突的路徑。例如,Ubuntu 用戶在 /usr/share 中應該有很多以 「app」 開頭的文件夾,在這種情況下,輸入 cd /u/s/app 只會展開 /usr/share/ 部分。

另一個更深層不完整文件路徑展開的例子。在Ubuntu系統上輸入 cd /u/s/f/t/u,按下 Tab,它應該自動展開為 cd /usr/share/fonts/truetype/ubuntu。

功能包括:

  • 轉義特殊字元
  • 如果用戶路徑開頭使用引號,則不轉義字元轉義,而是在展開路徑後使用匹配字元結束引號
  • 正確展開 ~ 表達式
  • 如果正在使用 bash-completion 包,則此代碼將安全地覆蓋其 _filedir 函數。無需額外配置,只需確保在主 bash-completion 後引入此項目。

查看 項目頁面 以獲取更多信息和演示截圖。


安裝 bash-complete-partial-path

bash-complete-partial-path 安裝說明指定直接下載 bash_completion 腳本。我更喜歡從 Git 倉庫獲取,這樣我可以用一個簡單的 git pull 來更新它,因此下面的說明將使用這種安裝 bash-complete-partial-path。如果你喜歡,可以使用 官方 說明。

1、 安裝 Git(需要克隆 bash-complete-partial-path 的 Git 倉庫)。

在 Debian、Ubuntu、Linux Mint 等中,使用此命令安裝 Git:

sudo apt install git

2、 在 ~/.config/ 中克隆 bash-complete-partial-path 的 Git 倉庫:


cd ~/.config && git clone https://github.com/sio/bash-complete-partial-path

3、 在 ~/.bashrc 文件中 source ~/.config/bash-complete-partial-path/bash_completion,

用文本編輯器打開 ~/.bashrc。例如你可以使用 Gedit:

gedit ~/.bashrc

在 ~/.bashrc 的末尾添加以下內容(在一行中):


[ -s "$HOME/.config/bash-complete-partial-path/bash_completion" ] && source "$HOME/.config/bash-complete-partial-path/bash_completion"

我提到在文件的末尾添加它,因為這需要包含在你的 ~/.bashrc 文件的主 bash-completion 下面(之後)。因此,請確保不要將其添加到原始 bash-completion 之上,因為它會導致問題。

4、 引入 ~/.bashrc:


source ~/.bashrc

這樣就好了,現在應該安裝完 bash-complete-partial-path 並可以使用了。



via: https://www.linuxuprising.com/2018/07/incomplete-path-expansion-completion.html

作者: Logix 選題: lujun9972 譯者: geekpi 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


點擊「了解更多」可訪問文內鏈接

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Linux技術 的精彩文章:

調試器到底怎樣工作
你的路由器有多不安全?

TAG:Linux技術 |