-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbashrc
executable file
·163 lines (142 loc) · 5.29 KB
/
bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash -eu
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
[ -z "$PS1" ] && return
function __bash_path_prepend() {
[[ ! -z "$1" ]] && echo "$PATH" | tr ':' '\n' | fgrep "$1" > /dev/null || export PATH="$1:${PATH}"
}
function __bash_path_append() {
[[ ! -z "$1" ]] && echo "$PATH" | tr ':' '\n' | fgrep "$1" > /dev/null || export PATH="${PATH}:$1"
}
function install_python3 {
case "$(os_release | cut -d: -f1)" in
Debian|Ubuntu) dpkg -s python3-venv 2>&1 >/dev/null || sudo apt install -y python3 python3-venv;;
openSUSE)
case "$(os_release | cut -d: -f2)" in
MicroOS) ;; # does not attempt to mutate the file system
*) sudo zypper install -y python3 python3-virtualenv;;
esac;;
*) echo "ERROR: Unsupported distribution: ${distro}" ; return 1;;
esac
}
function mkvirtualenv {
if [ ! -z "$1" ] ;then
local name="${1}"
shift
[[ -d "${HOME}/.virtualenvs" ]] || mkdir -p "${HOME}/.virtualenvs"
python3 -m venv "${HOME}/.virtualenvs/${name}" $@
fi
}
function workon {
if [ ! -z "${1}" ] ;then
source "${HOME}/.virtualenvs/${1}/bin/activate"
for script in ${VIRTUAL_ENV:-${HOME}/.local/share/bash-scripts}/postactivate/head.d/*.sh \
${VIRTUAL_ENV:-${HOME}/.local/share/bash-scripts}/postactivate/postactivate.d/*.sh \
${VIRTUAL_ENV:-${HOME}/.local/share/bash-scripts}/postactivate/tail.d/*.sh ;do
[[ -x "${script}" ]] && echo "sourcing ${script}" && source "${script}"
done
fi
}
function __workon_complete {
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=""
local envs=$( ls -p ~/.virtualenvs | fgrep / | sed 's:/::' )
COMPREPLY=( $(compgen -W "${envs}" -- ${cur}) )
return 0
}
complete -F __workon_complete workon
__bash_path_prepend "$(dirname $(readlink -f "${BASH_SOURCE[0]}"))/bin"
__bash_path_prepend "${HOME}/.local/bin"
__bash_path_prepend "${HOME}/bin"
# make sure python3, python3-pip and python3-venv are installed
install_python3
##FIXME: This is a temporary fix for snaps not being found. Credits: https://www.youtube.com/watch?v=2g-teghxI2A
if [ -d /var/lib/snapd/desktop/applications ] ;then
[[ -d ~/.local/share/applications/ ]] || mkdir -p ~/.local/share/applications/
find -L ~/.local/share/applications -type l -delete
ln -sf /var/lib/snapd/desktop/applications/*.desktop ~/.local/share/applications/
fi
##FIXME: enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
##FIXME: choose text editor on this order: emacs, zile, vim, nano, vi
if [ ! -z $(which emacs 2> /dev/null) ] ;then
VISUAL=emacs
EDITOR="zile"
ALTERNATE_EDITOR="vi -e"
elif [ ! -z $(which zile 2> /dev/null) ] ;then
VISUAL=emacs
EDITOR="zile"
ALTERNATE_EDITOR="vi -e"
elif [ ! -z $(which vim 2> /dev/null) ] ;then
VISUAL=vim
EDITOR=vi
ALTERNATE_EDITOR="vi -e"
elif [ ! -z $(which nano 2> /dev/null) ] ;then
VISUAL=emacs
EDITOR=nano
ALTERNATE_EDITOR="vi -e"
else
VISUAL=vim
EDITOR=vi
ALTERNATE_EDITOR="vi -e"
fi
export VISUAL EDITOR ALTERNATE_EDITOR
# viewing files nicely
case "$(os_release | cut -d: -f1)" in
Debian|Ubuntu)
export LESS=' -R ';
export LESSOPEN='| /usr/share/source-highlight/src-hilite-lesspipe.sh %s';
export LESSCLOSE='| /usr/share/source-highlight/src-hilite-lesspipe.sh %s %s';
export VIEWER=less;
;;
*)
export LESS=' -R ';
export LESSOPEN='| /usr/bin/src-hilite-lesspipe.sh %s';
export LESSCLOSE='| /usr/bin/src-hilite-lesspipe.sh %s %s';
export VIEWER=less;
;;
esac
# define prompt
if [ -x /usr/bin/dircolors ]; then
export PS1='\[\033[01;31m\][$(date "+%Y-%m-%d %H:%M:%S")]\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\] \u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
export PS1='[$(date "+%Y-%m-%d %H:%M:%S")]${debian_chroot:+($debian_chroot)} \u@\h:\w\$ '
fi
# Define notable locations
export DOWNLOADS="${DOWNLOADS:=${HOME}/Downloads}"
export DOCUMENTS="${DOCUMENTS:=${HOME}/Documents}"
export MEDIA="${MEDIA:=${HOME}/Media}"
export SOFTWARE="${SOFTWARE:=$HOME/Downloads}"
export WORKSPACE="${WORKSPACE:=${HOME}/workspace}"
export WORKON_HOME="${WORKON_HOME:=${HOME}/.virtualenvs}"
export TOOLS_HOME="${TOOLS_HOME:=$HOME/tools}"
# Define history processing
# see also: bin/history+
mkdir -p "${HOME}"/.bash_history+
export HISTSIZE=100000
export HISTFILESIZE=-1
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTCONTROL=ignorespace
export HISTIGNORE=ls:ps
export PROMPT_COMMAND='printf "%+7s %s\n" ${$} "$(history 1)" >> "${HOME}/.bash_history+/$(date +%Y%m%d)"'
shopt -s histappend
shopt -s checkwinsize
shopt -s globstar
shopt -s cmdhist
# Create directory structure
for folder in "${HOME}"/.local/share/bash-scripts/postactivate/{head.d,postactivate.d,tail.d} ;do
[[ ! -d "${folder}" ]] && mkdir -p "${folder}"
done
# echo "[ Run user defined initialization scripts ]"
for script in "${HOME}"/bin/bash_*.sh ;do
[[ -x "${script}" ]] && echo "sourcing ${script}" && source "${script}"
done