How can I make commands appear bold in zsh?

9,776

Solution 1

The old-fashioned way was to use POSTEDIT

 POSTEDIT=$'\e[0m'

(and by the way this isn't bash, don't use a DEBUG trap to simulate preexec: zsh is where it's from) but since zsh 4.3.11 you can use the command line syntax highlighting facility. Let your prompt care only about your prompt and set

zle_highlight=(default:bold)

Solution 2

What you want is preexec hook function:

preexec() { printf "\e[0m"; }

Then before each command was executed, preexec will be run to reset your font to normal.

So, to get the same prompt you show in your question, add these lines to your ~/.zshrc:

autoload -U colors && colors
PS1="%{$fg_bold[yellow]%}%n@%m %{$fg[blue]%}%~ \$ %{$reset_color%}%{$fg_bold[white]%}"
preexec() { printf "\e[0m"; }
Share:
9,776

Related videos on Youtube

terdon
Author by

terdon

Elected moderator on Unix & Linux. I've been using Linux since the late '90s and have gone through a variety of distributions. At one time or another, I've been a user of Mandrake, SuSe, openSuSe, Fedora, RedHat, Ubuntu, Mint, Linux Mint Debian Edition (basically Debian testing but more green) and, for the past few years, Arch. My Linux expertise, such as it is, is mostly on manipulating text and regular expressions since that represents a large chunk of my daily work.

Updated on September 18, 2022

Comments

  • terdon
    terdon over 1 year

    I have set up my bash shell so that any commands I type appear in bold and the commands' output is shown in normal weight:

    enter image description here

    I did this by adding \e[01m at the end of my PS1 variable to turn on bold, and using trap DEBUG to turn it off:

    trap 'printf "\e[0m" "$_"' DEBUG
    

    That way, the \e[0m is printed before each command is executed and I get normal font weight in the output.

    How would I go about getting the same effect in zsh?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    That's the right thing to use instead of a DEBUG trap, but neither is the right way to make the command line bold.