ls output colors for specific files?

Is it possible to assign the ls command’s output colors for specific files?
The reason I ask is that for git repositories, I want my tracked files to have different colors from untracked ones. So specifically, I want to take the output of “git ls-files” and give those files a certain color (or make the names bold or whatever).

  • Using subtree-merge strategy, history is not merging
  • iPhone: How to re-create XCode built-in git repository?
  • Why is my Git repository so much bigger than the working directory?
  • How do you pull from a different branch than the master using git?
  • PowerShell ISE throws an error on git checkout
  • How could I get my SVN-only host to pull from a git repository?
  • PHPStorm display working branch name
  • Did Apple remove the 'git gui' command in XCode 4.5 command line tools?
  • opendiff ( FileMerge ) stopped working with git on Mac OS X
  • Make git-grep ignore large text files
  • What git branch should I commit unit tests to?
  • how to create new subfolder in git repository?
  • 2 Solutions collect form web for “ls output colors for specific files?”

    For a standard ls command, it is quite easy. Let’s say you want to show all .mp3 files in a purple color, then run:

    $ LS_COLORS=$LS_COLORS:"*mp3=35:"
    

    Also, make sure to guarantee that your ls command has the –color option enabled. I usually use something like:

    alias ls='ls --color=auto'
    

    Purple color is “35”. The other basic colors are:

    0   = default colour
    1   = bold
    4   = underlined
    5   = flashing text
    7   = reverse field
    40  = black background
    41  = red background
    42  = green background
    43  = orange background
    44  = blue background
    45  = purple background
    46  = cyan background
    47  = grey background
    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
    

    I wanted to achieve the exact same thing to use for git directory listings as well.

    My solution is to add a hook in Z shell (add-zsh-hook chpwd git-alias) that calls the function git-alias when I change directories.
    The function git-alias defines, based on whether it’s a git directory, the functionality of ll.

    function git-alias {
        # Let ll depend on the current directory
        if git rev-parse --git-dir > /dev/null 2>&1; then
            # Make sure ll has no alias assigned
            alias ll='ls' && unalias ll
            function ll {
                ls $@ -lF|grep --color=never -e '/$' # directories
                ls $@ -lQ|grep -f <(git ls-files -cdmoi --exclude-standard)|sed -e "s/\(.*\) \"\(.*\)\"$/\1 ${fg[dgray]}\2 (ignored)$reset_color/" # ignored
                ls $@ -l|grep --color=never -f <(git ls-files -c 2>/dev/null) # cached
                ls $@ -lQ|grep -f <(git ls-files -m 2>/dev/null)|sed -e "s/\(.*\) \"\(.*\)\"$/\1 ${fg[blue]}✎ \2$reset_color/" # modified
                ls $@ -lQ|grep -f <(git ls-files -o --exclude-standard 2>/dev/null)|sed -e "s/\(.*\) \"\(.*\)\"$/\1 ${fg[blue]}★ \2$reset_color/" # new
                git ls-files -d 2>/dev/null|sed -e "s/^\(.*\)$/Deleted: ${fg[red]}✗ \1$reset_color/" # deleted
            }
        else
            alias ll='ls -lF'
        fi
    }
    

    This will return something like:

    example output

    It is still not perfect (e.g., note the double entry for the file ‘LICENSE’ and the fixed ordering of files and folders), but it’s the best I could achieve.

    Git Baby is a git and github fan, let's start git clone.