Mac 终端自动完成

通常在 Unix/Linux 终端上,当您按 Tab时,它会自动完成,直到有几个选项,然后它会列出下面的选项供您选择。例如:

cd he
helpFolder/ helpMe/ heIsThere/
cd help

对于 Mac 终端,它会强迫你按两次来查看选项。我想知道是否有可能改变设置为一个 Tab新闻?

115980 次浏览

Put this in your ~/.inputrc:

set show-all-if-ambiguous on

You'll need to restart your shell (for example by opening a new terminal window or typing exec /bin/bash).

  1. Type in terminal nano ~/.inputrc
  2. Paste the following on separate lines

    set completion-ignore-case on
    set show-all-if-ambiguous on
    TAB: menu-complete
    
  3. Hit Control+O to save changes to .inputrc followed by control+X to quit
  4. Open a new Terminal window or tab, or type “login” to open a new session with the rules in effect
  5. Type and hit the tab key

Src: http://osxdaily.com/2012/08/02/improve-tab-completion-in-mac-os-x-terminal/

What you want to change is the Bash shell setup. My OS version is 10.11.6 and I use mvim, here just vim or vi,if the ~/.inputrc does not exist, then you need create a new one.

  1. In terminal type vim /.inputrc.
  2. The variable of completion-ignore-case and show-all-if-ambiguous need to be enabled to be ON, this is done by paste those settings in two lines, set completion-ignore-case on and set show-all-if-ambiguous on

  3. Add TAB: menu-complete and write&quit the vim. :wq

  4. Quit the Terminal running, Command+Q.
  5. Start Terminal again and verify the result.

I am adding a new answer because in my case adding the line set show-all-if-ambiguous on was not enough.

I had also to remove the line: TAB: menu-complete.

My complete ~/.inputrc file is the following:

set completion-ignore-case on
set show-all-if-ambiguous on

Looks like the other comments are for bash whereas MacOs moved already to zsh as default shell. To enable autocompletion (including subcommands), putting the following line in zsh config file (~/.zshrc) works for me:

autoload -Uz compinit && compinit