MacOS, Java和IDEA

MacOS命令行自动补全设置不区分大小写

2019-03-16  本文已影响1人  SpaceCat

在MacOS上,使用命令行,有一个地方非常别扭:自动补全的时候,没法忽略大小写。也就是说,比如当前目录下有两个目录Document和download,输入cd d只能自动提示downlaod,没法提示Document,非常不方便。
在网上搜了下,可以通过配置inputrc文件来解决,下面大致介绍下。
如果你只想快速解决上面的问题,直接看第三部分即可。

1、inputrc文件介绍

Bash和其它大多数shell使用Readline库作为其输入相关的库。Readline库有一些默认的键盘映射,除此之外,也可以通过修改inputrc文件来定制键盘映射。inputrc文件时Readline库的启动文件,当使用Readline作为输入库的程序启动时,它会自动读取inputrc配置文件,初始化自定义的键盘映射。
inputrc文件的位置由shell的环境变量INPUTRC控制,如果该变量没有设置,缺省的inputrc文件的路径是~/.inputrc。如果该文件~/.inputrc不存在,就会使用系统级(对所有用户生效)的inputrc文件/etc/inputrc。如果某个用户需要修改系统默认的inputrc配置,可以改动~/.inputrc,这样会覆盖系统的默认配置。
关于如何编辑inputrc文件的信息,运行info bash,参考bash的info页的Readline Init File这一节,运行 info readline以参考readline自己的info页。

2、inputrc配置

在inputrc文件中,有两种配置:一种是inputrc变量,一种是键盘映射。
注意,在配置该文件时,注释必须占单独的一行,否则可能会有问题。

2.1 inputrc变量配置

变量配置的语法如下:

set variable value

常见的变量和配置如下:

2.2 键盘映射配置

配置inputrc变量的语法如下:

keyname: function-name or macro

keyname必须是英文,可以是Control-uControl-k等。并且keyname和冒号之间不能有空格,如果有空格会被认为是keyname的一部分。keyname的写法多样,取决于自己的喜好。
下面是一些键盘映射配置的例子:

Control-u: universal-argument C-u is bound to the function universal-argument

Meta-Rubout: backward-kill-word M-DEL is bound to the function backward-kill-word

Control-o: "> output" C-o is bound to run the macro expressed on the right hand side (that is, to insert the text ‘> output’ into the line)

Control-j: menu-complete C-j is bound to cycle through the available tab completions.

Control-k: menu-complete-backward C-k is bound to cycle backwards through the available tab completions.

"\t": menu-complete Use tab to cycle through all the possible completions.

"\C-p": history-search-backward Map control-p to allow search for completions to the current line from your history. e.g. type “git” and then hit control-p to cycle through all the recent git commands.

"\ep": history-search-backward Map escape-p to allow search for completions to the current line from your history. e.g. type “git” and then hit escape-p to cycle through all the recent git commands.

"\e[A": history-search-backward Map Up arrow to allow search for completions to the current line from your history. e.g. type “git” and then hit UP to cycle through all the recent git commands.

"\e[B": history-search-forward Map Down arrow to allow search for completions to the current line from your history. e.g. type “git” and then hit DOWN to cycle back through all the recent git commands.

"\C-d": kill-whole-line Map control-d to kill the whole line.

3、设置MacOS下自动补全不区分大小写

通过上面的介绍,解决这个问题应该是绰绰有余了。打开用户主目录下的文件~/.inputrc(如果没有的话,就新建该文件),在其中添加如下配置:

# 设置自动补全不区分大小写
set completion-ignore-case on
# 当有一个以上自动补全结果时,直接列出,不用beep提示
set show-all-if-ambiguous on

保存该文件,然后,重启命令行,就会发现自动补全不区分大小写已经生效了。
网上好多结果中,还需要多加一个下面的配置:

TAB: menu-complete

这个配置的目的是,在列出自动补全的结果之后,再按一次tab键,就会自动在自动补全列表中选择,而不是一直展示自动补全列表。效果如下图:


Ubuntu 18.10 autocomplete

但是,上面的这个图是我在Ubuntu 18.10系统上的效果。
在我的macOS 10.14.3系统上,这个配置好像和set show-all-if-ambiguous on冲突,配置之后,就不展示补全列表,直接在补全结果中循环自动补全。我不喜欢这种效果,所以最后去掉了这个配置。

参考资料

上一篇 下一篇

猜你喜欢

热点阅读