Advertisement
tikaszvince

bash prompt

Dec 15th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.79 KB | None | 0 0
  1. # ~/.bashrc: executed by bash(1) for non-login shells.
  2. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
  3. # for examples
  4.  
  5. # If not running interactively, don't do anything
  6. [ -z "$PS1" ] && return
  7.  
  8. # don't put duplicate lines in the history. See bash(1) for more options
  9. # ... or force ignoredups and ignorespace
  10. HISTCONTROL=ignoredups:ignorespace
  11.  
  12. # append to the history file, don't overwrite it
  13. shopt -s histappend
  14.  
  15. # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
  16. HISTSIZE=1000
  17. HISTFILESIZE=2000
  18.  
  19. # check the window size after each command and, if necessary,
  20. # update the values of LINES and COLUMNS.
  21. shopt -s checkwinsize
  22.  
  23. # make less more friendly for non-text input files, see lesspipe(1)
  24. [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
  25.  
  26. # set variable identifying the chroot you work in (used in the prompt below)
  27. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  28.     debian_chroot=$(cat /etc/debian_chroot)
  29. fi
  30.  
  31. # set a fancy prompt (non-color, unless we know we "want" color)
  32. case "$TERM" in
  33.     xterm-color) color_prompt=yes;;
  34. esac
  35.  
  36. # uncomment for a colored prompt, if the terminal has the capability; turned
  37. # off by default to not distract the user: the focus in a terminal window
  38. # should be on the output of commands, not on the prompt
  39. force_color_prompt=yes
  40.  
  41. if [ -n "$force_color_prompt" ]; then
  42.     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  43.     # We have color support; assume it's compliant with Ecma-48
  44.     # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
  45.     # a case would tend to support setf rather than setaf.)
  46.     color_prompt=yes
  47.     else
  48.     color_prompt=
  49.     fi
  50. fi
  51.  
  52. if [ "$color_prompt" = yes ]; then
  53.     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  54. else
  55.     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
  56. fi
  57. unset color_prompt force_color_prompt
  58.  
  59. # If this is an xterm set the title to user@host:dir
  60. case "$TERM" in
  61. xterm*|rxvt*)
  62.     PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
  63.     ;;
  64. *)
  65.     ;;
  66. esac
  67.  
  68. # enable color support of ls and also add handy aliases
  69. if [ -x /usr/bin/dircolors ]; then
  70.     test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
  71.     alias ls='ls --color=auto'
  72.     #alias dir='dir --color=auto'
  73.     #alias vdir='vdir --color=auto'
  74.  
  75.     alias grep='grep --color=auto'
  76.     alias fgrep='fgrep --color=auto'
  77.     alias egrep='egrep --color=auto'
  78. fi
  79.  
  80. # some more ls aliases
  81. alias ll='ls -alF'
  82. alias la='ls -A'
  83. alias l='ls -CF'
  84.  
  85. # Add an "alert" alias for long running commands.  Use like so:
  86. #   sleep 10; alert
  87. alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
  88.  
  89. # Alias definitions.
  90. # You may want to put all your additions into a separate file like
  91. # ~/.bash_aliases, instead of adding them here directly.
  92. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
  93.  
  94. if [ -f ~/.bash_aliases ]; then
  95.     . ~/.bash_aliases
  96. fi
  97.  
  98. # enable programmable completion features (you don't need to enable
  99. # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
  100. # sources /etc/bash.bashrc).
  101. if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
  102.     . /etc/bash_completion
  103. fi
  104.  
  105. ###############x
  106.  
  107. # This code was auto generated by with these options:
  108. # http://andrewray.me/bash-prompt-builder/index.html#git=1&color-git=3&git-prefix=1&color-git-prefix=3&git-ahead=1&color-git-ahead=6b&git-modified=1&color-git-modified=3&git-conflicted=1&color-git-conflicted=1&git-revno=1&color-git-revno=3&color-git-bisect=5&option-submodule=1&color-option-submodule=5&git-ontag=1&color-git-ontag=3&color-hg=5&color-hg-prefix=5&color-hg-modified=5&color-hg-conflicted=1&color-hg-revno=5&color-hg-bisect=3&color-hg-patches=3&svn=1&color-svn=2b&svn-modified=1&color-svn-modified=3b&svn-revno=1&color-svn-revno=Well%2C%20we're%20fucked&option-modified=%E2%9C%8E&option-conflict=%E2%98%A2&color-option-conflict=3&max-conflicted-files=0&option-nobranch=no%20branch!&color-option-nobranch=1&bisecting-text=bisecting&submodule-text=%5Bsubmodule%5D%20
  109.  
  110. MAX_CONFLICTED_FILES=0
  111. DELTA_CHAR="✎"
  112. CONFLICT_CHAR="☢"
  113. BISECTING_TEXT="bisecting"
  114. NOBRANCH_TEXT="no branch!"
  115. SUBMODULE_TEXT="[submodule] "
  116.  
  117. # Colors for prompt
  118. COLOR_RED=$(tput sgr0 && tput setaf 1)
  119. COLOR_GREEN=$(tput sgr0 && tput setaf 2)
  120. COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
  121. COLOR_BLUE=$(tput sgr0 && tput setaf 4)
  122. COLOR_MAGENTA=$(tput sgr0 && tput setaf 5)
  123. COLOR_CYAN=$(tput sgr0 && tput setaf 6)
  124. COLOR_GRAY=$(tput sgr0 && tput setaf 7)
  125. COLOR_WHITE=$(tput sgr0 && tput setaf 7 && tput bold)
  126. COLOR_LIGHTRED=$(tput sgr0 && tput setaf 1 && tput bold)
  127. COLOR_LIGHTGREEN=$(tput sgr0 && tput setaf 2 && tput bold)
  128. COLOR_LIGHTYELLOW=$(tput sgr0 && tput setaf 3 && tput bold)
  129. COLOR_LIGHTBLUE=$(tput sgr0 && tput setaf 4 && tput bold)
  130. COLOR_LIGHTMAGENTA=$(tput sgr0 && tput setaf 5 && tput bold)
  131. COLOR_LIGHTCYAN=$(tput sgr0 && tput setaf 6 && tput bold)
  132.  
  133. COLOR_RESET=$(tput sgr0)
  134.  
  135.  
  136. _svn_dir=""
  137. function _svn_check {
  138.     parent=""
  139.     grandparent="."
  140.  
  141.     while [ -d "$grandparent/.svn" ]; do
  142.         parent=$grandparent
  143.         grandparent="$parent/.."
  144.     done
  145.  
  146.     if [ ! -z "$parent" ]; then
  147.         _svn_dir=`cd $parent; pwd`
  148.         return 0
  149.     else
  150.         return 1
  151.     fi
  152. }
  153.  
  154. _git_dir=""
  155. function _git_check {
  156.     _git_dir=`git rev-parse --show-toplevel 2> /dev/null`
  157.     if [[ "$_git_dir" == "" ]]; then
  158.         return 1
  159.     else
  160.         return 0
  161.     fi
  162. }
  163.  
  164. function is_submodule() {
  165.     local parent_git=`cd "$_git_dir/.." && git rev-parse --show-toplevel 2> /dev/null`
  166.     if [[ -n $parent_git ]]; then
  167.         local submodules=`cd $parent_git && git submodule --quiet foreach 'echo $path'`
  168.         for line in $submodules; do
  169.             cd "$parent_git/$line"
  170.             if [[ `pwd` = $_git_dir ]]; then return 0; fi
  171.         done
  172.     fi
  173.     return 1
  174. }
  175.  
  176. dvcs_function="
  177.    # Figure out what repo we are in
  178.    _git_check \
  179.        || _svn_check
  180.  
  181.    # Build the prompt!
  182.    prompt=\"\"
  183.  
  184.    # If we are in git ...
  185.    if [ -n \"\$_git_dir\" ]; then
  186.        # find current branch
  187.        gitBranch=\$(git symbolic-ref HEAD 2> /dev/null)
  188.        gitStatus=\`git status\`
  189.  
  190.        # Figure out current branch, or if we are bisecting, or lost in space
  191.        bisecting=\"\"
  192.        noBranch=\"\"
  193.        if [ -z \"\$gitBranch\" ]; then
  194.            bisect=\$(git rev-list --bisect 2> /dev/null | cut -c1-7)
  195.            if [ -z \"\$bisect\" ]; then
  196.            noBranch=True
  197.            gitBranch=\"\\[\$COLOR_LIGHTRED\\]\$NOBRANCH_TEXT\\[\$COLOR_YELLOW\\]\"
  198.        else
  199.            bisecting=\"\\[\$COLOR_LIGHTGREEN\\]\$BISECTING_TEXT:\"\$bisect\"\\[\$COLOR_YELLOW\\]\"
  200.            gitBranch=\"\"
  201.        fi
  202.        fi
  203.  
  204.        # changed *tracked* files in local directory?
  205.        gitChange=\$(echo \$gitStatus | ack-grep 'modified:|deleted:|new file:')
  206.        if [ -n \"\$gitChange\" ]; then
  207.            gitChange=\"\\[\$COLOR_LIGHTYELLOW\\] \\[`tput sc`\\]  \\[`tput rc`\\]\\[\$DELTA_CHAR\\]\"
  208.        fi
  209.  
  210.        # output the branch and changed character if present
  211.        prompt=\$prompt\"\\[\$COLOR_YELLOW\\](\"
  212.  
  213.        if is_submodule; then
  214.            prompt=\$prompt\"\\[\$COLOR_MAGENTA\\]\$SUBMODULE_TEXT\\[\$COLOR_YELLOW\\]\"
  215.        fi
  216.  
  217.        prefix=\"\\[\$COLOR_LIGHTYELLOW\\]git:\\[\$COLOR_YELLOW\\]\"
  218.        prompt=\$prompt\$prefix\${gitBranch#refs/heads/}\$bisecting
  219.        revNo=\$(git rev-parse HEAD 2> /dev/null | cut -c1-7) || return
  220.  
  221.        # If bisecting don't output revno
  222.        if [ -z \"\$bisecting\" ] && [ -z \"\$noBranch\" ];then
  223.            prompt=\$prompt\":\$revNo\"
  224.        fi
  225.        tag=\`git describe --tags --exact 2> /dev/null\`
  226.        if [ -n \"\$tag\" ]; then
  227.            prompt=\"\$prompt\\[\$COLOR_YELLOW\\] \\\"\$tag\\\"\\[\$COLOR_YELLOW\\]\"
  228.        fi
  229.        prompt=\$prompt\"\$gitChange\\[\$COLOR_YELLOW\\])\\[\$COLOR_RESET\\]\"
  230.  
  231.        # How many local commits do you have ahead of origin?
  232.        num=\$(echo \$gitStatus | grep \"Your branch is ahead of\" | awk '{split(\$0,a,\" \"); print a[13];}') || return
  233.        if [ -n \"\$num\" ]; then
  234.            prompt=\$prompt\"\\[\$COLOR_WHITE\\] +\$num\"
  235.        fi
  236.  
  237.        # any conflicts? (sed madness is to remove line breaks)
  238.        files=\$(git ls-files -u | cut -f 2 | sort -u | sed '$(($MAX_CONFLICTED_FILES+1)),1000d' |  sed -e :a -e '\$!N;s/\\\n/, /;ta' -e 'P;D')
  239.    fi
  240.  
  241.    # If we are in subversion ...
  242.    if [ -n \"\$_svn_dir\" ]; then
  243.  
  244.        # changed files in local directory? NOTE: This command is the slowest of the bunch
  245.        svnChange=\$(svn status | ack-grep \"^\[M!\]\" | wc -l)
  246.        if [[ \"\$svnChange\" != \"       0\" ]]; then
  247.            svnChange=\"\\[\$COLOR_LIGHTYELLOW\\]\\[`tput sc`\\]\\[`tput rc`\\]\\[\$DELTA_CHAR\\]\"
  248.        else
  249.            svnChange=\"\"
  250.        fi
  251.  
  252.        # revision number (instead of branch name, silly svn)
  253.        revNo=\`svnversion --no-newline\`
  254.        prompt=\$prompt\"\\[\$COLOR_LIGHTGREEN\\](svn\"
  255.        prompt=\$prompt\"\\[\$COLOR_GRAY\\]:\$revNo\\[\$COLOR_LIGHTGREEN\\] \$svnChange)\\[\$COLOR_RESET\\]\"
  256.    fi
  257.  
  258.    # Show conflicted files if any
  259.    if [ -n \"\$files\" ]; then
  260.        prompt=\$prompt\" \\[\$COLOR_RED\\](\\[\$COLOR_YELLOW\\]\"
  261.        prompt=\$prompt\"\\[`tput sc`\\]  \\[`tput rc`\\]\\[\$COLOR_YELLOW\\]\\[\$CONFLICT_CHAR\\] \"
  262.        prompt=\$prompt\"\\[\$COLOR_RED\\] \${files})\"
  263.    fi
  264.  
  265.    if [ -n \"\$prompt\" ]; then
  266.        prompt=\$prompt\"\$ \\[\$COLOR_RESET\\]\"
  267.    fi
  268.  
  269.    echo -e \$prompt"
  270. # End code auto generated by http://andrewray.me/bash-prompt-builder/index.html
  271.  
  272. PS1="$PS1\$(${dvcs_function})"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement