【问题标题】:How to remove dir background in `ls -color` output [closed]如何在`ls -color`输出中删除目录背景[关闭]
【发布时间】:2017-03-27 06:26:25
【问题描述】:

我使用默认的 Linux Mint .bashrc,这里是full bashrc,输出是这样的:

某些目录有绿色背景,如何删除它?

【问题讨论】:

  • 尝试删除别名:alias ls='ls --color=auto'
  • @MarounMaroun 我希望保留颜色但更改“绿色背景”,例如,将其更改为“红色背景”
  • 最简单最干净的是LS_COLORS=$LS_COLORS:'tw=00;33:ow=01;33:'; export LS_COLORS,见answer below。

标签: linux shell terminal ls


【解决方案1】:

要删除所有背景颜色,请将以下内容粘贴到您的 ~/.bashrc 中:

eval "$(dircolors -p | \
    sed 's/ 4[0-9];/ 01;/; s/;4[0-9];/;01;/g; s/;4[0-9] /;01 /' | \
    dircolors /dev/stdin)"

【讨论】:

  • 它们在ls 的输出中被删除,但在 zsh 中使用自动完成时它们仍然显示。有什么解决方法吗?
  • ls 的颜色在 LS_COLORS 环境变量中设置,如果 zsh 代码使用这些颜色,它应该可以正常工作,如果它使用其他颜色,它将无法工作。
  • 我发现了问题。出于某种奇怪的原因,eval 需要在plugins 之后,source $ZSH/oh-my-zsh.sh 之前定义(我使用 Oh My Zsh),否则它没有完全加载。
【解决方案2】:

快速解决方案:

在 Bash 命令行中输入这两个命令:

dircolors -p | sed 's/;42/;01/' > ~/.dircolors
source ~/.bashrc

说明:

有一个程序dircolors 旨在为ls 设置配置。 默认的~/.bashrc 脚本使用以下几行加载配置:

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"

因为默认情况下~/.dircolors 文件实际上并不存在,所以脚本使用内置的 Bash 配置 (eval "$(dircolors -b)")。

要删除o+w(drwxrwxrwx 中drwxrwxrwx 中的最后一个'w' 标记的'可写权限')目录的绿色背景,您需要根据当前(内置-在)配置。在命令行中输入以下内容:

dircolors -p > ~/.dircolors

dircolor -p 打印当前配置,> 将输出重定向到给定文件。

现在在编辑器中打开文件并找到以下行:

OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky

将数字42(表示绿色背景)更改为01(无背景)并保存更改。或者,您可以直接从命令行使用sed 程序及其替换功能('s/PATTERN/NEW_STRING/' 语法):

sed -i 's/;42/;01/' ~/.dircolors

使用管道'|'的单个命令可以实现以上两件事:

dircolors -p | sed 's/;42/;01/' > ~/.dircolors

要使更改生效(无需重新启动 shell),请键入:

source ~/.bashrc

【讨论】:

  • 为了使这种方法起作用,我将 eval "$(dircolors -b ~/.dircolors)" 放在我的 .bashrc 中,因为 Bash 5.0.18 不会自动拾取 macOS 上的 ~/.dircolors 或 ~/.dir_colors as the man page says 10.15.7.
【解决方案3】:

LS_COLORS 是ls 引用的变量,用于为其输出着色。如果LS_COLORS 未设置,则在后台使用dircolors 生成。这也可以使用dircolors 手动设置/不使用(请参阅下面的Vivid)。

如果大多数默认设置都有效,而您只希望修复少数几个,最简单的方法是将它们设置在您的 .bashrc 中。

LS_COLORS=$LS_COLORS:'tw=00;33:ow=01;33:'; export LS_COLORS

这会将背景颜色 (42) 替换为正常 (00) 和粗体 (01)

  1. 设置了粘滞位的其他可写目录 (tw)
  2. 其他可写目录,无粘滞位 (ow)

这是最简单的解决方案,因为我们保留其余的默认值。

另一个答案的技巧

# -b: make dircolors generate for bash
# sed replaces offending background colors
# sed's output is fed as input for another instance of dircolors
# the entire subshell returns LS_COLORS that's `eval`uated
eval $(dircolors -b | sed 's/ 4[0-9];/ 01;/; s/;4[0-9];/;01;/g; s/;4[0-9] /;01 /' | dircolors /dev/stdin)

只需通过获取所有颜色值并将任何带有背景 (4[0-9]) 的内容替换为粗体 (01) 来即时执行此操作。


有更好的选择可以避免所有手动摆弄:

  1. LS_COLORS(项目;不要与变量 $LS_COLORS 混淆)
    • 针对不同文件格式/扩展名的精选颜色集
    • 下载最新版本,评估并附加到.bashrc
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O $HOME/.config/LS_COLORS
echo 'eval $(dircolors -b "$HOME/.config/dircolors")' >> $HOME/.bashrc
  1. Vivid
    • 多个主题
    • 即时生成LS_COLORS
    • 使用vivid 命令在.bashrc 中设置$LS_COLORS,该命令会根据主题生成必要的颜色代码
export LS_COLORS="$(vivid generate molokai)"

这两种工具配色方案都没有任何目录的背景色。

【讨论】:

    【解决方案4】:

    在dircolors -p的输出中给出了解释,例如,

    当然dircolors 不会为它的 输出着色。我使用了这个脚本:

    #!/usr/bin/perl -w
    
    use strict;
    
    our $comment = "\e[31m";
    our $reset   = "\e[K\e[m";
    
    our @data;
    
    open my $fh, "dircolors -p|" or die "cannot read from dircolors";
    @data = <$fh>;
    close $fh;
    
    printf "\e[H\e[2J";
    
    for my $n ( 0 .. $#data ) {
        chomp $data[$n];
        if ( $data[$n] =~ /^\s*#/ ) {
            printf "%s%s%s\n", $comment, $data[$n], $reset;
        }
        elsif ( $data[$n] =~ /^\s*TERM\s/ ) {
            printf "%s\n", $data[$n];
        }
        elsif ( $data[$n] =~ /^\s*[^\s]+\s+\d+(;\d+)?\s*(#.*)?$/ ) {
            my $code = $data[$n];
            $code =~ s/^\s*[^\s]+\s+//;
            $code =~ s/\s.*//;
            my $data = $data[$n];
            $data =~ s/(#.*)$/$comment$1$reset/;
            $data =~ s/^(\s*)([^\s]+)(\s+)/$1\e[${code}m$2\e[m$3/;
            printf "%s\n", $data;
        }
        else {
            printf "%s\n", $data[$n];
        }
    }
    
    1;
    

    要摆脱后台,您可以更改目录权限,或使用不同的数据库来设置您的LS_COLORS 环境变量。 dircolors documentation 是您要去的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      相关资源
      最近更新 更多