问题描述
我使用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}$ '
有关目录的例子〜/工作区/项目/ PROJECT1 /文件夹1 /测试
这让PS1为:〜/工作区/ ... /文件夹1 /测试
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}")$ '
如果(NF> 4安培;&安培;长度($ 0)> 14)在AWK当你的当前目录是超过3只适用特别处理
条件目录深,如果 $ PWD
的长度,否则超过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
.
例如:如果当前目录为〜/工作区/项目/ PROJECT1 $
然后PS1将〜/工作区/项目/ 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.
这篇关于击:用漂亮的工作路径的自定义PS1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!