70

In the fish shell, the actual command line is syntax highlighted:

enter image description here

Is there any way to get this same behavior in bash?

There is something just like this but for zsh, not bash. Is it possible that this could be ported to bash?

To clarify for those who don't understand the screenshot, explanation, or link provided: I want to highlight the actual text that is entered at the terminal. The commands, parentheses, quotes, etc.

Mxx
  • 2,889
Wuffers
  • 19,619

3 Answers3

30

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, as no other solutions were offered after a long time despite many views (which reflect 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.

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.

akinomyoga
  • 585
  • 5
  • 9
14

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.

-7

I dont think syntax highlighting should happen at the shell level but at the interface level (just my opinion -and someone else's it seems-), so I would look into "plugins" for Terminal or your favorite console, for example this plug in for Kate might help, or this other one which offers syntax highlighting in nano

Here is still more talk about how to syntax highlighting in Terminal:

Add alias ls='ls -G' to .bash_profile.

although here is better explained the how to

Hope it helps

Purefan
  • 303