65

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,791
  • 2
  • 19
  • 35
Wuffers
  • 19,020
  • 17
  • 95
  • 126
  • 1
    Don't think bash accepts plugins like zsh does, so it may require a custom build of the bash shell/binary. Any reason not to just use fish instead? Also, thanks for pointing me to another shell to play around with and see if I can learn some advanced features of haha. – Jonathan Heady Jul 11 '11 at 01:47
  • 1
    @Matrix: I can't use fish because I have a very complex bash configuration that I couldn't give up. – Wuffers Jul 11 '11 at 01:54
  • Unfortunately, I don't think that this is currently possible in bash. I'm sure that the research that you've done points to that as well. If you absolutely cannot switch to zsh, then that's understandable, but otherwise you may want to look at [fizsh](http://sourceforge.net/projects/fizsh/) which incorporates some fish-like features into zsh, although it hasn't been updated since February, so perhaps the zsh-syntax-highlighting link that you provided would be more robust. – voithos Jul 19 '11 at 18:34
  • 2
    This would be hugely beneficial for learning bash, since it allows for playing around in a whole different way. Just easily spotting that you forgot to escape a special character would be valuable. – Andreas Oct 21 '17 at 23:46

3 Answers3

22

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.

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
  • 428
  • 3
  • 6
  • 1
    Thank you, that was really helpful. I don't care if it is a "promotion" of your software. If it solves problems, is stable, maintained etc. Then, I don't see how answering with a roboust (personal) solution would be deleterious to our community - talking as a member only. – BuddhiLW Jan 11 '22 at 16:31
  • 1
    I just stumbled upon ble.sh and am quite impressed how feature-rich it is, while wondering on the other hand why it isn't way more popular. IMO this deserves much more attention. So please don't be shy of promoting it. This is great useful software. – carlfriedrich May 24 '22 at 07:18
  • Thank you both for the encouraging comments! – akinomyoga May 25 '22 at 05:44
  • That's very good, Also, Is there any way to customize it, I'm looking for the completion/suggestion to be grey/faded and background remains same similar to suggestions by fish or copilot. Although I'm almost sure there would be some option for this, that I just can't figure out. I have tried doing it through region_insert/match/target but can't make it work... – Syed M Abbas Haider Taqvi Jun 17 '23 at 10:45
  • [Got it](https://superuser.com/questions/1512618/autocompletion-background-colour-in-ble-sh), it is just so damn good, Thankyou – Syed M Abbas Haider Taqvi Jun 17 '23 at 10:51
13

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.

razzintown
  • 554
  • 5
  • 7
-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
  • 3
  • 10
  • 11
    It looks like you're answering how to set up syntax highlighting in editors, not in the shell. The ls-colorization isn't even "syntax" highlighting. – Kyle Strand Mar 25 '14 at 23:11
  • I think you're focusing on semantics more than on the solution, with the code from the links and suggested solution I get colors in bash, which I believe is what the OP wanted to know – Purefan Mar 26 '14 at 12:03
  • 10
    The original question is actually quite clearly about actual syntax highlighting. This is shown both by the screenshot of the fish shell and the final sentence: "I want to highlight the actual text that is entered at the terminal. The commands, parentheses, quotes, etc." – Kyle Strand Mar 26 '14 at 16:18
  • Can you explain why none of the two last links that I provided help in this case? – Purefan Mar 27 '14 at 15:32
  • 10
    As I said, it's not "syntax" highlighting. Those are highlighting the output of the `ls` command based on file type, which has nothing to do with shell syntax. – Kyle Strand Mar 27 '14 at 20:17
  • At the interface level seems hard to implement and keep consistent. How would you parse the many PS1 lines out there? In any case, is there a solution for Putty? Or bash itself? – Avindra Goolcharan Dec 09 '14 at 03:00
  • 3
    Zsh (https://github.com/zsh-users/zsh-syntax-highlighting) and Fish (https://fishshell.com/docs/current/tutorial.html) both have syntax highlighting, this question is obviously asking about a similar feature for bash. It is a completely valid request to ask for a feature that is "hard to implement and keep consistent" especially when it already exists in other shells – Brandon Jan 12 '18 at 17:57