Linux/MacOS 终端大小写敏感问题

邱秋 • 2020年04月12日 • 阅读:1307 • linux mac

打开终端,并cd 到用户home目录,输入:

echo "set completion-ignore-case on" >> .inputrc



inputrc 文件介绍



inputrc文件的位置由shell的环境变量INPUTRC控制,如果该变量没有设置,缺省的 inputrc 文件的路径是~/.inputrc。

如果该文件~/.inputrc 不存在,就会使用系统级(对所有用户生效)的inputrc文件/etc/inputrc。如果某个用户需要修改系统默认的 inputrc 配置,可以改动~/.inputrc,这样会覆盖系统的默认配置。

关于如何编辑inputrc文件的信息,运行info bash,参考bashinfo页的Readline Init File这一节,运行 info readline以参考readline自己的info页。

inputrc 配置

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



set variable value




#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.

#If set to ‘on’, Readline attempts to bind the control characters treated specially by the kernel's terminal driver to their Readline equivalents.

#The string to insert at the beginning of the line when the insert-comment command is executed. The default value is "#".

#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

#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.

#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’.

#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’.

#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’.

#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’.

#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’.

#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’.

#If set to ‘on’, tilde expansion is performed when Readline attempts word completion. The default is ‘off’.

#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’.

#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.

#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’.

#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.

#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.

#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.

#If set to ‘on’, completed directory names have a slash appended. The default is ‘on’.

#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.

#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’.

#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.

#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’.

#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.

#If set to ‘on’, Readline will display completions with matches sorted horizontally in alphabetical order, rather than down the screen. The default is ‘off’.

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’.

#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’.

#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’.

#If set to ‘on’, a character denoting a file's type is appended to the filename when listing possible completions. The default is ‘off’.


配置 inputrc 变量的语法如下:

keyname: function-name or macro


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.

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


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

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

TAB: menu-complete

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