ls colors in Windows git bash

7 min. read

I started using git recently, and I installed it in my Ubuntu partition, as well as in my Windows partition. Something very stupid was annoying me on the Windows Git Bash; the fact that I couldn't differentiate file types by color as I can in the Linux terminal. It was all white text over a black background.

Soon after starting to play with the issue, I discovered a .bash_history file under my Windows user folder... could it be that Git Bash was treating my Windows user folder as a Linux user folder? You loose nothing by trying, so I created a .bashrc file under my user folder and typed in the line:

alias ls='ls --color -hX

The --color option tells the bash to use color to distinguish file types, the -h is to list file sizes in a human readable format, and the X is to list directories first. I opened my Git Bash and typed ls -al... and voilá! colors!

I rolled all over the floor, because that's how excited I get about my stupidest discoveries.

If you are new to this. When you open your Git Bash, you should be by default in your home directory. Now create the .bashrc file (if on Windows 7, the file should be named .bashrc.). If you're not in the home directory change it with:

$ cd

You can create the file with Notepad++. Just beware of some text formatting issues.

I tried to push it forward. Since I don't dig the default colors, can I define my custom colors as I do in the Linux terminal?

This is not possible, or at least, I couldn't make it work. In Linux, there is the system variable LS_COLORS, that allows you to do something like this:


LS_COLORS='di=1:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35:*.rpm=90'
export LS_COLORS

Those numbers refer to colors; and the codes, to file types, according to these lists:


di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket file
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a non-existent file (orphan)
mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
ex = file which is executable (ie. has 'x' set in permissions).

00  = default colour
01  = bold
04  = underlined
05  = flashing text
07  = reverse field
31  = red
32  = green
33  = orange
34  = blue
35  = purple
36  = cyan
37  = grey
40  = black background
41  = red background
42  = green background
43  = orange background
44  = blue background
45  = purple background
46  = cyan background
47  = grey background
90  = dark grey
91  = light red
92  = light green
93  = yellow
94  = light blue
95  = light purple
96  = turquoise
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background

So, say you want to list directories with a bold font and in light blue, you would use di=01;94, where di means "apply this to directories", 01 means "bold", and 94 means "light blue". But this is not working for me at the moment (I mean, for Git Bash running on Windows 7). Could be that I have to use a different variable instead of LS_COLORS, or that I have to edit a different file, not the .bashrc, or that I have to just take a totally different approach.

Once you know all this, it's easy to do more interesting things with your bash file. For example, now you can also go and try to add your aliases at the end, like:

$ alias gs='git status'

I haven't push this forward, though, so it's possible that some commands may not work. Remember that the Git Bash is limited, you can't type all the commands you can in a Linux terminal.

Finally, if you have the Git Bash opened when making changes to the .bashrc file, you can type:

$ source .bashrc

to run the changes in that window, or you can just close it and open a new Git Bash window. That way you can test and try to check whether your commands work or not.

Comments