How can I display the current branch and folder path in terminal?

前端 未结 13 1093
无人共我
无人共我 2020-12-04 07:09

I\'ve been watching some of the Team Treehouse videos and they have a very nice looking terminal when working with Git.

For example they have (something similar):

相关标签:
13条回答
  • 2020-12-04 07:46

    For anyone looking for how to do this in macOS Catalina or above (10.15+ incl. Big Sur 11.0) which has deprecated bash in favour of zsh, here is my .zshrc file:

    parse_git_branch() {
        git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
    }
    COLOR_DEF=$'\e[0m'
    COLOR_USR=$'\e[38;5;243m'
    COLOR_DIR=$'\e[38;5;197m'
    COLOR_GIT=$'\e[38;5;39m'
    NEWLINE=$'\n'
    setopt PROMPT_SUBST
    export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
    

    If you don't like the colours I have used, replace the 243/197/39 values with the colour codes as defined here: https://misc.flogisoft.com/bash/tip_colors_and_formatting

    0 讨论(0)
  • 2020-12-04 07:46

    In 2019, I think git branch --show-current is a better command than the accepted answer.

    $ git branch --show-current
    master
    

    (Added in git 2.22 release in June 2019)

    It runs much faster as it doesn't need to iterate through all branches. Similarly git branch should be avoided too in the command prompt as it slows down your prompt if you have many local branches.

    Put it in a function to use anywhere on command prompt:

      # This function returns '' in all below cases:
      #   - git not installed or command not found
      #   - not in a git repo
      #   - in a git repo but not on a branch (HEAD detached)
      get_git_current_branch() {
        git branch --show-current 2> /dev/null
      }
    

    More context:

    $ git version
    git version 2.23.0
    
    0 讨论(0)
  • 2020-12-04 07:47

    My prompt includes:

    • Exit status of last command (if not 0)
    • Distinctive changes when root
    • rsync-style user@host:pathname for copy-paste goodness
    • Git branch, index, modified, untracked and upstream information
    • Pretty colours

    Example: To do this, add the following to your ~/.bashrc:

    #
    # Set the prompt #
    #
    
    # Select git info displayed, see /usr/share/git/completion/git-prompt.sh for more
    export GIT_PS1_SHOWDIRTYSTATE=1           # '*'=unstaged, '+'=staged
    export GIT_PS1_SHOWSTASHSTATE=1           # '$'=stashed
    export GIT_PS1_SHOWUNTRACKEDFILES=1       # '%'=untracked
    export GIT_PS1_SHOWUPSTREAM="verbose"     # 'u='=no difference, 'u+1'=ahead by 1 commit
    export GIT_PS1_STATESEPARATOR=''          # No space between branch and index status
    export GIT_PS1_DESCRIBE_STYLE="describe"  # detached HEAD style:
    #  contains      relative to newer annotated tag (v1.6.3.2~35)
    #  branch        relative to newer tag or branch (master~4)
    #  describe      relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
    #  default       exactly eatching tag
    
    # Check if we support colours
    __colour_enabled() {
        local -i colors=$(tput colors 2>/dev/null)
        [[ $? -eq 0 ]] && [[ $colors -gt 2 ]]
    }
    unset __colourise_prompt && __colour_enabled && __colourise_prompt=1
    
    __set_bash_prompt()
    {
        local exit="$?" # Save the exit status of the last command
    
        # PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1
        local PreGitPS1="${debian_chroot:+($debian_chroot)}"
        local PostGitPS1=""
    
        if [[ $__colourise_prompt ]]; then
            export GIT_PS1_SHOWCOLORHINTS=1
    
            # Wrap the colour codes between \[ and \], so that
            # bash counts the correct number of characters for line wrapping:
            local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'
            local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'
            local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'
            local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'
            local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]'
            local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'
            local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'
            local None='\[\e[0m\]' # Return to default colour
    
            # No username and bright colour if root
            if [[ ${EUID} == 0 ]]; then
                PreGitPS1+="$BRed\h "
            else
                PreGitPS1+="$Red\u@\h$None:"
            fi
    
            PreGitPS1+="$Blu\w$None"
        else # No colour
            # Sets prompt like: ravi@boxy:~/prj/sample_app
            unset GIT_PS1_SHOWCOLORHINTS
            PreGitPS1="${debian_chroot:+($debian_chroot)}\u@\h:\w"
        fi
    
        # Now build the part after git's status
    
        # Highlight non-standard exit codes
        if [[ $exit != 0 ]]; then
            PostGitPS1="$Red[$exit]"
        fi
    
        # Change colour of prompt if root
        if [[ ${EUID} == 0 ]]; then
            PostGitPS1+="$BRed"'\$ '"$None"
        else
            PostGitPS1+="$Mag"'\$ '"$None"
        fi
    
        # Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1
        __git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)'
    
        # echo '$PS1='"$PS1" # debug    
        # defaut Linux Mint 17.2 user prompt:
        # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ '
    }
    
    # This tells bash to reinterpret PS1 after every command, which we
    # need because __git_ps1 will return different text and colors
    PROMPT_COMMAND=__set_bash_prompt
    
    0 讨论(0)
  • 2020-12-04 07:49

    It's not about a plugin. It's about prompt tricks in the shell.

    For a cool setup in bash, check out the dotfiles project of this guy:

    https://github.com/mathiasbynens/dotfiles

    To get a fancy prompt, include the .bash_prompt in your ~/.bash_profile or ~/.bashrc.

    To get the exact same prompt as in your question, change the export PS1 line at the end of .bash_prompt like this:

    export PS1="\[${BOLD}${MAGENTA}\]\u\[$WHITE\]@\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]"
    

    I ended up using all the .bash* files from this repository about a month ago, and it's been really useful for me.

    For Git, there are extra goodies in .gitconfig.

    And since you're a mac user, there are even more goodies in .osx.

    0 讨论(0)
  • 2020-12-04 07:49

    To expand on the existing great answers, a very simple way to get a great looking terminal is to use the open source Dotfiles project.

    https://github.com/mathiasbynens/dotfiles


    enter image description here


    Installation is dead simple on OSX and Linux. Run the following command in Terminal.

    git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
    

    This is going to:

    1. Git clone the repo.
    2. cd into the folder.
    3. Run the installation bash script.
    0 讨论(0)
  • 2020-12-04 07:53

    The git package installed on your system includes bash files to aid you in creating an informative prompt. To create colors, you will need to insert terminal escape sequences into your prompt. And, the final ingredient is to update your prompt after each command gets executed by using the built-in variable PROMPT_COMMAND.

    Edit your ~/.bashrc to include the following, and you should get the prompt in your question, modulo some color differences.

    #
    # Git provides a bash file to create an informative prompt. This is its standard
    # location on Linux. On Mac, you should be able to find it under your Git
    # installation. If you are unable to find the file, I have a copy of it on my GitHub.
    #
    # https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-prompt.sh
    #
    source /usr/share/git/completion/git-prompt.sh
    
    #
    # Next, we need to define some terminal escape sequences for colors. For a fuller
    # list of colors, and an example how to use them, see my bash color file on my GitHub
    # and my coniguration for colored man pages.
    #
    # https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh
    # https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh
    #
    color_start='\e['
    color_end='m'
    color_reset='\e[0m'
    color_bg_blue='44'
    
    #
    # To get a fancy git prompt, it's not sufficient to set PS1. Instead, we set PROMPT_COMMAND,
    # a built in Bash variable that gets evaluated before each render of the prompt.
    #
    export PROMPT_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\u@\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \""
    
    #
    # If you find that the working directory that appears in the prompt is ofter too long,
    # then trim it.
    #
    export PROMPT_DIRTRIM=3
    
    0 讨论(0)
提交回复
热议问题