在 tcsh 提示符中替换 PWD 的固定部分
Replacing a fixed part of PWD in a tcsh prompt
我的提示目前显示如下:
[Aug-27 14:36] /x/y/z/w/u/v/dir1/dir2/dir3>
我想做的是替换当前工作目录的常量部分路径
/x/y/z/w/u/v
和
$WORK
所以最终会显示的是
[Aug-27 14:36] $WORK/dir1/dir2/dir3>
/x/y/z/w/t/u
始终是我通常工作的相同路径,并且我为此设置了一个局部变量 $WORK
(非常类似于 home ~
的想法)。
直接的解决方案将是最受欢迎的,因为我真的不太了解设置 shell。
这只是一个自定义提示,可能会让您了解如何 create/improve 您的提示:
set COLOR1="%{\e[0;32m%}"
set COLOR2="%{\e[0;33m%}"
set COLOR3="%{\e[0;36m%}"
set COLOR4="%{\e[0;0m%}"
set COLOR5="%{\e[0;33m%}"
set prompt="$COLOR2\[$COLOR3%n@%M$COLOR2\:$COLOR1%~$COLOR2\] [%p %d]\n$COLOR5>$COLOR4 "
set promptchars = "%#"
提示将类似于:
[user@host:/current/dir] [current date]
>
像 COLOR
变量一样,您可以设置 WORK
。
另外,这个答案可能有帮助:
只需将这些行放入 ~/.tcshrc
:
set WORK='/x/y/z/w/u/v'
set dollar='$'
alias precmd 'printf "%b" "\e[36m"; date +"[%b-%d %H:%M] " | tr -d "\n"; [ `expr "$PWD" : "$WORK*"` -gt 0 ] && printf "%s" "$dollar$PWD" | sed "s|$WORK|WORK|" - || printf "%s" "$PWD"'
set prompt='%#%{\e[0;0m%} '
# The default tcsh ^L binding for screen clearing does not run precmd.
# This one does.
bindkey -s "^L" "clear\n"
precmd
是一个命令,它是 运行 在提示之前显示给你。您可以使用它来使用系统上可用的其他命令来自定义提示。
当涉及到颜色时,您可以使用像 \e[36m
这样的特殊颜色序列来添加它们(更多详细信息 here)。在我的示例中,我通过在 precmd
的定义前加上 printf "%b" "\e[36m";
为整个提示打开了非粗体青色。您可以通过这种方式添加自己的颜色,只需在其中某处放置一个类似的 printf
命令即可。我通过在提示中附加 %{\e[0;0m%}
来关闭颜色(恢复终端的默认文本颜色),提示的末尾恰好由 prompt
设置多变的。我使用 %{...%}
是因为这是在设置 prompt
变量时改变内部颜色的方式。所以基本上你应该使用 printf "%b" "...";
作为 precmd
别名,%{...%}
作为 prompt
变量。
我参考了那些:
- Setting a part of a PWD as a prompt and keeping a variable updated (SO)
- Customizing your shell prompt (www.nparikh.org)
- Setting the current path in the command prompt in (t)csh (www.unix.com)
- How to write If-else statement in one line in csh? (SO)
- How to get a list of tcsh shortcuts? (Unix SE)
在 Ubuntu 17.04 上测试 tcsh --version
返回 tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec
。
我的提示目前显示如下:
[Aug-27 14:36] /x/y/z/w/u/v/dir1/dir2/dir3>
我想做的是替换当前工作目录的常量部分路径
/x/y/z/w/u/v
和
$WORK
所以最终会显示的是
[Aug-27 14:36] $WORK/dir1/dir2/dir3>
/x/y/z/w/t/u
始终是我通常工作的相同路径,并且我为此设置了一个局部变量 $WORK
(非常类似于 home ~
的想法)。
直接的解决方案将是最受欢迎的,因为我真的不太了解设置 shell。
这只是一个自定义提示,可能会让您了解如何 create/improve 您的提示:
set COLOR1="%{\e[0;32m%}"
set COLOR2="%{\e[0;33m%}"
set COLOR3="%{\e[0;36m%}"
set COLOR4="%{\e[0;0m%}"
set COLOR5="%{\e[0;33m%}"
set prompt="$COLOR2\[$COLOR3%n@%M$COLOR2\:$COLOR1%~$COLOR2\] [%p %d]\n$COLOR5>$COLOR4 "
set promptchars = "%#"
提示将类似于:
[user@host:/current/dir] [current date]
>
像 COLOR
变量一样,您可以设置 WORK
。
另外,这个答案可能有帮助:
只需将这些行放入 ~/.tcshrc
:
set WORK='/x/y/z/w/u/v'
set dollar='$'
alias precmd 'printf "%b" "\e[36m"; date +"[%b-%d %H:%M] " | tr -d "\n"; [ `expr "$PWD" : "$WORK*"` -gt 0 ] && printf "%s" "$dollar$PWD" | sed "s|$WORK|WORK|" - || printf "%s" "$PWD"'
set prompt='%#%{\e[0;0m%} '
# The default tcsh ^L binding for screen clearing does not run precmd.
# This one does.
bindkey -s "^L" "clear\n"
precmd
是一个命令,它是 运行 在提示之前显示给你。您可以使用它来使用系统上可用的其他命令来自定义提示。
当涉及到颜色时,您可以使用像 \e[36m
这样的特殊颜色序列来添加它们(更多详细信息 here)。在我的示例中,我通过在 precmd
的定义前加上 printf "%b" "\e[36m";
为整个提示打开了非粗体青色。您可以通过这种方式添加自己的颜色,只需在其中某处放置一个类似的 printf
命令即可。我通过在提示中附加 %{\e[0;0m%}
来关闭颜色(恢复终端的默认文本颜色),提示的末尾恰好由 prompt
设置多变的。我使用 %{...%}
是因为这是在设置 prompt
变量时改变内部颜色的方式。所以基本上你应该使用 printf "%b" "...";
作为 precmd
别名,%{...%}
作为 prompt
变量。
我参考了那些:
- Setting a part of a PWD as a prompt and keeping a variable updated (SO)
- Customizing your shell prompt (www.nparikh.org)
- Setting the current path in the command prompt in (t)csh (www.unix.com)
- How to write If-else statement in one line in csh? (SO)
- How to get a list of tcsh shortcuts? (Unix SE)
在 Ubuntu 17.04 上测试 tcsh --version
返回 tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec
。