Shortening my prompt in Zsh
Asked Answered
N

5

32

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!

Noonan answered 17/5, 2016 at 22:4 Comment(5)
What actually makes your prompt so long? Is it showing the full path, for example? You're not even showing what you have for prompt, e.g., how is the actual PROMPT in your .zshrc set?Deal
This Q is not about programming as defined for StackOverflow. It may be more appropriate on the related site superuser.com. Consider using the flag link at the bottom of your Q and ask the moderator to move it. Please don't post the same Q on 2 different sites. Thanks and Good Luck.Joyous
Maybe consider accepting an answer or help us to help youToxophilite
See Shorten path in zsh promptBelvia
It most definitely IS programming as are things like bash exports. Think before you rush in to put people down. If modifying a zsh resource file isn't programming I don't know what is.Ballade
G
29

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.

Gunilla answered 28/2, 2018 at 10:45 Comment(1)
Now an even better solution exists: Take a look at powerlevel10kToxophilite
A
18

add this to ~/.zshrc

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

Explanatory note: %<N>~ will limit the number of path segments leading up to the current directory to N. I.e. %2~ will show only two last segments: the current directory and its parent directory.

Ampulla answered 15/8, 2020 at 11:26 Comment(2)
This is placing the limitation on displayed directories, not characters. Much nicer IMO.Boorman
I like this better since no extension is required. Thank you!Fund
T
17

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

Toxophilite answered 20/7, 2016 at 7:56 Comment(4)
this suggested solution did do something! which is a good step in the right direction; but the proposed formatter just got rid of the path entirely, which is also not good.Noonan
So you now use your local ocpy of the angoster.zsh-theme file? What parts of the prompt would you like to keep. Then we can find a way to modify it to your likings.Toxophilite
I think it would be useful to have the final two extensions. something like '~/.../outer_folder/current_folder'Noonan
I made a copy of the theme called myagnoster.zsh-theme, and am editing in thereNoonan
I
8

In case you stumbled upon this post coming from macOS' zsh like me and want to shorten the prefix/prompt there, I found this article helpful: https://www.makeuseof.com/customize-zsh-prompt-macos-terminal/.

I wanted to remove the name of my MacBook model from the prompt and was able to do so with the following steps:

  1. vim .zshrc
  2. append PROMPT="%n %1~ %# "
  3. Verify with source .zshrc or simply open a new terminal

Before

chris@Christophers-MacBook-Pro ~ %

After

chris ~ %

Immanent answered 1/12, 2021 at 18:56 Comment(0)
F
4

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.

Farant answered 17/5, 2020 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.