Bash – Make bash as close to fish as possible

bashfish

I love the fish shell and use it exclusively in MacOS but I use bash everywhere else because its the only thing installed. Having felt the pain of maintaining configuration files for the two shells, I've decided to stop using fish, and fall back to bash instead. To make it as painless as possible, which configuration options / plugins / etc should I be using with bash to make it as close to fish as possible?

Things I miss most:

  • auto completion support: fish remembers all my commands, does completions on partial (sometimes huge) commands, seems to understand all the cmd-line apps I use and offers completions on their commands, etc. For each command fish shows in a dimmed color the command it would auto-complete to if I were to hit tab.
  • git support: fish shows me whether I am in a github repository, the name of the branch, and whether it has been modified or is clear using nice colors next to my username@hostname, I would love to have this on bash as well

Best Answer

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.

Related Question