56

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.

4
  • 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. Jul 11, 2011 at 1: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, 2011 at 1: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 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, 2011 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, 2017 at 23:46

3 Answers 3

12

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.

11

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.

1
  • 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 at 16:31
-6
+50

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

7
  • 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. Mar 25, 2014 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, 2014 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." Mar 26, 2014 at 16:18
  • 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. Mar 27, 2014 at 20:17
  • 3
    Zsh (github.com/zsh-users/zsh-syntax-highlighting) and Fish (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, 2018 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.