dotfiles/.bashrc

187 lines
5.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Clean and minimalistic Bash prompt
# Author: Artem Sapegin, sapegin.me
#
# Inspired by: https://github.com/sindresorhus/pure & https://github.com/dreadatour/dotfiles/blob/master/.bash_profile
#
# Notes:
# - $local_username - username you dont want to see in the prompt - can be defined in ~/.bashlocal : `local_username="admin"`
# - Colors ($RED, $GREEN) - defined in ../tilde/bash_profile.bash
#
# User color
case $(id -u) in
0) user_color="$RED" ;; # root
*) user_color="$GREEN" ;;
esac
# Symbols
prompt_symbol=""
prompt_clean_symbol="☀ "
prompt_dirty_symbol="☂ "
prompt_venv_symbol="☁ "
function prompt_command() {
# Local or SSH session?
local remote=
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
# Git branch name and work tree status (only when we are inside Git working tree)
local git_prompt=
if [[ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]]; then
# Branch name
local branch="$(git symbolic-ref HEAD 2>/dev/null)"
branch="${branch##refs/heads/}"
# Working tree status (red when dirty)
local dirty=
# Modified files
git diff --no-ext-diff --quiet --exit-code --ignore-submodules 2>/dev/null || dirty=1
# Untracked files
[ -z "$dirty" ] && test -n "$(git status --porcelain)" && dirty=1
# Format Git info
if [ -n "$dirty" ]; then
git_prompt=" $RED$prompt_dirty_symbol$branch$NOCOLOR"
else
git_prompt=" $GREEN$prompt_clean_symbol$branch$NOCOLOR"
fi
fi
# Virtualenv
local venv_prompt=
if [ -n "$VIRTUAL_ENV" ]; then
venv_prompt=" $BLUE$prompt_venv_symbol$(basename $VIRTUAL_ENV)$NOCOLOR"
fi
# Only show username if not default
local user_prompt=
[ "$USER" != "$local_username" ] && user_prompt="$user_color$USER$NOCOLOR"
# Show hostname inside SSH session
local host_prompt=
[ -n "$remote" ] && host_prompt="@$YELLOW$HOSTNAME$NOCOLOR"
# Show delimiter if user or host visible
local login_delimiter=
[ -n "$user_prompt" ] || [ -n "$host_prompt" ] && login_delimiter=":"
# Format prompt
first_line="$user_prompt$host_prompt$login_delimiter$WHITE\w$NOCOLOR$git_prompt$venv_prompt"
# Text (commands) inside \[...\] does not impact line length calculation which fixes stange bug when looking through the history
# $? is a status of last command, should be processed every time prompt prints
second_line="\`if [ \$? = 0 ]; then echo \[\$CYAN\]; else echo \[\$RED\]; fi\`\$prompt_symbol\[\$NOCOLOR\] "
PS1="\n$first_line\n$second_line"
# Multiline command
PS2="\[$CYAN\]$prompt_symbol\[$NOCOLOR\] "
# Terminal title
local title="$(basename "$PWD")"
[ -n "$remote" ] && title="$title \xE2\x80\x94 $HOSTNAME"
echo -ne "\033]0;$title"; echo -ne "\007"
}
# Show awesome prompt only if Git is istalled
command -v git >/dev/null 2>&1 && PROMPT_COMMAND=prompt_command
bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'
# Sensible Bash - An attempt at saner Bash defaults
# Maintainer: mrzool <http://mrzool.cc>
# Repository: https://github.com/mrzool/bash-sensible
# Version: 0.2.2
## GENERAL OPTIONS ##
# Prevent file overwrite on stdout redirection
# Use `>|` to force redirection to an existing file
set -o noclobber
# Update window size after every command
shopt -s checkwinsize
# Automatically trim long paths in the prompt (requires Bash 4.x)
PROMPT_DIRTRIM=2
# Enable history expansion with space
# E.g. typing !!<space> will replace the !! with your last command
bind Space:magic-space
# Turn on recursive globbing (enables ** to recurse all directories)
shopt -s globstar 2> /dev/null
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob;
## SMARTER TAB-COMPLETION (Readline bindings) ##
# Perform file completion in a case insensitive fashion
bind "set completion-ignore-case on"
# Treat hyphens and underscores as equivalent
bind "set completion-map-case on"
# Display matches for ambiguous patterns at first tab press
bind "set show-all-if-ambiguous on"
# Immediately add a trailing slash when autocompleting symlinks to directories
bind "set mark-symlinked-directories on"
## SANE HISTORY DEFAULTS ##
# Append to the history file, don't overwrite it
shopt -s histappend
# Save multi-line commands as one command
shopt -s cmdhist
# Huge history. Doesn't appear to slow things down, so why not?
HISTSIZE=500000
HISTFILESIZE=100000
# Avoid duplicate entries
HISTCONTROL="erasedups:ignoreboth"
# Don't record some commands
export HISTIGNORE="&:[ ]*:exit:ls:bg:fg:history:clear"
# Use standard ISO 8601 timestamp
# %F equivalent to %Y-%m-%d
# %T equivalent to %H:%M:%S (24-hours format)
HISTTIMEFORMAT='%F %T '
# Enable incremental history search with up/down arrows (also Readline goodness)
# Learn more about this here: http://codeinthehole.com/writing/the-most-important-command-line-tip-incremental-history-searching-with-inputrc/
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\e[C": forward-char'
bind '"\e[D": backward-char'
## BETTER DIRECTORY NAVIGATION ##
# Prepend cd to directory names automatically
shopt -s autocd 2> /dev/null
# Correct spelling errors during tab-completion
shopt -s dirspell 2> /dev/null
# Correct spelling errors in arguments supplied to cd
shopt -s cdspell 2> /dev/null
# This defines where cd looks for targets
# Add the directories you want to have fast access to, separated by colon
# Ex: CDPATH=".:~:~/projects" will look for targets in the current working directory, in home and in the ~/projects folder
CDPATH="."
# This allows you to bookmark your favorite places across the file system
# Define a variable containing a path and you will be able to cd into it regardless of the directory you're in
shopt -s cdable_vars
# Examples:
# export dotfiles="$HOME/dotfiles"
# export projects="$HOME/projects"
# export documents="$HOME/Documents"
# export dropbox="$HOME/Dropbox"