CODV

WSL Ubuntu终端颜色优化

由于手动从2204升级到2404,一些设置变了,于是看到了https://www.cnblogs.com/lethe1203/p/18065194,简单修改了一下

使用nano ~/.bashrc可以快速全选删除,注意保存cuda conda等系统变量

进入 nano 后:
Alt+A # 设置标记起点(光标位置)
Alt+/ # 跳到文件末尾(全选)
Ctrl+K # 剪切所选内容(即删除)
Ctrl+O # 保存文件
Ctrl+X # 退出 nano

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# 开启WSL 256色核心支持,解决颜色单调问题
export TERM=xterm-256color
# 强制开启彩色提示符(WSL专属优化)
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

# 彩色PS1命令提示符(核心高亮:用户浅紫@主机浅绿:路径蓝色$ ,root自动红色#)
if [ "$color_prompt" = yes ]; then
    PS1='\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]\[\033[01;35m\]${debian_chroot:+($debian_chroot)}\u\[\033[00m\]@\[\033[01;32m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# 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)"
    # 彩色ls基础命令(-h显示K/M/G大小单位,WSL优化)
    alias ls='ls --color=auto -h'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    # 彩色grep系列(匹配内容红色高亮,忽略无用目录)
    alias grep='grep --color=auto --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=venv'
    alias fgrep='fgrep --color=auto --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=venv'
    alias egrep='egrep --color=auto --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=venv'
fi

# 增强版彩色ls别名(全量高亮,保留系统默认参数+彩色)
alias ll='ls -alFh --color=auto'  # 所有文件+详细信息+大小单位+彩色
alias la='ls -Ah --color=auto'    # 显示隐藏文件(除.和..)+彩色
alias l='ls -CFh --color=auto'    # 分栏显示+彩色+大小单位
alias lsd='ls -ld --color=auto */' # 仅显示目录+彩色(实用拓展)

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# 开启bash可编程彩色补全(WSL Ubuntu新版适配,核心补全高亮)
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# WSL彩色补全优化(补全菜单/前缀高亮,不区分大小写)
bind 'set completion-ignore-case on'   # 补全不区分大小写
bind 'set colored-stats on'           # 补全列表文件类型彩色高亮
bind 'set colored-completion-prefix on' # 补全前缀彩色高亮
bind 'set show-all-if-ambiguous on'   # 按一次Tab直接显示补全列表
# 解决WSL中ls目录/其他用户文件颜色失效问题
export LS_COLORS="$LS_COLORS:ow=30;42:tw=30;42:di=01;34:ex=01;32:ln=01;36:pi=01;33:so=01;35:bd=01;31:cd=01;31:su=01;31:sg=01;31:"

在默认配置基础上进行了大量针对性优化。

  1. 终端环境与颜色支持 (WSL 优化)

强制开启 256 色: 直接执行 export TERM=xterm-256color。这是为了解决 WSL(Windows Subsystem for Linux)默认终端有时候颜色显示不全或单调的问题。

强制开启彩色提示符: 取消了 force_color_prompt=yes 的注释,确保无论通过什么方式连接,提示符都有颜色。

  1. 命令提示符 (PS1) 样式

样式:颜色更丰富,且将用户和主机名分开染色。

颜色:用户为浅紫色 (Magenta),@ 符号无色,主机为浅绿色,当前路径为蓝色。

注:代码注释中提到“root自动红色”,但在提供的代码段中是硬编码颜色,未见针对 UID 的判断逻辑,可能是硬编码了普通用户的颜色方案。

  1. 命令别名 (Aliases) 的增强

这是定制版改动最实用的部分,主要针对 ls 和 grep 做了人性化调整。

A. ls 命令 (列出文件)

增加 -h 参数: 所有的 ls 别名(包括 ls 本身、ll、la、l)都强制加上了 -h,这意味着文件大小会自动显示为 K, M, G(如 1.2M),而不是难以阅读的字节数。

增强显示:

ll:显示所有文件详细信息 + 彩色 + 易读大小。

la:显示隐藏文件 + 彩色 + 易读大小。

l:分栏显示 + 彩色 + 易读大小。

新增 lsd: 增加了一个新命令 lsd,只列出当前目录下的文件夹。

B. grep 命令 (搜索)

排除干扰目录: 在使用 grep, fgrep, egrep 时,自动排除 .git、node_modules 和 venv 目录。

作用: 这对程序员非常有用,避免搜索代码时被庞大的依赖库或版本控制文件刷屏。

  1. 补全与交互体验 (Input & Completion)

定制版在文件末尾添加了一段默认版没有的配置,极大地提升了操作手感:

大小写不敏感补全: bind 'set completion-ignore-case on'

输入 cd des 按 Tab 可以补全 Desktop,无需区分大小写。

补全菜单优化:

set colored-stats on:补全列表显示文件类型颜色。

set show-all-if-ambiguous on:按一次 Tab 直接显示所有候选项,不需要按两次。

posted on 2026-01-30 11:40  VvLin-61  阅读(5)  评论(0)    收藏  举报

导航