如何在終端中啟用自動完成功能

此 - 對於所有經常(每天)使用 終端 或GNU / Linux系統上的控制台。

在開始之前

如果我們沒有安裝 bash-completion 包,我們安裝它:

aptitude install bash 完成

對所有用戶啟用TTY自動完成

我們打開/ etc / profile

納米/等/簡介

我們在/ etc / profile中查找以下行...

#在交互式shell中啟用bash完成
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; 然後
# / etc / bash_completion
#fi

我們替換為:

如果[“ $ BASH”]; 然後
       如果[-f / etc / bash_completion] &&! shopt -oq posix; 然後
            。 / etc / bash_completion
       fi
fi

後者將為所有用戶觸發 bash_completion,包括 root。 但它只會在 TTY 上激活它,而不會在終端模擬器上激活它。

我們重置了TTY,僅此而已。

正如你所看到的,我們在原始文件中添加了一個 if ,它驗證 bash_completion 僅當我們在 Bash 中時才運行。 如果沒有這個條件,GDM 會給我們前面提到的錯誤,因為 GDM 將調用 bash_completion,並且由於某種原因它與 xsession 衝突。

在所有用戶的終端仿真器中啟用自動補全

我們打開/etc/bash.bashrc

#nano /etc/bash.bashrc

我們在/etc/bash.bashrc中查找以下行...

#在交互式shell中啟用bash完成
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; 然後
# / etc / bash_completion
#fi

...我們刪除“#”(取消註釋),如下所示:

#在交互式shell中啟用bash完成
如果[-f / etc / bash_completion] &&! shopt -oq posix; 然後
    。 / etc / bash_completion
fi

後者將為所有用戶觸發 bash_completion,包括 root。 但它只會在終端仿真器上激活它,而不會在 TTY 上激活它。

我們重新啟動任何終端,更改將生效。

僅為一名用戶啟用終端模擬器中的自動完成功能

我們必須創建(或編輯,如果存在)~/.bashrc 文件。

nano~ / .bashrc

我們添加(或搜索,如果它們不存在,但將其註釋掉,就像 /etc/bash.bashrc 一樣),使其看起來像這樣:

#在交互式shell中啟用bash完成
如果[-f / etc / bash_completion] &&! shopt -oq posix; 然後
    。 / etc / bash_completion
fi

-如果創建文件,我們只需添加這些行。
– 如果存在但這些行不存在,我們將它們添加到文件末尾。
– 如果存在並且這些行存在,我們只需取消註釋即可。

我們重新啟動控制台,更改將生效。

根據需要啟用自動完成功能

您只需在要使用它時運行 bash_completion 即可。 一旦我們在終端中結束會話(使用 exit 命令)或關閉我們正在使用的終端(如果我們處於圖形環境中),它將被停用。 為了隨時運行它,我們這樣做:

。 / etc / bash_completion
感謝Carlos Fioriti!

資料來源:98 年筆記本


在 Google 中將其新增為首選來源