Shortening my prompt in Zsh

21,872

Solution 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.

Solution 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

Solution 3

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.

Solution 4

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 ~ %

Solution 5

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.

Share:
21,872

Related videos on Youtube

bgenchel
Author by

bgenchel

Updated on December 06, 2021

Comments

  • bgenchel
    bgenchel over 2 years

    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!

    • Admin
      Admin almost 8 years
      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?
    • shellter
      shellter almost 8 years
      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.
    • AnimiVulpis
      AnimiVulpis over 6 years
      Maybe consider accepting an answer or help us to help you
    • David C. Rankin
      David C. Rankin almost 5 years
    • RichieHH
      RichieHH over 4 years
      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.
  • bgenchel
    bgenchel almost 8 years
    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.
  • AnimiVulpis
    AnimiVulpis almost 8 years
    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.
  • bgenchel
    bgenchel almost 8 years
    I think it would be useful to have the final two extensions. something like '~/.../outer_folder/current_folder'
  • bgenchel
    bgenchel almost 8 years
    I made a copy of the theme called myagnoster.zsh-theme, and am editing in there
  • AnimiVulpis
    AnimiVulpis almost 4 years
    Now an even better solution exists: Take a look at powerlevel10k
  • zor-el
    zor-el over 3 years
    This is placing the limitation on displayed directories, not characters. Much nicer IMO.
  • user3768495
    user3768495 almost 3 years
    I like this better since no extension is required. Thank you!