Bash PWD 缩短
我正在寻找一个 bash 函数,它可以缩短长路径名,以防止我的 PS1 变量变得过长。类似以下内容的内容
/this/is/the/path/to/a/really/long/directory/i/would/like/shortened
可能最终会变成:
/t../i../t../p../to/a/r../l../d../i/w../like/shortened
采用路径和要缩短的最大可接受字符数的内容对于我的 .bashrc 文件来说是完美的。
I'm looking for a bash function that will shorten long path names to keep my PS1 variable from getting excessively long. Something along the lines of:
/this/is/the/path/to/a/really/long/directory/i/would/like/shortened
might end up as:
/t../i../t../p../to/a/r../l../d../i/w../like/shortened
something that the took the path and a maximum acceptable number of characters to shorten to would be perfect for my .bashrc file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
没有给出相同的结果,但我的
~/.bashrc
包含将显示的路径限制为最多 20 个字符。如果路径超过 20 个字符,它将显示为
/...d/like/shortened
或~/.../like/shortened
。Doesn't give the same result, but my
~/.bashrc
containswhich limits the path shown to 20 characters max. If the path is over 20 characters, it will be shown like
/...d/like/shortened
or~/.../like/shortened
.这是您可能会喜欢的仅 bash 解决方案。这会将路径的每个部分缩短为仍可以通过制表符完成的最短前缀,并使用 * 而不是 .. 作为填充符。
将长度作为第一个参数,将路径作为可选的第二个参数。如果没有给出第二个参数,它将使用当前工作目录。
这将尝试缩短到给定的长度以下。如果这是不可能的,它只会给出它可以给出的最短路径。
从算法上来说,这可能很糟糕,但最终速度相当快。 (快速 shell 脚本的关键是避免使用子 shell 和外部命令,尤其是在内部循环中。)
根据设计,它仅缩短 2 个或更多字符(“hom*”与“home”的字符数一样多)。
它并不完美。在某些情况下,它不会尽可能缩短,例如,如果有多个文件名共享前缀的文件(如果 foobar1 和 foobar2 存在,则 foobar3 将不会被缩短。)
Here's a bash-only solution that you might like. This shortens each part of the path down to the shortest prefix that can still be tab-completed, and uses * instead of .. as the filler.
Give it the length as the first argument, and the path as the optional second argument. If no second argument is given, it uses the current working directory.
This will try to shorten to under the length given. If that's not possible, it just gives the shortest path it can give.
Algorithmically speaking, this is probably horrible, but it ends up being pretty fast. (The key to quick shell scripts is avoiding subshells and external commands, especially in inner loops.)
By design, it only shortens by 2 or more characters ('hom*' is just as many characters as 'home').
It's not perfect. There are some situations where it won't shorten as much as is possible, like if there are several files whose filenames share a prefix (If foobar1 and foobar2 exist, foobar3 won't be shortened.)
仅供参考,Bash 4+ 中有一个内置的
\w
“缩短器”:将
/var/lib/whatever/foo/bar/baz
缩短为.../foo/bar/baz
。FYI, there is a built-in
\w
"shortener" in Bash 4+:will shorten
/var/lib/whatever/foo/bar/baz
to.../foo/bar/baz
.我对 Evan Krall 的代码做了一些改进。现在,它会检查您的路径是否从 $HOME 开始,并以 ~/ 而不是 /h*/u*/ 开始缩短的变量。
另外,这里有一些我放入 .bashrc 文件中的函数,用于缩小 shell 显示的路径。我不确定像这样编辑 $PWD 是否完全安全,因为某些脚本可能依赖于有效的 $PWD 字符串,但到目前为止,我在偶尔使用时还没有遇到问题。请注意,我将上面的脚本保存为“shortdir”并将其放入我的路径中。
编辑 2010 年 10 月 19 日
在 bash 中使用别名的正确方法是修改
$PS1
变量;这就是提示的解析方式。在大多数情况下(99% 的时间),当前路径在提示字符串中为“\w”。我们可以使用 sed 将其替换为shortdir
,如下所示:I made some improvements to Evan Krall's code. It now checks to see if your path starts in $HOME and begins the shortened variety with ~/ instead of /h*/u*/
Also, here are some functions I put in my .bashrc file to shrink the path shown by the shell. I'm not sure if editing $PWD like this is completely safe as some scripts might depend on a valid $PWD string, but so far I haven't had problems with occasional use. Note that I saved the above script as "shortdir" and put it in my PATH.
EDIT Oct 19 2010
The proper way to do the aliases in bash is by modifying the
$PS1
variable; this is how the prompt is parsed. In MOST cases (99% of the time) the current path is in the prompt string as "\w". We can use sed to replace this withshortdir
, like so:Python 脚本怎么样?这首先会缩短最长的目录名称,一次一个字符,直到达到其长度目标或无法使路径变得更短。它不会缩短路径中的最后一个目录。
(我开始用普通的 shell 脚本编写这个,但是 bash 在字符串操作方面很糟糕。)
示例输出:
How about a Python script? This shortens the longest directory names first, one character at a time until it meets its length goal or cannot get the path any shorter. It does not shorten the last directory in the path.
(I started writing this in plain shell script but man, bash stinks at string manipulation.)
Example output:
这是 Evan 答案的另一种说法:
这个使用加号 (+) 而不是星号 (*) 来表示被截断的路径。它用 ~ 替换 HOME 路径,并完整保留最终目录段。如果最后一段超过 20 个字符,则会将其缩短为制表符补全位并添加省略号 (...)。
在此处下载脚本并将其包含在您的
.bashrc
中:https://raw.github.com/alanctkc/dotfiles/master/.bash_scripts/pwd-prompt.bash
将目录添加到您的
PS1
中,如下所示:Here's another spin on Evan's answer:
This one uses plus (+) instead of an asterisk (*) for truncated paths. It replaces the HOME path with ~, and it leaves the final directory segment intact. If the final segment is over 20 characters, it shortens it to the tab-completable bit and adds an ellipses (...).
Download the script here and include it in your
.bashrc
:https://raw.github.com/alanctkc/dotfiles/master/.bash_scripts/pwd-prompt.bash
Add the directory to your
PS1
like this:这是一个相对简单的 Perl 解决方案。这很短
足以让你可以直接将其嵌入到 PS1 中
而不是调用脚本。它给出了所有的字符
截断的名称而不是替换为“.”
我没有立即找到用“.”替换字符的好方法,
但这是一个丑陋的方法:
Here's a relatively easy perl solution. This is short
enough that you could embed it directly in PS1 rather
than invoking a script. It gives all the characters
of the truncated names rather than replacing with '.'
I'm not immediately seeing a nice way to replace characters with '.',
but here's an ugly way:
试试这个:
它转换
为
transfrom
to
当
$PWD
与$HOME
相同时,不显示任何内容。奖励:您可以修改长度数量以满足您的需要。
Try this:
It transforms
to
transfrom
to
And when
$PWD
same as$HOME
, show nothing.Bonus: you could modify number of length to fit you need.