最近在使用阿里云 ECS 时,发现 Centos 无法进行 tab 补全,特别影响操作效率,本文简单记录下 Linux 下的 tab 命令补全功能,希望对 Linux 初学者有所帮助。

安装

Linux 最小化安装时,是没有 tab 命令补全的,需要自己手动安装。

  1. # 安装
  2. $ yum -y install bash-completion
  3. # 重新登录生效

命令补全

默认情况下,在 Linux 中提供下列补全功能:

  • 变量补全
  • 用户名补全
  • 可执行命令补全
  • 文件名和目录补全
  • 主机名补全

变量补全

  1. # echo 在 $ 符号后按两次 tab 将显示所有可用的变量
  2. $ echo $[tab] [tab]

用户名补全

  1. # su 在 “- ” 符号后,按两次 tab 将显示所有用户名
  2. $ su - [tab] [tab]
  3. # 同上,按两次 tab 将显示所有用户名
  4. $ cd ~[tab] [tab]

注意:用户名是从 /etc/passwd 文件中获取的。

可执行命令补全

在执行命令时,如果找到单个匹配项的可执行文件,则一个 tab 就会将可执行命令自动补全。

  1. $ ls -lt
  2. 总用量 5736
  3. -rwxr-xr-x 1 nginx nginx 5872560 3 24 15:33 nginx
  4. # ./n 之后按一次 tab 将补全可执行命令:./nginx
  5. $ ./n[tab]

当找到多个匹配项时,则两个 tab 将会显示可用命令。

  1. $ ./yum[tab] [tab]
  2. yum yum-builddep yum-config-manager yum-debug-dump yum-debug-restore yumdownloader yum-groups-manager

文件名和目录补全

与可执行命令补全类似,找到单个匹配项时,一个 tab 自动补全,两个 tab 列出所有匹配项。

  1. $ ls -lt
  2. 总用量 80
  3. -rw-r--r-- 1 nginx nginx 6542 3 26 21:06 nginx.conf
  4. drwxr-xr-x 2 root root 4096 3 26 20:59 site-enable
  5. drwxr-xr-x 2 nginx nginx 4096 3 24 15:33 ssl
  6. -rw-r--r-- 1 nginx nginx 2656 3 24 15:33 nginx.conf.default
  7. -rw-r--r-- 1 nginx nginx 636 3 24 15:33 scgi_params.default
  8. -rw-r--r-- 1 nginx nginx 636 3 24 15:33 scgi_params
  9. -rw-r--r-- 1 nginx nginx 664 3 24 15:33 uwsgi_params.default
  10. -rw-r--r-- 1 nginx nginx 664 3 24 15:33 uwsgi_params
  11. -rw-r--r-- 1 nginx nginx 1077 3 24 15:33 fastcgi.conf.default
  12. -rw-r--r-- 1 nginx nginx 1077 3 24 15:33 fastcgi.conf
  13. -rw-r--r-- 1 nginx nginx 1007 3 24 15:33 fastcgi_params.default
  14. -rw-r--r-- 1 nginx nginx 1007 3 24 15:33 fastcgi_params
  15. -rw-r--r-- 1 nginx nginx 5231 3 24 15:33 mime.types.default
  16. -rw-r--r-- 1 nginx nginx 5231 3 24 15:33 mime.types
  17. -rw-r--r-- 1 nginx nginx 3610 3 24 15:33 win-utf
  18. -rw-r--r-- 1 nginx nginx 2837 3 24 15:33 koi-utf
  19. -rw-r--r-- 1 nginx nginx 2223 3 24 15:33 koi-win
  20. # 在cat n 之后按一次 tab 键,会自动补全 cat nginx.conf
  21. $ cat n[tab]
  22. # “cd ” 之后按一次 tab 键,会
  23. $ cd [tab]
  24. $ cd s[tab]
  25. site-enable/ ssl/
  26. # 当有很多文件要显示时,会显示以下警告消息
  27. $ ls -l /etc/[tab] [tab]
  28. Display all 194 possibilities? (y or n)

主机名补全

  1. # ssh 在 @ 符号后,按两次 tab 键,获取要连接的主机名
  2. $ ssh root@ [tab] [tab]
  3. # 同上,按两次 tab 键,获取要连接的主机名
  4. $ scp nginx.conf nginx@ [tab] [tab]

注意:主机名是从 /etc/hosts 文件中获取的。

查看已有的命令行补全

  1. # 查看已有的命令行补全
  2. $ complete | more
  3. complete -F _minimal
  4. complete -F _filedir_xspec oodraw
  5. complete -F _filedir_xspec elinks
  6. complete -F _filedir_xspec freeamp
  7. complete -F _longopt split
  8. complete -F _longopt sed
  9. complete -F _longopt ld
  10. complete -F _longopt grep
  11. complete -j -P '"%' -S '"' jobs
  12. complete -d pushd
  13. complete -F _minimal sh
  14. complete -F _filedir_xspec playmidi
  15. complete -F _longopt mv
  16. complete -F _known_hosts rlogin
  17. complete -F _service service
  18. complete -b help
  19. complete -A stopped -P '"%' -S '"' bg
  20. complete -F _filedir_xspec cdiff
  21. complete -F _filedir_xspec bibtex
  22. complete -F _filedir_xspec rgview
  23. complete -F _filedir_xspec realplay
  24. complete -F _filedir_xspec xine
  25. complete -F _filedir_xspec xpdf
  26. complete -F _longopt strip
  27. complete -F _longopt pr
  28. complete -F _longopt grub
  29. complete -F _longopt gperf
  30. complete -F _known_hosts ftp
  31. complete -o filenames -F _yu_debug_dump yum-debug-dump.py
  32. complete -o filenames -F _yu_builddep yum-builddep
  33. complete -o filenames -F _yu_repoclosure repoclosure
  34. complete -o filenames -F _yu_repo_rss repo-rss
  35. complete -F _filedir_xspec oowriter
  36. complete -F _filedir_xspec chromium-browser
  37. complete -F _filedir_xspec gqmpeg
  38. complete -F _filedir_xspec tex
  39. complete -F _filedir_xspec zathura
  40. complete -F _filedir_xspec lzegrep
  41. complete -F _longopt m4
  42. complete -F _command time
  43. --More--
  44. # complete 命令详情
  45. $ man complete

另外,complete 可以让自己写的程序也支持自动补全功能,目前我没有此需求,需要时再研究。

Linux 提高操作效率之 tab 命令补全的更多相关文章

  1. CentOS 安装tab命令补全

    CentOS 安装tab命令补全 1. 安装epel 源 yum -y install epel-release 2. 加快yum速度 yum -y install yum-plugin-fastes ...

  2. Python tab 命令补全,以及 vim 补全

    在python 命令行中,使用补全 python 查看 packages 的目录 可用 sys.path 查看. /usr/lib/python2.7/site-packages vim tab.py ...

  3. CentOS7系统tab命令补全

    在新安装的CentOS7系统中,如果没有安装命令补全的话,在systemctl管理服务的时候就没法用tab来自动补全,因此在安装完系统后,我们要再安装命令补全这个软件: yum -y install ...

  4. ubuntu tab命令补全失效

    主要是由于环境变量设置出了问题,修改/etc/environment即可. sudo nano /etc/environment 修改后source /etc/environment

  5. shell介绍、命令历史、命令补全和别名、通配符、输入输出重定向

    第5周第5次课(4月20日) 课程内容: 8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向 8.1 shell介绍 使用yum+管道方式查看zsh和ks ...

  6. Linux命令之tab 键补全

    tab 键补全 tab 键可以实现命令及路径等补全,提高输入效率,避免出错 命令补全 用户给定的字符串只有一条惟一对应的命令,直接补全, 两次Tab会给出列表 内部命令: 外部命令:bash根据PAT ...

  7. Linux使用退格键时出现^H + Tab键命令补全失效/方向键失效 + ls文件夹和文件没有颜色

    删除问题 安装kalilinux使用普通用户的的时候按退格键无法实现删除功能 解决的办法有两个 一改变快捷键: 使用Ctrl+Backspace组合键可以实现删除功能 ctrl + backspace ...

  8. linux命令补全 忘记命令只记得开头

    linux的shell不仅提供上下箭头来翻阅历史使用过的命令,还提供命令补全功能. 例如,你想创建一个文件夹,只记得是m开头的命令,此时可以: ①输入m ②按键盘上的Tab键两次 (有可能还出现这句话 ...

  9. Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向

    一.shell介绍 shell脚本是日常Linux系统管理工作中必不可少的,不会shell,就不是一个合格管理员. shell是系统跟计算机硬件交互使用的中间介质,一个系统工具.实际上在shell和计 ...

随机推荐

  1. 零售CRM系统开发的核心功能

    在零售行业中,客户关系管理系统是一个包含销售,市场营销和客户服务流程的中央枢纽.它为企业所有者提供了一种可以结合所有与销售有关的问题并管理销售流程的有效工具.零售CRM可以留住客户,提供个性化的一流客 ...

  2. 微信小程序校历组件

    微信小程序校历组件 校历组件,可以作为校园小程序的插件,如果觉得不错,点个star吧

  3. MySQL----DDL(操作数据库,表)

    1. 操作数据库:CRUD 1. C(Create):创建 * 创建数据库: * create database 数据库名称; * 创建数据库,判断不存在,再创建: * create database ...

  4. [暴力+前缀和]2019牛客暑期多校训练营(第六场)Upgrading Technology

    链接:https://ac.nowcoder.com/acm/contest/886/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  5. JUC 中提供的限流利器-Semaphore(信号量)

    在 JUC 包下,有一个 Semaphore 类,翻译成信号量,Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.Semaphore 跟锁 ...

  6. TensorFlow 多 GPU 处理并行数据

    Multi-GPU processing with data parallelism If you write your software in a language like C++ for a s ...

  7. Excel中减少两边的字符

    从右边减少几个字符: =LEFT(A1,LEN(A1)-4)

  8. coding++:Java 获取request中的参数

    第一种: private Map<String,Object> mapParameters(HttpServletRequest request) { //封装查询条件参数 Map< ...

  9. coding++:解决Not allowed to load local resource错误-SpringBoot配置虚拟路径

    1.在SpringBoot里上传图片后返回了绝对路径,发现本地读取的环节上面出现了错误(Not allowed to load local resource),一开始用的是直接本地路径. 但是在页面上 ...

  10. Cygwin工具编译Ardupilot方法

    注意:该编译方法生成的固件基于Chibios系统,如果想要Nuttx系统固件,需采用make编译,步骤见make编译说明部分. 软件安装准备 安装Cygwin 打开链接www.cygwin.com/i ...