[TIP] How do I show the git branch with colours in Bash prompt? :: 행복한 프로그래머

posted by 쁘로그램어 2017. 12. 19. 11:01

bash prompt에 git branch name을 color로 표시하는 방법입니다.

.bashrc에 적용하니까 보기가 좋습니다.

참고로 저는 ubuntu 16.04에서 테스트 했습니다.


수정된 .basrc 파일은 첨부 파일을 참고하세요^^

bashrc



1. .bashrc 파일의 아래 부분을 주석 처리하고 내용 추가

#if [ "$color_prompt" = yes ]; then

#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#else

#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

#fi


parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'

}

if [ "$color_prompt" = yes ]; then

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '

else

    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '

fi


2. PS1 부분을 주석처리 하고 내용 추가

# If this is an xterm set the title to user@host:dir

case "$TERM" in

xterm*|rxvt*)

    #PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '

    ;;

*)

    ;;

esac 

 

# 수정 전


# 수정 후(git branch name에 color로 표시)


※ 참고 사이트 ※

https://askubuntu.com/questions/730754/how-do-i-show-the-git-branch-with-colours-in-bash-prompt

https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

https://gist.github.com/justintv/168835


■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

저는 IT 기술에 대해 관심이 많고, 기술적인 토론하는 것을 매우 좋아합니다^^

github following 해주시면 정말로 감사하겠습니다^^

github: https://github.com/jhhwang4195


어떠한 댓글이든 환영합니다^^

댓글 좀 달아주세요^^ (구걸 모드) 

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■


이 글이 도움이 되셨다면 아래에 있는 "♡공감" 버튼을 클릭해주세요