在tcsh中添加动态提示颜色

时间:2014-10-14 22:39:03

标签: csh tcsh

我有一个脚本用于更新我的tcsh提示符。我想添加一些可以添加颜色的附加代码。但是,我希望颜色根据$dir_str的值而有所不同。下面是我的.cshrc中的代码:

# Initial set required for 1st prompt in a new window
set dir_str=`update_dir.csh`
# set prompt to use $dir_str
set prompt = '%$dir_str [%c09]%L\n -> '
# aliases for change dir commands to update prompt
alias cd 'chdir \!* && set dir_str="`update_dir.csh`"'
alias pushd 'pushd \!* && set dir_str="`update_dir.csh`"'
alias popd 'popd \!* && set dir_str="`update_dir.csh`"'

以下是update_dir.csh的代码。我实际上并没有使用env vars($in_dev_directory等),这些实际上是更复杂的命令:

if ($in_dev_directory) then
  echo "\e[1;34mDEV\e[0m"
else if ($in_prod_directory) then
  echo "\e[1;30mPROD\e[0m"
else
  echo "\e[1;31mNO_PROJ\e[0m"
endif

没有任何颜色代码信息,提示更新按预期工作。我也试过回应提示颜色代码,例如echo "%{\033[1;31m%}NO_PROJ%{\033[0m%}"但是会​​打印字符(没有颜色)。独立运行更新脚本会以所需颜色打印字符串。 有关如何根据目录更新提示颜色的任何想法吗?

旁注:我正在使用目录更改别名,因为别名cwdcmd一直给我“命令未找到”错误(即使是完整路径)。另外,我坚持使用tcsh(没有zsh等)。

1 个答案:

答案 0 :(得分:0)

我能够使用set prompt = '%{%$prompt_color_str%}%B%$prompt_dir_str%b%{\033[0m%} [%c09]%L\n -> '使用$prompt_color_str通过回显"^[[0;31m"(或所需颜色代码)的shell脚本生成^[。请注意,{{1}}是转义字符(不是插入符号括号)。我还更新了我的chdir别名来运行附加脚本。