在tcsh提示符下替换PWD的固定部分 [英] Replacing a fixed part of PWD in a tcsh prompt
问题描述
我的提示当前显示如下:
My prompt is currently displayed like here:
[Aug-27 14:36] / x / y / z / w / u / v / dir1 / dir2 / dir3>
[Aug-27 14:36] /x/y/z/w/u/v/dir1/dir2/dir3>
我想做的是替换当前工作的常量部分路径目录
What I would like to do is replace the constant partial-path of the current working directory
/ x / y / z / w / u / v
/x/y/z/w/u/v
with
$ WORK
$WORK
所以最终将显示的是
[Aug-27 14:36] $ WORK / dir1 / dir2 / dir3>
/ x / y / z / w / t / u
始终是我平时工作的路径,并且我设置了局部变量 $ WORK
(非常类似于首页〜
的想法)。
/x/y/z/w/t/u
is always the same path from which I usually do my work and for which I have a local variable $WORK
set (very similar to the home ~
idea).
最直接的解决方案将是最受欢迎的,因为我真的不了解设置外壳。
A straight-forward solution will be most-welcomed as I really don't know much about setting a shell.
推荐答案
只需将这些行放入〜/ .tcshrc
:
Just put those lines into ~/.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
是一个命令,在显示提示之前运行。您可以使用它使用系统上可用的其他命令来自定义提示。
precmd
is a command, which is run before a prompt is shown to you. You can use it to customize your prompt using other commands available on your system.
涉及颜色时,可以使用 \e [36m
(更多详细信息,请此处)。在我的示例中,我通过在 printf%b \e [36m;
之前添加 precmd
。您可以通过这种方式添加自己的颜色,只需在其中的某个位置放置类似的 printf
命令即可。我通过将 %{\e [0; 0m%}
附加到命令行来关闭颜色(恢复终端的默认文本颜色)。提示,其结尾恰巧由提示
变量设置。我使用的是%{...%}
,因为这是在设置提示
变量时更改内部颜色的方式。所以基本上,您应该使用 printf%b ...;
作为 precmd
别名和%{...%}
表示提示
变量。
When it comes to colors, you can add them using those special color sequences like \e[36m
(more details here). In the my example I turned on non-bold cyan for the whole prompt by prepending printf "%b" "\e[36m";
to the definition of precmd
. You add your own colors this way, just put that a similar printf
command somewhere in there. I turned off colors (bringing back the default text color of the terminal) by appending %{\e[0;0m%}
to the prompt, end of which happens to be set by the prompt
variable. I'm using %{...%}
because this is how you change colors inside when setting the prompt
variable. So basically you should use printf "%b" "...";
for the precmd
alias and %{...%}
for the prompt
variable.
我使用了这些作为参考:
I used those for reference:
- 将PWD的一部分设置为提示,并保持变量更新(SO)
- 自定义Shell提示符(www.nparikh.org)
- 在(t)csh(www.unix.com)中的命令提示符
- 如何在csh的一行中编写If-else语句? (SO)
- 如何获取tcsh快捷方式列表? (Unix SE)
- 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 $ c测试$ c>恢复
tcsh 6.20.00(Astron)2016-11-24(x86_64-unknown-linux)选项宽,nls,dl,al,kan,sm,rh,nd,color,filec
。
Tested on Ubuntu 17.04 with tcsh --version
returining tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec
.
这篇关于在tcsh提示符下替换PWD的固定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!