Customize zsh prompt to separately show tail of current working dir

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

up vote
1
down vote

favorite

So my current prompt looks like this:
~/path/to/dir %

If possible, I’d like it to show the topmost dir separately, like this:
~/path/to (dir) %

I’ve gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs:
~/path/to/dir (dir) %

I just need to remove the last folder “dir” from the first path. Any ideas?

(Note: I asked this question in Stack Overflow and got redirected here.)

share|improve this question

  • One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
    – user11319
    Nov 28 at 19:26

up vote
1
down vote

favorite

So my current prompt looks like this:
~/path/to/dir %

If possible, I’d like it to show the topmost dir separately, like this:
~/path/to (dir) %

I’ve gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs:
~/path/to/dir (dir) %

I just need to remove the last folder “dir” from the first path. Any ideas?

(Note: I asked this question in Stack Overflow and got redirected here.)

share|improve this question

  • One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
    – user11319
    Nov 28 at 19:26

up vote
1
down vote

favorite

up vote
1
down vote

favorite

So my current prompt looks like this:
~/path/to/dir %

If possible, I’d like it to show the topmost dir separately, like this:
~/path/to (dir) %

I’ve gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs:
~/path/to/dir (dir) %

I just need to remove the last folder “dir” from the first path. Any ideas?

(Note: I asked this question in Stack Overflow and got redirected here.)

share|improve this question

So my current prompt looks like this:
~/path/to/dir %

If possible, I’d like it to show the topmost dir separately, like this:
~/path/to (dir) %

I’ve gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs:
~/path/to/dir (dir) %

I just need to remove the last folder “dir” from the first path. Any ideas?

(Note: I asked this question in Stack Overflow and got redirected here.)

zsh prompt cwd

share|improve this question

share|improve this question

share|improve this question

share|improve this question

edited Nov 28 at 20:31

Gilles

524k12610471578

524k12610471578

asked Nov 28 at 19:09

user11319

62

62

  • One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
    – user11319
    Nov 28 at 19:26

  • One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
    – user11319
    Nov 28 at 19:26

One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
– user11319
Nov 28 at 19:26

One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
– user11319
Nov 28 at 19:26

1 Answer
1

active

oldest

votes

up vote
1
down vote

You can say “the last component” with %1d and “the first component” with %-1d but there’s no way to say “all components except the last one”.

You can use a variable expansion instead of a prompt escape sequence.

setopt prompt_subst
PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'

You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.

Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.

setopt prompt_subst
PROMPT='$psvar[1] %#'
chpwd_prompt () {
  local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
  case $HPWD in
    *?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
    *) psvar[1]="($HPWD)";;
  esac
}
chpwd_functions+=(chpwd_prompt)
# Trigger the chpwd hooks once
cd .

share|improve this answer

    Your Answer

    StackExchange.ready(function() {
    var channelOptions = {
    tags: “”.split(” “),
    id: “106”
    };
    initTagRenderer(“”.split(” “), “”.split(” “), channelOptions);

    StackExchange.using(“externalEditor”, function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using(“snippets”, function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: ‘answer’,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: “”,
    imageUploader: {
    brandingHtml: “Powered by u003ca class=”icon-imgur-white” href=”https://imgur.com/”u003eu003c/au003e”,
    contentPolicyHtml: “User contributions licensed under u003ca href=”https://creativecommons.org/licenses/by-sa/3.0/”u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href=”https://stackoverflow.com/legal/content-policy”u003e(content policy)u003c/au003e”,
    allowUrls: true
    },
    onDemand: true,
    discardSelector: “.discard-answer”
    ,immediatelyShowMarkdownHelp:true
    });

    }
    });

    draft saved
    draft discarded

    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f484741%2fcustomize-zsh-prompt-to-separately-show-tail-of-current-working-dir%23new-answer’, ‘question_page’);
    }
    );

    Post as a guest

    Required, but never shown

    1 Answer
    1

    active

    oldest

    votes

    1 Answer
    1

    active

    oldest

    votes

    active

    oldest

    votes

    active

    oldest

    votes

    up vote
    1
    down vote

    You can say “the last component” with %1d and “the first component” with %-1d but there’s no way to say “all components except the last one”.

    You can use a variable expansion instead of a prompt escape sequence.

    setopt prompt_subst
    PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'
    

    You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.

    Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.

    setopt prompt_subst
    PROMPT='$psvar[1] %#'
    chpwd_prompt () {
      local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
      case $HPWD in
        *?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
        *) psvar[1]="($HPWD)";;
      esac
    }
    chpwd_functions+=(chpwd_prompt)
    # Trigger the chpwd hooks once
    cd .
    

    share|improve this answer

      up vote
      1
      down vote

      You can say “the last component” with %1d and “the first component” with %-1d but there’s no way to say “all components except the last one”.

      You can use a variable expansion instead of a prompt escape sequence.

      setopt prompt_subst
      PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'
      

      You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.

      Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.

      setopt prompt_subst
      PROMPT='$psvar[1] %#'
      chpwd_prompt () {
        local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
        case $HPWD in
          *?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
          *) psvar[1]="($HPWD)";;
        esac
      }
      chpwd_functions+=(chpwd_prompt)
      # Trigger the chpwd hooks once
      cd .
      

      share|improve this answer

        up vote
        1
        down vote

        up vote
        1
        down vote

        You can say “the last component” with %1d and “the first component” with %-1d but there’s no way to say “all components except the last one”.

        You can use a variable expansion instead of a prompt escape sequence.

        setopt prompt_subst
        PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'
        

        You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.

        Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.

        setopt prompt_subst
        PROMPT='$psvar[1] %#'
        chpwd_prompt () {
          local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
          case $HPWD in
            *?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
            *) psvar[1]="($HPWD)";;
          esac
        }
        chpwd_functions+=(chpwd_prompt)
        # Trigger the chpwd hooks once
        cd .
        

        share|improve this answer

        You can say “the last component” with %1d and “the first component” with %-1d but there’s no way to say “all components except the last one”.

        You can use a variable expansion instead of a prompt escape sequence.

        setopt prompt_subst
        PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'
        

        You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.

        Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.

        setopt prompt_subst
        PROMPT='$psvar[1] %#'
        chpwd_prompt () {
          local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
          case $HPWD in
            *?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
            *) psvar[1]="($HPWD)";;
          esac
        }
        chpwd_functions+=(chpwd_prompt)
        # Trigger the chpwd hooks once
        cd .
        

        share|improve this answer

        share|improve this answer

        share|improve this answer

        answered Nov 28 at 20:31

        Gilles

        524k12610471578

        524k12610471578

            draft saved
            draft discarded

            Thanks for contributing an answer to Unix & Linux Stack Exchange!

            • Please be sure to answer the question. Provide details and share your research!

            But avoid

            • Asking for help, clarification, or responding to other answers.
            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.

            Some of your past answers have not been well-received, and you’re in danger of being blocked from answering.

            Please pay close attention to the following guidance:

            • Please be sure to answer the question. Provide details and share your research!

            But avoid

            • Asking for help, clarification, or responding to other answers.
            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.

            draft saved

            draft discarded

            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f484741%2fcustomize-zsh-prompt-to-separately-show-tail-of-current-working-dir%23new-answer’, ‘question_page’);
            }
            );

            Post as a guest

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Required, but never shown

            Related Post

            Leave a Reply

            Your email address will not be published. Required fields are marked *