当前位置: 首页>>技术教程>>正文


端子颜色不起作用

问题描述

我正在从OSX通过SSH访问Ubuntu 10.04.2 LTS服务器。最近,颜色停止工作了。我认为这是在我安装RVM或对其进行故障排除时发生的,但我并不乐观。

在.bashrc中,我无法使用force_color_prompt=yes,并且在运行env | grep TERM时,我得到TERM=xterm-color。但是仍然没有颜色。

有任何想法吗?谢谢!

这是cat .bashrc的输出

# ~/.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
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# 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

# 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


# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac


# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
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

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\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)"
    alias ls='ls --color=auto'
   alias dir='dir --color=auto'
   alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# 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

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"

最佳方案

/etc /skel中有一个默认的示例.bashrc文件-您可以使用mv .bashrc .bashrc.bak然后使用cp /etc/skel/.bashrc .bashrc备份当前.bashrc的备份,以将该示例复制回您的主目录。之后,在新文件中添加un-comment force_color_prompt=yes并将[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"添加回该文件的末尾(最后一行),以使RVM仍然有效。

之后,在提示符下执行. .bashrc以重新加载bash配置文件,这将再次为您提供彩色提示,并使用命令type rvm | head -1测试RVM,该命令应返回文本:rvm is a function

如果这不起作用,请让我们知道您是否在Mac OS X上使用标准的Terminal应用程序,以及在Mac的Terminal中使用的配置文件。

次佳方案

颜色已停止工作,很可能是在您从Snow Leopard升级到Lion之后。

在Lion中,由于某种原因,默认情况下将终端声明为”xterm-256color”,而不是Ubuntu理解的”xterm-color”。

如先前的响应者所述,其中一个选项适合您,请取消注释~/.bashrc中的force_color_prompt=yes

另一个选项是在Terminal.app中将终端重新声明为xterm-color。这样做的方法如下:

  1. 启动Terminal.app

  2. 转到菜单终端->首选项(或使用Cmd +)

  3. 转到Settings选项卡,找到设置为默认主题

  4. 转到“ Advanced”标签

  5. 找到Declare terminal as:旁边的下拉列表并将其设置为xterm-color

这是我的Settings

更新:似乎Mountain Lion从下拉菜单中完全删除了xterm-color选项。如果要进行升级,则很可能会保留原来的配置,但是如果要进行全新安装,则必须手动编辑Terminal的配置文件,为此,您将需要一个plist编辑器(包含在XCode中,通过App Store安装)。终端文件的位置在〜/Library /Preferences /com.apple.Terminal.plist中,以便添加xterm-color选项,请执行以下操作:

  1. 如果当前正在运行,请退出Terminal.app

  2. 从App Store安装XCode(如果已经安装,请跳过此步骤)

  3. $ open ~/Library/Preferences/com.apple.Terminal.plist

  4. 将plist导航到Root > Window Settings

  5. 找到默认主题的名称(在我的情况下为Pro),将其展开并找到TerminalType键。

  6. 现在,只需将TerminalType密钥手动更改为xterm-color

  7. 保存文件并启动终端。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/11200.html,未经允许,请勿转载。