Linux 提高操作效率之 tab 命令補全

Linux 提高操作效率之 tab 命令補全

最近在使用阿里雲 ECS 時,發現 Centos 無法進行 tab 補全,特別影響操作效率,本文簡單記錄下 Linux 下的 tab 命令補全功能,希望對 Linux 初學者有所幫助。

安裝

Linux 最小化安裝時,是沒有 tab 命令補全的,需要自己手動安裝。

<code># 安裝
$ yum -y install bash-completion

# 重新登錄生效/<code>

命令補全

默認情況下,在 Linux 中提供下列補全功能:

  • 變量補全
  • 用戶名補全
  • 可執行命令補全
  • 文件名和目錄補全
  • 主機名補全

變量補全

<code># echo 在 $ 符號後按兩次 tab 將顯示所有可用的變量
$ echo $[tab] [tab]/<code>

用戶名補全

<code># su 在 “- ” 符號後,按兩次 tab 將顯示所有用戶名
$ su - [tab] [tab]

# 同上,按兩次 tab 將顯示所有用戶名
$ cd ~[tab] [tab]/<code>

注意:用戶名是從 /etc/passwd 文件中獲取的。

可執行命令補全

在執行命令時,如果找到單個匹配項的可執行文件,則一個 tab 就會將可執行命令自動補全。

<code>$ ls -lt
總用量 5736
-rwxr-xr-x 1 nginx nginx 5872560 3月 24 15:33 nginx

# ./n 之後按一次 tab 將補全可執行命令:./nginx
$ ./n[tab]/<code>

當找到多個匹配項時,則兩個 tab 將會顯示可用命令。

<code>$ ./yum[tab] [tab]
yum yum-builddep yum-config-manager yum-debug-dump yum-debug-restore yumdownloader yum-groups-manager/<code>

文件名和目錄補全

與可執行命令補全類似,找到單個匹配項時,一個 tab 自動補全,兩個 tab 列出所有匹配項。

<code>$ ls -lt
總用量 80
-rw-r--r-- 1 nginx nginx 6542 3月 26 21:06 nginx.conf
drwxr-xr-x 2 root root 4096 3月 26 20:59 site-enable
drwxr-xr-x 2 nginx nginx 4096 3月 24 15:33 ssl
-rw-r--r-- 1 nginx nginx 2656 3月 24 15:33 nginx.conf.default
-rw-r--r-- 1 nginx nginx 636 3月 24 15:33 scgi_params.default
-rw-r--r-- 1 nginx nginx 636 3月 24 15:33 scgi_params
-rw-r--r-- 1 nginx nginx 664 3月 24 15:33 uwsgi_params.default
-rw-r--r-- 1 nginx nginx 664 3月 24 15:33 uwsgi_params
-rw-r--r-- 1 nginx nginx 1077 3月 24 15:33 fastcgi.conf.default
-rw-r--r-- 1 nginx nginx 1077 3月 24 15:33 fastcgi.conf
-rw-r--r-- 1 nginx nginx 1007 3月 24 15:33 fastcgi_params.default
-rw-r--r-- 1 nginx nginx 1007 3月 24 15:33 fastcgi_params
-rw-r--r-- 1 nginx nginx 5231 3月 24 15:33 mime.types.default
-rw-r--r-- 1 nginx nginx 5231 3月 24 15:33 mime.types
-rw-r--r-- 1 nginx nginx 3610 3月 24 15:33 win-utf
-rw-r--r-- 1 nginx nginx 2837 3月 24 15:33 koi-utf
-rw-r--r-- 1 nginx nginx 2223 3月 24 15:33 koi-win

# 在cat n 之後按一次 tab 鍵,會自動補全 cat nginx.conf
$ cat n[tab]

# “cd ” 之後按一次 tab 鍵,會
$ cd [tab]
$ cd s[tab]
site-enable/ ssl/

# 當有很多文件要顯示時,會顯示以下警告消息
$ ls -l /etc/[tab] [tab]
Display all 194 possibilities? (y or n)/<code>

主機名補全

<code># ssh 在 @ 符號後,按兩次 tab 鍵,獲取要連接的主機名
$ ssh root@ [tab] [tab]

# 同上,按兩次 tab 鍵,獲取要連接的主機名
$ scp nginx.conf nginx@ [tab] [tab]/<code>

注意:主機名是從 /etc/hosts 文件中獲取的。

查看已有的命令行補全

<code># 查看已有的命令行補全
$ complete | more
complete -F _minimal
complete -F _filedir_xspec oodraw
complete -F _filedir_xspec elinks
complete -F _filedir_xspec freeamp
complete -F _longopt split
complete -F _longopt sed
complete -F _longopt ld
complete -F _longopt grep
complete -j -P '"%' -S '"' jobs
complete -d pushd
complete -F _minimal sh
complete -F _filedir_xspec playmidi
complete -F _longopt mv
complete -F _known_hosts rlogin
complete -F _service service
complete -b help
complete -A stopped -P '"%' -S '"' bg
complete -F _filedir_xspec cdiff
complete -F _filedir_xspec bibtex
complete -F _filedir_xspec rgview
complete -F _filedir_xspec realplay
complete -F _filedir_xspec xine
complete -F _filedir_xspec xpdf
complete -F _longopt strip

complete -F _longopt pr
complete -F _longopt grub
complete -F _longopt gperf
complete -F _known_hosts ftp
complete -o filenames -F _yu_debug_dump yum-debug-dump.py
complete -o filenames -F _yu_builddep yum-builddep
complete -o filenames -F _yu_repoclosure repoclosure
complete -o filenames -F _yu_repo_rss repo-rss
complete -F _filedir_xspec oowriter
complete -F _filedir_xspec chromium-browser
complete -F _filedir_xspec gqmpeg
complete -F _filedir_xspec tex
complete -F _filedir_xspec zathura
complete -F _filedir_xspec lzegrep
complete -F _longopt m4
complete -F _command time
--More--

# complete 命令詳情
$ man complete/<code>

另外,complete 可以讓自己寫的程序也支持自動補全功能,目前我沒有此需求,需要時再研究。


分享到:


相關文章: