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

安装

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

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

命令补全

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

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

变量补全

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

用户名补全

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

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

可执行命令补全

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

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

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

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

文件名和目录补全

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

$ 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)

主机名补全

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

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

查看已有的命令行补全

# 查看已有的命令行补全
$ 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

另外,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. 【转载】oracle的安装和配置

    转自: https://blog.csdn.net/weixin_40364885/article/details/80787472 一.概念 oracle在使用的时候时是不收费的,如果你要在工作中来 ...

  2. cmdb客户端代码完善2

    目录: 1.面试提问 2.完善采集端代码 3.唯一标识的问题 4.API的验证 1.面试会问到的问题: # 1. 为啥要做CMDB?# - 实现运维自动化, 而CMDB是实现运维自动化的基石# - 之 ...

  3. 【2019牛客暑期多校第三场】J题LRU management

    题目链接 题意 好吧,这道题我其实看都没看过,队友跟我说了说这道题是模拟题,卡时间.然后我就上了-- 大致就是维护一个线性表,然后有两种操作:插入.查询 插入时,如果这个值(string)之前出现过, ...

  4. Building Applications with Force.com and VisualForce (DEV401) (四):Building Your user Interface

    Dev 401-004:Application essential:Building Your user Interface: Module Agenda1.Custom Applications2. ...

  5. TensorFlow 实战卷积神经网络之 LeNet

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! LeNet 项目简介 1994 年深度学习三巨头之一的 Yan L ...

  6. Spring 事务注意事项

    使用事务注意事项 1,事务是程序运行如果没有错误,会自动提交事物,如果程序运行发生异常,则会自动回滚. 如果使用了try捕获异常时.一定要在catch里面手动回滚. 事务手动回滚代码 Transact ...

  7. Python常用模块之configparser

    ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置内容 ...

  8. WPF使用 Gmap.NET 绘制极坐标运动轨迹

    大家好,已经很久没有更新了,今天写一篇关于WPF 使用 Gmap.NET 相关的,网上很多Winform的很**,所以我给Wpf进行一些补充.虽然它已经很久没有更新了,但是也只能用这个了.没别的好选择 ...

  9. Appium:We shut down because no new commands came in

    在使用Appium自带的Inspector来查找元素定位时,一段时间(60s)不对其进行任何操作appium就会关闭Android应用,并打印出 info: [debug] We shut down ...

  10. echarts设置图标图例legend多种形状

    legend: {   icon: "circle",   //  字段控制形状  类型包括 circle,rect,line,roundRect,triangle,diamond ...