Can I make Tab auto-completion case-insensitive in Bash?

131,612

Solution 1

In order to make bash case-insensitive for to current user:

Run the following shell script in a terminal:

# If ~/.inputrc doesn't exist yet: First include the original /etc/inputrc
# so it won't get overriden
if [ ! -a ~/.inputrc ]; then echo '$include /etc/inputrc' > ~/.inputrc; fi

# Add shell-option to ~/.inputrc to enable case-insensitive tab completion
echo 'set completion-ignore-case On' >> ~/.inputrc

Start a new shell (reopen the terminal).

To Make the changes systemwide:

# add option to /etc/inputrc to enable case-insensitive tab completion for all users
echo 'set completion-ignore-case On' >> /etc/inputrc
# you may have to use this instead if you are not a superuser:
echo 'set completion-ignore-case On' | sudo tee -a /etc/inputrc

For details, see man bash . Yes it is a long page, but bash is a somewhat complex program, and if you want just search that page for "case-insensitive" to go to the relevant section. People usually learn bash one option at a time or one bash script at a time and it takes a long time to master all the nuances. Your interest may vary.

Solution 2

Open a terminal and type the below command:

echo set completion-ignore-case on | sudo tee -a /etc/inputrc

Enter password. Restart terminal.

If in some case you want to remove case insensitive, just edit /etc/inputrc file by removing the set completion-ignore-case line.

That's all.

Solution 3

I know this question is very old but unless I am missing something I think I have a super simple solution if you are using bash.

echo "bind 'set completion-ignore-case on'" >> ~/.bashrc

Or just add the line using your favorite text editor. Restart your bash session and enjoy.

Solution 4

You can do this by setting a configuration variable for GNU readline, which is what handles the input in an interactive shell.

The variable needed is completion-ignore-case, and can be set directly in your bash session with:

bind "set completion-ignore-case on"

It can be enabled for all future bash sessions by putting set completion-ignore-case on into the users's ~/.inputrc file, or the system /etc/inputrc, to enable it for all users. This is the initialisation file for readline.

(Note that ~/.inputrc probably doesn't exist, and you will have to create it, this will override the system copy at /etc/inputrc. This has lots of useful key mappings configured, such as Ctrl-Left/Right.

The way to fix this problem is to put the line $include /etc/inputrc at the top of ~/.inputrc, e.g.:

$include /etc/inputrc

set completion-ignore-case on

Then either restart bash or reload inputrc, e.g. with Ctrlx,Ctrlr.)

More information about readline and inputrc can be found in man bash and man 3 readline.

Share:
131,612

Related videos on Youtube

user3757405
Author by

user3757405

Updated on September 18, 2022

Comments

  • user3757405
    user3757405 over 1 year

    Ubuntu's Terminal uses case-sensitive auto-completion, as I suppose would be expected for Linux.

    But I think it would often be more convenient to use a case-insensitive one instead, to save you having to be accurate while starting a name, and would probably be worth the extra false positives. Is it possible to change this behaviour?

    • masterxilo
      masterxilo over 5 years
      Very good question. As a usability tool, tab-completion should not be as strict as the computer system in general when it comes to naming things.
  • user3757405
    user3757405 over 12 years
    Thanks. I appreciate the user-specific/non-admin friendly solution. The echo line seems to have worked, but now I seem to have lost the ability to use Ctrl-Left/Right to move the cursor. Also, would >> be safer than >?
  • Panther
    Panther over 12 years
    In general >> is going to be safer, my mistake, I was assuming you did not have a ~/.inputrc . I also set noclobber =) Bit sure why your arrow keys are not working, I can not replicate that. You can remove ~/.inputrc and start a new shell.
  • Panther
    Panther over 12 years
    On my system, I can move the cursor with just the arrow keys.
  • user3757405
    user3757405 over 12 years
    @enzotib thanks, that works.. I'd like to accept the above answer, but it would be good to see this issue addressed in it.
  • Panther
    Panther over 12 years
    @Matthew - I updated the command , does that work ? You now almost certainly need > rather then >>
  • enzotib
    enzotib over 12 years
    Doesn't \n in echo require -e option?
  • Panther
    Panther over 12 years
    @enzotib It does not seem to require the -e , but it does require quotes.
  • enzotib
    enzotib over 12 years
    I still believe (after doing a test, giving the output on a single line with a literal two char sequence '\n) that to output two lines, interpreting the \n` as a newline, you need -e option to echo.
  • Panther
    Panther over 12 years
    OK, I added -e . I think it is working without the -e as I am using zsh.
  • user3757405
    user3757405 over 12 years
    Yes, that works for me.. Sorry I'm a bit of a newbie here, but maybe it would be easier just to give two separate lines? That allows a brief explanation for the $include line. It could also feature a check to make sure the file doesn't already exist.
  • Panther
    Panther over 12 years
    OK, I clarified that for you. It takes a while to learn about bash, but keep exploring, reading, and asking. linuxcommand.org is a popular start ;)
  • Morlock
    Morlock over 9 years
    Since you can also have vim mode in bash, what reason is there left to learn zsh now? ;)
  • ThorSummoner
    ThorSummoner over 9 years
    I've hesitated to use this answer for a long time, though now I try it and see how simple the ~/.inputrc configuration actually is. I would like to see a plain example of the contents of the ~/.inputrc file rather than a shell script to create that file any day.
  • Panther
    Panther over 9 years
    As long as you understand what and how the script functions, you can manually write the file if you wish. If you do not understand the script , see the comments. The comments and script were part of the explanation.
  • Walter Tross
    Walter Tross almost 9 years
    In case there is more than one user, this changes it for all, which may or may not be the desired behavior
  • Alexander
    Alexander over 8 years
    I don't think you need that if statement. >> will append if the file exists, and create and append if it doesn't.
  • muru
    muru over 8 years
    Well, you're missing something: ~/.inputrc is read by readline, which is what bash uses to provide this completion. Readline is also used by other programs, so, for generally setting this, ~/.inputrc as suggested the accepted answer would be better.
  • user3757405
    user3757405 over 8 years
    Thanks for your suggestion, it teaches me a little more, but I have to say that it doesn't seem any simpler than the one I accepted, which just uses an additional line to ensure the new file doesn't nullify the old.
  • Luca Steeb
    Luca Steeb about 8 years
    Holy shit, I copied this into /etc/inputrc and I can't type "i" anymore and when I type "e" it just spams "ssssssssss[..]" into the console.. better use the solution from @emtin4
  • workabyte
    workabyte almost 8 years
    by far the most straight forward and simplistic answer here
  • Chris
    Chris over 6 years
    @LucaSteeb I hit that too, but then realized this whole block is not supposed to be put in your .inputrc, but typed once. Only $include /etc/inputrc and set completion-ignore-case on should be in your ~..inputrc file.
  • Panther
    Panther over 6 years
    Those are commands to run not to be copy pasted into files
  • Vishrant
    Vishrant over 5 years
    perfect. well the only thing to remember is bind 'set completion-ignore-case on' should go in new line of .bashrc
  • Developerium
    Developerium over 5 years
    simple and clear answer
  • Damien Bezborodow
    Damien Bezborodow over 5 years
    If you do not want to add this to your bashrc but instead only want to set it for the current session, how is this accomplished?
  • masterxilo
    masterxilo almost 5 years
    "source /etc/inputrc" is wrong, it should be "$include /etc/inputrc"!
  • Martin Thornton
    Martin Thornton almost 5 years
    @Arch Linux Tux You broke this answer with your edit to source from $include. .inputrc is not a bash script!
  • wbg
    wbg over 4 years
    I didn't take the answer as given, but reading man bash and the mini inspirational note is solving my desire for case insensitive tab-completes.C
  • Marslo
    Marslo over 3 years
    thanks! using bind will be more easier to execute in a public system instead of modify any other files (~/.inputrc).