Shortening my prompt in Zsh

后端 未结 4 1889
青春惊慌失措
青春惊慌失措 2021-02-04 04:39

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 a

相关标签:
4条回答
  • 2021-02-04 05:17

    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.

    0 讨论(0)
  • 2021-02-04 05:36

    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.

    0 讨论(0)
  • 2021-02-04 05:37

    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.

    0 讨论(0)
  • 2021-02-04 05:42

    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

    0 讨论(0)
提交回复
热议问题