Is there any way to get bash to have syntax highlighting like fish?
Bash uses readline
for interactive input, so syntax highlighting would need to be implemented in that program. I found a Google Groups discussion about how to code such a feature.
The fish shell uses its own line editor that is specific to that program, and can not be directly ported.
You may find that zsh
is very similar to bash, and its line editor is extendable. I found zsh-syntax-highlighting to enable this feature in zsh.
There is no simple way to obtain syntax highlighting in GNU Bash (or GNU Readline), but it is in principle possible to implement your own line editor in Bash script by binding all the user inputs to shell functions using the builtin command bind -x 'BYTE: SHELL-COMMAND'
. It is of course possible to integrate the feature of syntax highlighting in your own line editor.
In fact, I implemented a line editor ble.sh
with features like syntax highlighting and auto-suggestions. It supports Bash 3.0..5.1. Since it is written in (almost-)pure Bash scripts, you can just source
the script in ~/.bashrc
. Here is an example to set up ble.sh
in the bashrc (see README for details):
$ git clone https://github.com/akinomyoga/ble.sh.git
$ cd ble.sh
$ make
$ make INSDIR="$HOME/.local/share/blesh" install
# bashrc
# Add the following line at the beginning of bashrc
[[ $- == *i* ]] &&
source "$HOME/.local/share/blesh/ble.sh" --attach=none
# ... other bashrc settings ...
# Add the following line at the end of bashrc
[[ ${BLE_VERSION-} ]] && ble-attach
Note: I know that sometimes answering questions with links to own products is considered self-promotion and unpreferable, so I have been refraining from answering this question. However, no other solutions did not appear a long time, and also this question has many views (which reflects its significant demand). So I decided to answer this question today. Referring to the following meta-questions/answers, I described the idea first and next provided a link to my project as an example implementation.
- How to offer personal open-source libraries? - Meta Stack Exchange
- User promoting his software in relevant questions without disclosure - Meta Stack Overflow
Yes, I have to admit that this is actually self-promotion, but I believe that this helps people who want the feature. If there are problems, I would appreciate it if you could tell me that in the comments.
Update 2022-01-12 Update the supported Bash versions. Correct grammar.