ls输出特定文件的颜色?

时间:2021-01-14 14:05:38

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).

是否可以为特定的文件分配ls命令的输出颜色?我这样问的原因是,对于git存储库,我希望我的跟踪文件与未跟踪的文件有不同的颜色。因此,特别地,我想获取“git ls-files”的输出,并为这些文件赋予某种颜色(或将名称加粗或其他)。

2 个解决方案

#1


2  

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命令,这很容易。假设你想用紫色显示所有。mp3文件,然后运行:

$ 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:

另外,确保您的ls命令启用了——color选项。我通常会这样说:

alias ls='ls --color=auto'

Purple color is "35". The other basic colors are:

紫色是“35”。其他基本颜色是:

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

#2


1  

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

我想要实现对git目录列表的完全相同的操作。

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.

我的解决方案是在Z shell中添加一个钩子(add- sh-hook chpwd -alias),它在我更改目录时调用函数git-alias。函数git-alias定义了,基于它是否是一个git目录,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:

这将返回如下内容:

ls输出特定文件的颜色?

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.

它仍然不完美(例如,注意文件“许可证”的双条目以及文件和文件夹的固定顺序),但这是我所能达到的最好的结果。

#1


2  

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命令,这很容易。假设你想用紫色显示所有。mp3文件,然后运行:

$ 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:

另外,确保您的ls命令启用了——color选项。我通常会这样说:

alias ls='ls --color=auto'

Purple color is "35". The other basic colors are:

紫色是“35”。其他基本颜色是:

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

#2


1  

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

我想要实现对git目录列表的完全相同的操作。

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.

我的解决方案是在Z shell中添加一个钩子(add- sh-hook chpwd -alias),它在我更改目录时调用函数git-alias。函数git-alias定义了,基于它是否是一个git目录,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:

这将返回如下内容:

ls输出特定文件的颜色?

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.

它仍然不完美(例如,注意文件“许可证”的双条目以及文件和文件夹的固定顺序),但这是我所能达到的最好的结果。