问题描述
我正在使用Ubuntu,在处理某些深层目录层次结构时,我对bash中的这种长提示感到厌倦.因此,我想通过以下方式调整我的PS1以缩短工作目录部分:
I am using Ubuntu and I am tired of this long prompts in bash when I am working with some deep directory hierarchy. So, I would like to tweak my PS1 to shorten the working directory part the following way:
目前,我有:
pajton@dragon:~/workspace/projects/project1/folder1/test$
,并希望拥有:
pajton@dragon:~/workspace/.../folder1/test$
如果len($ PWD)超过给定的阈值,则会发生截断.我要始终保留第一个路径组件和至少一个最后一个路径组件.然后在空间允许的情况下,从右侧添加更多组件.
The truncating would occur if len($PWD) passes given threshold. I want to always keep the first path component and at least one last path component. Then as space permits, add more components taking from the right.
这是我目前所拥有的.它有效,但是:1)不保留第一个路径分量,2)不遵守边界处的切割路径:
This is what I have currently. It works, but: 1) doesn't keep first path component, 2) doesn't respect cutting path at boundaries:
pwd_length=14
pwd_symbol="..."
newPWD="${PWD/#$HOME/~}"
if [ $(echo -n $newPWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD="...$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else
newPWD="$(echo -n $PWD)"
fi
结果:
pajton@dragon:...sth/folder1/sample$
提前谢谢!
推荐答案
针对您的情况,使用awk而不是sed来考虑以下脚本:
Consider this script using awk instead of sed for your case:
pwd_length=14
pwd_symbol="..."
newPWD="${PWD/#$HOME/~}"
if [ $(echo -n $newPWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD=$(echo -n $newPWD | awk -F '/' '{
print $1 "/" $2 "/.../" $(NF-1) "/" $(NF)}')
fi
PS1='${newPWD}$ '
对于目录~/workspace/projects/project1/folder1/test
的示例,它将PS1设置为:~/workspace/.../folder1/test
For your example of directory ~/workspace/projects/project1/folder1/test
it makes PS1 as: ~/workspace/.../folder1/test
以上解决方案将设置您的提示,但正如您在注释中指出的那样,当您更改目录时,它不会动态更改PS1.因此,这里是可以在更改目录时动态设置PS1的解决方案.
Above solution will set your prompt but as you noted in your comment that it will NOT change PS1 dynamically when you change directory. So here is the solution that will dynamically set PS1 when you change directories around.
将这两行放在.bashrc文件中:
Put these 2 lines in your .bashrc file:
export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{
if (length($0) > 14) { if (NF>4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF;
else if (NF>3) print $1 "/" $2 "/.../" $NF;
else print $1 "/.../" $NF; }
else print $0;}'"'"')'
PS1='$(eval "echo ${MYPS}")$ '
只有当当前目录的深度超过3个目录并且$PWD
的长度大于14个字符时,awk中的
if (NF > 4 && length($0) > 14)
条件才会应用特殊处理,否则PS1将保持为$PWD
.
if (NF > 4 && length($0) > 14)
condition in awk will only apply special handling when your current directory is more than 3 directories deep AND if length of $PWD
is more than 14 characters otherwise and it will keep PS1 as $PWD
.
例如:如果当前目录是~/workspace/projects/project1$
,则PS1将是~/workspace/projects/project1$
eg: if current directory is ~/workspace/projects/project1$
then PS1 will be ~/workspace/projects/project1$
.bashrc中的上述效果在您的PS1上如下所示:
Effect of above in .bashrc will be as follows on your PS1:
~$ cd ~/workspace/projects/project1/folder1/test
~/workspace/.../folder1/test$ cd ..
~/workspace/.../project1/folder1$ cd ..
~/workspace/.../project1$ cd ..
~/.../projects$ cd ..
~/workspace$ cd ..
~$
注意更改目录时提示的更改方式.让我知道这是否不是您想要的.
Notice how prompt is changing when I change directories. Let me know if this is not what you wanted.
这篇关于如何缩短命令行提示符的当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!