Shortening my prompt in Zsh

放肆的年华 提交于 2020-06-24 08:24:05

问题


I'm having a lot of trouble getting zsh to shorten my prompt. I'm currently using zsh with the agnoster theme and oh-my-zsh package manager.

My prompt currently gets annoyingly long during work, usually around 110 characters, taking up the entire length of my terminal, which is just not very aesthetically pleasing.

I've looked at a few other people's .zshrc's and attempts to modify their prompt, but nothing seems to work in mine. I've tried copying many, many things into my .zshrc and have seen no effects.

My most recent attempt was to try to copy the prompt block from https://stackoverflow.com/a/171564/2416097

Nothing. Even when I disabled my theme while having this block included, the prompt is still at full length.

Additionally, I can't seem to find any simple or straightforward guides on how to format my prompt in the first place. Most of the results I found while searching only yielded long format strings without explanation or instruction on use.

Any help appreciated!


回答1:


Old question, I know, but as an alternative solution I just discovered powerlevel9k, an extension of agnoster (they appear practically identical bar a fair few tweaks), which has this functionality built in.

Just set it as your zsh theme, then in .zshrc set

POWERLEVEL9K_SHORTEN_DIR_LENGTH=2

which ensures that only two directories are listed.

Alternate options are outlined in the readme.




回答2:


First you would have to copy the theme into a different one in order to customize it to your liking.

  • Copy agnoster.zsh-theme to e.g. mytheme.zsh-theme and select it in your .zshrc
  • Then modify the theme to your liking

I looked at the agnoster theme and found a place where you could save space.

prompt_dir() {
    prompt_segment blue $CURRENT_FG ' %~ '
}

could be changed to

prompt_dir() {
    prompt_segment blue $CURRENT_FG ' %25<...<%~%<< '
}

This will truncate your path to 25 characters and replacing more with ... How this works is described in the zsh manual (linked below).

Short explanation is:

  • %25<...< will truncate everything that is longer than 25 characters to ...
  • %<< will basically tell zsh that anything after this should not be truncated (limiting the truncation to the path part)

I leave it to you to find more places where you can save space by.

And for more customization needs take a look at zsh: 13 Prompt Expansion




回答3:


In the Zsh themes folder you should search for this file agnoster.zsh-theme , open with an editor and change this piece of code :

prompt_dir() {
    prompt_segment blue $CURRENT_FG ' %~ '
}

with this :

prompt_dir() {
  prompt_segment blue $CURRENT_FG "%c"
}

This will prompt the current directory instead of the full path.



来源:https://stackoverflow.com/questions/37286971/shortening-my-prompt-in-zsh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!