6

I have a folder ~/TestFolder. The current working directory is the home directory(~).

I want to enter a partial command, for example cd test, and I want it auto filled to give the command cd TestFolder upon pressing Tab.

How can I achieve this?

  • 4
    How about cd Test and then Tab? It may be about case sensitivity. Or do you explicitly want your tab completions to be case-insensitive? What is the shell? – Kamil Maciorowski Jan 05 '22 at 10:07
  • 1
    Tab completion is a feature of your shell (default is bash on most Linuxes, zsh on up-to-date Macs). But unix-style file and directory names are case sensitive: in a single directory, both test and Test could exist at the same time, and be two different files or directories. Usually, the shell will not change the case of typed characters on tab-completion, but your shell might have an option to enable it. – telcoM Jan 05 '22 at 10:08
  • Hi! And welcome here, Dugu02! As the others have pointed out, this is a functionality of the shell. We don't know which shell you are using! could you tell us what echo $SHELL says? – Marcus Müller Jan 05 '22 at 10:37
  • I use /bin/bash on Ubuntu 20.04 – George Do. Jan 05 '22 at 10:45
  • The same applies to filesystems which are case-preserving but not case-sensitive (e.g. many Mac or PC filesystems): even though you can access the folder as testfolder, all the shells I tried (bash, zsh, tcsh, ksh, csh) autocomplete only if you start with the correct case. – gidds Jan 05 '22 at 11:34
  • 1
    @gidds Zsh can be configured to do case-insensitive completion. – Gilles 'SO- stop being evil' Jan 05 '22 at 12:48

1 Answers1

5

Bash

Add the following to your ~/.bashrc-file:

bind -s 'set completion-ignore-case on'

This enables case-insensitive completion in bash, as explained by Gilles' answer in How to make cd arguments case INsensitive?.

Tested with bash 5.0.17 on Ubuntu 20.04.

Other options

I have only tested bash and zsh. This is what I found out for other shells:

The other option in the linked answer adds set completion-ignore-case on to inputrc. That could be used by other shells, too. According to this answer, which also talks about whether to use this option with bash.

Therefore, if not using bash, researching your specific shell first is a good idea (unless others will add tested answers for other shells).

Zsh

zsh uses a different line editor and not readline/inputrc. On install it gives the option to press 2 to add a recommended ~/.zshrc, which already includes the following line. It makes the the desired behaviour work out of the box:

zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za- z}' 'r:|[._-]=* r:|=* l:|=*'

Tested with zsh 5.8 on Ubuntu 20.04.

This also might be helpful: Combining zsh’s tab completion with case insensitivity

Paul Smith
  • 193
  • 1
  • 8