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

在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

常见的变量和配置如下:

  • completion-ignore-case
    如果设置为开(on),在自动补全时不区分大小写。该配置默认是关。
  • show-all-if-ambiguous
    这个配置该变了自动补全函数的默认行为。如果设置为开(on),当自动补全的结果有一个以上时,会直接列出这些结果。如果设置为关(off),就会提示一个beep蜂鸣声,当再按下tab时才会列出自动补全的结果。默认这项配置为关。
  • bell-style
    Controls what happens when Readline wants to ring the terminal bell. If set to ‘none’, Readline never rings the bell. If set to ‘visible’, Readline uses a visible bell if one is available. If set to ‘audible’ (the default), Readline attempts to ring the terminal's bell.
  • bind-tty-special-chars
    If set to ‘on’, Readline attempts to bind the control characters treated specially by the kernel's terminal driver to their Readline equivalents.
  • comment-begin
    The string to insert at the beginning of the line when the insert-comment command is executed. The default value is "#".
  • completion-prefix-display-length
    When set to a value greater than zero, common prefixes longer than this value are replaced with an ellipsis when displaying possible completions.
    e.g. set completion-prefix-display-length 4
  • completion-query-items
    The number of possible completions that determines when the user is asked whether the list of possibilities should be displayed. If the number of possible completions is greater than this value, Readline will ask the user whether or not he wishes to view them; otherwise, they are simply listed. This variable must be set to an integer value greater than or equal to 0. A negative value means Readline should never ask. The default limit is 100.
  • convert-meta
    If set to ‘on’, Readline will convert characters with the eighth bit set to an ascii key sequence by stripping the eighth bit and prefixing an <ESC> character, converting them to a meta-prefixed key sequence. The default value is ‘on’.
  • disable-completion
    If set to ‘On’, Readline will inhibit word completion. Completion characters will be inserted into the line as if they had been mapped to self-insert. The default is ‘off’.
  • editing-mode
    The editing-mode variable controls which default set of key bindings is used. By default, Readline starts up in Emacs editing mode, where the keystrokes are most similar to Emacs. This variable can be set to either ‘emacs’ or ‘vi’.
  • echo-control-characters
    When set to ‘on’, on operating systems that indicate they support it, readline echoes a character corresponding to a signal generated from the keyboard. The default is ‘on’.
  • enable-keypad
    When set to ‘on’, Readline will try to enable the application keypad when it is called. Some systems need this to enable the arrow keys. The default is ‘off’.
  • enable-meta-key
    When set to ‘on’, Readline will try to enable any meta modifier key the terminal claims to support when it is called. On many terminals, the meta key is used to send eight-bit characters. The default is ‘on’.
  • expand-tilde
    If set to ‘on’, tilde expansion is performed when Readline attempts word completion. The default is ‘off’.
  • history-preserve-point
    If set to ‘on’, the history code attempts to place the point (the current cursor position) at the same location on each history line retrieved with previous-history or next-history. The default is ‘off’.
  • history-size
    Set the maximum number of history entries saved in the history list. If set to zero, the number of entries in the history list is not limited.
  • horizontal-scroll-mode
    This variable can be set to either ‘on’ or ‘off’. Setting it to ‘on’ means that the text of the lines being edited will scroll horizontally on a single screen line when they are longer than the width of the screen, instead of wrapping onto a new screen line. By default, this variable is set to ‘off’.
  • input-meta
    If set to ‘on’, Readline will enable eight-bit input (it will not clear the eighth bit in the characters it reads), regardless of what the terminal claims it can support. The default value is ‘off’. The name meta-flag is a synonym for this variable.
  • isearch-terminators
    The string of characters that should terminate an incremental search without subsequently executing the character as a command. If this variable has not been given a value, the characters <ESC> and C-J will terminate an incremental search.
  • keymap
    Sets Readline's idea of the current keymap for key binding commands. Acceptable keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command, and vi-insert. vi is equivalent to vi-command; emacs is equivalent to emacs-standard. The default value is emacs. The value of the editing-mode variable also affects the default keymap.
  • mark-directories
    If set to ‘on’, completed directory names have a slash appended. The default is ‘on’.
  • mark-modified-lines
    This variable, when set to ‘on’, causes Readline to display an asterisk (*) at the start of history lines which have been modified. This variable is ‘off’ by default.
  • mark-symlinked-directories
    If set to ‘on’, completed names which are symbolic links to directories have a slash appended (subject to the value of mark-directories). The default is ‘off’.
  • match-hidden-files
    This variable, when set to ‘on’, causes Readline to match files whose names begin with a ‘.’ (hidden files) when performing filename completion, unless the leading ‘.’ is supplied by the user in the filename to be completed. This variable is ‘on’ by default.
  • output-meta
    If set to ‘on’, Readline will display characters with the eighth bit set directly rather than as a meta-prefixed escape sequence. The default is ‘off’.
  • page-completions
    If set to ‘on’, Readline uses an internal more-like pager to display a screenful of possible completions at a time. This variable is ‘on’ by default.
  • print-completions-horizontally
    If set to ‘on’, Readline will display completions with matches sorted horizontally in alphabetical order, rather than down the screen. The default is ‘off’.
  • revert-all-at-newline
    If set to ‘on’, Readline will undo all changes to history lines before returning when accept-line is executed. By default, history lines can be modified and retain individual undo lists across calls to readline. The default is ‘off’.
  • show-all-if-unmodified
    This alters the default behavior of the completion functions in a fashion similar to show-all-if-ambiguous. If set to ‘on’, words which have more than one possible completion without any possible partial completion (the possible completions don't share a common prefix) cause the matches to be listed immediately instead of ringing the bell. The default value is ‘off’.
  • skip-completed-text
    If set to ‘on’, this alters the default completion behavior when inserting a single match into the line. It's only active when performing completion in the middle of a word. If enabled, readline does not insert characters from the completion that match characters after point in the word being completed, so portions of the word following the cursor are not duplicated. For instance, if this is enabled, attempting completion when the cursor is after the ‘e’ in ‘Makefile’ will result in ‘Makefile’ rather than ‘Makefilefile’, assuming there is a single possible completion. The default value is ‘off’.
  • visible-stats
    If set to ‘on’, a character denoting a file's type is appended to the filename when listing possible completions. The default is ‘off’.

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冲突,配置之后,就不展示补全列表,直接在补全结果中循环自动补全。我不喜欢这种效果,所以最后去掉了这个配置。

参考资料

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容