将PWD的一部分设置为提示并保持变量更新 [英] Setting a part of a PWD as a prompt and keeping a variable updated

查看:101
本文介绍了将PWD的一部分设置为提示并保持变量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tcsh,并且尝试将PWD的一部分设置为始终显示在提示符下(这样,我将始终知道我在哪个父目录中)。

I'm using tcsh, and I'm trying to set a part of the PWD to appear always in the prompt (so I will always know in which "parent" directory I am).

我设法通过以下方式提取了提示的所需部分,并且工作正常(我称其为 MyTreePath ):

I managed to extract the needed part of the prompt in the following way, and it works fine (I call it MyTreePath):

set MyTreePath=`echo $PWD | awk '{... print whichTree}'`

我已将上述代码添加到 .tcshrc ,我已经将%$ MyTreePath 添加到我的设置提示 .tcshrc 中的c>行。

I've added the code above to my .tcshrc and I've added %$MyTreePath to my set prompt line in .tcshrc.

问题是一旦打开外壳, MyTreePath 不会改变,即使我要使用完全不同的路径。

The problem is that once the shell is opened, the MyTreePath doesn't change, even if I'm going to a totally different path.

如何保留出现在变量中的变量提示是否已更新?

How to keep a variable that appears in the prompt updated?

推荐答案

使用神奇的 cwdcmd 别名!它用于定义每次cwd更改时执行的命令。在您的情况下,您需要更新变量。

Use the magical cwdcmd alias! It is used for defining a command which executes everytime the cwd changes. In your case, you need to updated your variable.

从联机帮助页:


可以设置beepcmd,cwdcmd,periodic,precmd,postcmd和jobcmd特殊的
别名,分别在
外壳想要敲响铃铛,工作目录更改时执行命令。每tb时段b $ b,在每个提示之前,在每个命令执行之前,在执行每个命令之后以及在启动作业
或将其置于前台时。

The beepcmd, cwdcmd, periodic, precmd, postcmd, and jobcmd Special aliases can be set, respectively, to execute commands when the shell wants to ring the bell, when the working directory changes, every tperiod minutes, before each prompt, before each command gets executed, after each command gets executed, and when a job is started or is brought into the foreground.

下面是一个简单的示例:

Here's a quick example:

alias cwdcmd 'set FOO=`pwd`'
set prompt='%$FOO >>>      '

现场测试:

cd /
/ >>>      cd dev
/dev >>>

所以剩下的就是替换 pwd

有关更多信息和其他魔术别名,请参见此处

For more info and other magic aliases, see here.

这篇关于将PWD的一部分设置为提示并保持变量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆