Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

simon lefisch

macrumors 65816
Original poster
Sep 29, 2014
1,006
253
Hey everyone,

Recently bought a new 2019 MBP, loving it so far. I noticed the new shell is zsh. How do I get colored output for ls? I’ve looked online and tried things out but for whatever reason I cannot get it working. Used LSCOLOR=exfxcxdxbxegedabagacad under ~/.zprofile and ~/.zshrc but nothing. Any help would be appreciated, thanks
 

KALLT

macrumors 603
Sep 23, 2008
5,373
3,401
Try export CLICOLOR=1 in .zprofile. Also, the variable is called LSCOLORS (plural).
 

ondioline

macrumors 6502
May 5, 2020
290
297
You might want to try oh my ZSH. It automatically sets this up and has themes and stuff. I've been using it for years:
 

casperes1996

macrumors 604
Jan 26, 2014
7,570
5,748
Horsens, Denmark
macOS does not use GNU ls.
What you want is this
Code:
alias ls="ls -G"
in your zshrc or profile.

The -G tells ls to give coloured output in the BSD version of ls
[automerge]1590521467[/automerge]
Oh and if you're new to Mac, but know your way around a Terminal; Get home-brew or Fink or MacPorts. - They're package managers for macOS, so you can, similar to Linux, just go
Code:
brew install <package>

Or for closed source packages
Code:
brew cask install <package>
[automerge]1590521590[/automerge]
And if you're more comfortable in Bash, you can change to any other shell through System Preferences Advanced user settings. :)
 

KALLT

macrumors 603
Sep 23, 2008
5,373
3,401
macOS does not use GNU ls.
What you want is this
Code:
alias ls="ls -G"
in your zshrc or profile.

Setting CLICOLOR=1 as I mentioned is all you need to do to enable the colour output by default. The -G argument is meant for one-off output. You don’t have to set an alias for this. LSCOLORS only affects the colours when the colour mode is enabled, either by the CLICOLOR=1 variable or -G argument.

You can find all of this in the manual page of ls (man ls).
 

casperes1996

macrumors 604
Jan 26, 2014
7,570
5,748
Horsens, Denmark
Setting CLICOLOR=1 as I mentioned is all you need to do to enable the colour output by default. The -G argument is meant for one-off output. You don’t have to set an alias for this. LSCOLORS only affects the colours when the colour mode is enabled, either by the CLICOLOR=1 variable or -G argument.

You can find all of this in the manual page of ls (man ls).

Oh cheers, didn't know that worked as well.
 

simon lefisch

macrumors 65816
Original poster
Sep 29, 2014
1,006
253
What is the result you want (which part should be coloured)?

I’d like USER to be blue and HOSTNAME to be green (similar to how I have it on my Linux box). Makes a world of difference when scrolling in Terminal looking back at commands you’ve ran. Having everything in white makes it hard to find where the commands are. Tried using
Code:
%n@{\e[0;34m%}%m%{\e[0m%}
like in the tutorial I posted for the hostname but it keeps coming out as
Code:
user@{\e[0;34muser-mbp\e[0m:~%
 

KALLT

macrumors 603
Sep 23, 2008
5,373
3,401
It is easier if you do it like this: %F{blue}<input> %f

Example: PROMPT="%F{blue}%n%f@%F{green}%m%f %1~ %# "
Result: user@host ~ %
 
  • Like
Reactions: simon lefisch

casperes1996

macrumors 604
Jan 26, 2014
7,570
5,748
Horsens, Denmark
Not my thread, but I didn't know you could use colour keyword, so I'm going to thank you as well. This is pretty cool! - I usually use Bash which I've set up differently, but being able to use keywords for colour makes customising the prompt way easier. - Also didn't know you could use PROMPT as the env variable instead of PS1
It is easier if you do it like this: %F{blue}<input> %f

Example: PROMPT="%F{blue}%n%f@%F{green}%m%f %1~ %# "
Result: user@host ~ %
 

Attachments

  • Screenshot 2020-05-27 at 00.03.05.png
    Screenshot 2020-05-27 at 00.03.05.png
    20.6 KB · Views: 249

KALLT

macrumors 603
Sep 23, 2008
5,373
3,401
I am not sure what the difference is between PROMPT and PS1. Apple’s default is this:
PS1="%n@%m %1~ %# "

If you want to use a specific colour value, you can also use the colour code:
Example: PROMPT="%32F%n%f@%34F%m%f %1~ %# "

It is better to use the colour names though, because then the colour depends on the Terminal profile you have selected. You can change the colours per profile.

To add to my answer above, to fix simon lefisch’s prompt:
PS1=$'%n@%{\e[0;34m%}%m%{\e[0m%}' (note the single quotes, the $ sign in front of it as well as the additional % in front of the first curly bracket)
 

simon lefisch

macrumors 65816
Original poster
Sep 29, 2014
1,006
253
To add to my answer above, to fix simon lefisch’s prompt:
PS1=$'%n@%{\e[0;34m%}%m%{\e[0m%}' (note the single quotes, the $ sign in front of it as well as the additional % in front of the first curly bracket)

Huh....I wonder if the reason I kept getting the messed up prompt was because I was using double quotes instead of single. Interesting.....regardless, thanks again @KALLT, you’re a life saver. Def much easier to read
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.