Make bash as close to fish as possible

There are many options for configuring bash. I use the following commands to give easy command history access:-

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

These set the Up and Down arrows to scan up and down the command history for commands beginning with the characters before the cursor on the command line (as TCC does in Windows).

As for the prompt, you can put commands to be run each time the prompt is output. I use:-

PS1="\`curspos -n>/dev/tty\`\`[ \$BASH_LEVEL != 1 ]&&echo \"[\$BASH_LEVEL]\"\`\\t[\\w/]\\\$ "

This calls two commands on each prompt:-

  • curspos is a script I wrote to check the cursor position and output a new line if not in the first column (I got annoyed with unnecessary blank lines).
  • The BASH_LEVEL checks precede the prompt with the level in square brackets if it's not 1, so it's immediately obvious if you are in a child shell (eg [2]15:55:32[~/]$).

Neither of these answers your requirement directly, but they illustrate the power that you can use in the prompt string. In your case you can simply prepend a script or function (such as gitcheck) to execute before the rest of your prompt, and this can output any information you want to see in the format you want as part of the prompt.

I would finally comment that fish is available in many Linux distributions (eg it's in the Ubuntu repository), so you need do these bash customisations only if you are prevented from installing packages.


Regarding auto-completion, bash uses GNU Readline to provide tab-completion, and history lookup & completion. Tab completion works for command names, files, and — for any commands which have completion scripts — options and arguments. Use Up or Down to move through the history linearly; or, to search, type Ctrl + R and any part of the command you remember. Readline is installed almost everywhere bash is, especially true for modern OSes.

To answer your Git concerns, Git itself includes scripts to color the prompt, and perform tab-completion for the bash shell, and others. When I installed Git for Windows, the bash program distributed with it was pre-cooked to use these automatically.

If you're familiar with bash, it probably isn't difficult to add a git prompt on Mac. Bash generates its prompt from the contents of the PS1 variable. It allows you to execute an arbitrary function to generate parts of the prompt, which allows these clever scripts to run. Like other shells, bash relies on the underlying terminal emulator to produce color. It simply passes the text you set for the prompt to the terminal emulator. Just add the right ANSI codes to the PS1 prompt, and you'll get color output.