blo*_*ody 15
在 Bash 命令行中输入这两个命令:
dircolors -p | sed 's/;42/;01/' > ~/.dircolors
source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
有一个程序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)"
Run Code Online (Sandbox Code Playgroud)
因为默认情况下该文件~/.dircolors实际上并不存在,所以脚本使用内置的 Bash 配置 ( eval "$(dircolors -b)")。
要删除o+w('writable by others' 权限标记为最后一个 ' w' in drwxrwxrwxnotation in ls) 目录的绿色背景,您需要根据当前(内置)配置创建此文件。在命令行中键入以下内容:
dircolors -p > ~/.dircolors
Run Code Online (Sandbox Code Playgroud)
dircolor -p打印当前配置并将>输出重定向到给定文件。
现在在编辑器中打开文件并找到以下行:
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
Run Code Online (Sandbox Code Playgroud)
将数字42(表示绿色背景)01更改为(无背景)并保存更改。或者,您可以直接从命令行使用sed程序及其替换功能('s/PATTERN/NEW_STRING/'语法)来执行此操作:
sed -i 's/;42/;01/' ~/.dircolors
Run Code Online (Sandbox Code Playgroud)
以上两件事可以通过使用管道' |'的单个命令来实现:
dircolors -p | sed 's/;42/;01/' > ~/.dircolors
Run Code Online (Sandbox Code Playgroud)
要使更改生效(无需重新启动 shell),请键入:
source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
leg*_*s2k 10
LS_COLORS是ls用于为其输出着色的变量。如果LS_COLORS未设置,则dircolors在幕后使用生成。这也可以使用/不使用手动设置dircolors(参见下面的生动)。
如果大多数默认设置都有效,而您只想修复其中的几个,最简单的方法是将它们设置在您的.bashrc.
LS_COLORS=$LS_COLORS:'tw=00;33:ow=01;33:'; export LS_COLORS
Run Code Online (Sandbox Code Playgroud)
这将背景颜色 ( 42)替换为normal( 00) 和粗体 ( 01)
tw) 的其他可写目录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)
Run Code Online (Sandbox Code Playgroud)
只需通过获取所有颜色值并用4[0-9]粗体 ( 01)替换背景 ( ) 中的任何内容,即可即时执行此操作。
有更好的替代方法可以避免所有手动摆弄:
$LS_COLORS变量混淆)
.bashrcwget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O $HOME/.config/LS_COLORS
echo 'eval $(dircolors -b "$HOME/.config/dircolors")' >> $HOME/.bashrc
Run Code Online (Sandbox Code Playgroud)
LS_COLORS即时生成$LS_COLORS在.bashrc使用vivid了基于主题产生必要的颜色代码的命令export LS_COLORS="$(vivid generate molokai)"
Run Code Online (Sandbox Code Playgroud)
这两种工具配色方案都没有任何类型目录的背景颜色。
解释在输出中给出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;
Run Code Online (Sandbox Code Playgroud)
要摆脱背景,您可以更改目录权限,或使用不同的数据库来设置LS_COLORS环境变量。该dircolors文件是去的地方。
要删除所有背景色,请将以下内容粘贴到〜/ .bashrc中:
eval "$(dircolors -p | \
sed 's/ 4[0-9];/ 01;/; s/;4[0-9];/;01;/g; s/;4[0-9] /;01 /' | \
dircolors /dev/stdin)"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2012 次 |
| 最近记录: |