]> code.delx.au - dotfiles/blob - .bashrc
bash: load ~/.bashrc_local
[dotfiles] / .bashrc
1 # shellcheck disable=SC1090 # -- Can't follow non-constant source
2
3 ###########################
4 # Settings for all shells #
5 ###########################
6
7 # Set umask, depending on the user ID
8 if [ "$(id -un)" = "root" ]; then
9 umask 0022
10 else
11 umask 0007
12 fi
13
14 # Add ~/bin and subdirs to PATH if needed
15 while read -r p; do
16 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
17 done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
18
19 # Set EMAIL from the freedesktop environment.d
20 if ls ~/.config/environment.d/*.conf &> /dev/null; then
21 eval "$(awk -F= '{print $0 ";export " $1}' ~/.config/environment.d/*.conf)"
22 fi
23
24 # Pick up SSH agent socket
25 if [ -z "$SSH_AUTH_SOCK" ]; then
26 if [ -r ~/.ssh-agent.env ]; then
27 source ~/.ssh-agent.env
28 else
29 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
30 export SSH_AUTH_SOCK
31 fi
32 fi
33
34
35 ###########################
36 # Interactive shells only #
37 ###########################
38
39 if [ -z "${PS1}" ]; then
40 return
41 fi
42
43
44 ################
45 # bash options #
46 ################
47
48 # Bash should check the terminal size after every command terminates
49 shopt -s checkwinsize
50
51 # Don't attempt to tab-complete an empty line
52 shopt -s no_empty_cmd_completion
53
54 # Prevent overwriting existing files on stdout redirection
55 set -o noclobber
56
57 # Better history
58 shopt -s histappend
59 shopt -s cmdhist
60 export HISTCONTROL='erasedups:ignoredups:ignorespace'
61 export HISTSIZE='100000'
62 export HISTTIMEFORMAT='%F %T '
63
64
65 ##################
66 # Terminal setup #
67 ##################
68
69 # Disable CTRL-s / CTRL-q
70 stty -ixon
71
72 # hostname:workingdir
73 PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/~}\\007"'
74
75
76 #############
77 # Fancy PS1 #
78 #############
79
80 # Revision control status for git, hg, svn
81
82 function find_up_exists {
83 local d="$PWD"
84 while [ -n "$d" ]; do
85 if [ -e "$d/$1" ]; then
86 return 0
87 fi
88 d="${d%/*}"
89 done
90 return 1
91 }
92
93 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
94 function my_git_ps1 {
95 find_up_exists .git || return
96 GIT_PS1_SHOWDIRTYSTATE=1 \
97 GIT_PS1_SHOWUNTRACKEDFILES=1 \
98 __git_ps1 ' (%s)' 2> /dev/null
99 }
100
101 function my_hg_ps1 {
102 find_up_exists .hg || return
103 local status
104 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
105 echo -n " ($status)"
106 }
107
108 function my_svn_ps1 {
109 find_up_exists .svn || return
110 local status
111 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
112 [ -n "$status" ] && echo -n " ($s)"
113 }
114
115 # Two line prompt
116 PS1=''
117 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
118 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
119
120 if ! [[ "$TERM" =~ ^screen ]]; then
121 PS1="$PS1"'\n\$ '
122 else
123 # matches with shelltitle in .screenrc
124 PS1="$PS1"'\n\[\033k\033\\\]\$> '
125 fi
126
127
128 #################################
129 # Display return codes on error #
130 #################################
131
132 function print_exit_code {
133 _exit_msg="\\033[01;33mexit code: $?\\033[00m"
134 if [ -z "${BASH_SOURCE[1]}" ]; then
135 echo -e "$_exit_msg"
136 fi
137 unset _exit_msg
138 }
139 trap print_exit_code ERR
140
141
142 ###############
143 # Pager setup #
144 ###############
145
146 export PAGER='less'
147 export LESS='RS'
148
149
150 ################
151 # Editor setup #
152 ################
153
154 if emacsclient --version &> /dev/null; then
155 export ALTERNATE_EDITOR='vim'
156 export EDITOR='emacsclient --tty'
157
158 if [[ "$TERM" == screen* ]]; then
159 alias edit='emacsclient --tty'
160 else
161 alias edit='emacsclient --create-frame --no-wait'
162 fi
163 else
164 export EDITOR='vim'
165 alias edit='vim'
166 fi
167
168
169 ##########################
170 # ls aliases and colours #
171 ##########################
172
173 # GNU ls colours
174 eval "$(TERM=xterm dircolors 2> /dev/null)"
175
176 # BSD ls colours
177 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
178
179 # Lets find the ls
180 if ls --color=auto -v &> /dev/null; then
181 alias ls='ls --color=auto -v'
182 elif gls --color=auto -v &> /dev/null; then
183 alias ls='gls --color=auto -v'
184 elif ls -G &> /dev/null; then
185 alias ls='ls -G'
186 else
187 alias ls='ls -F'
188 fi
189 alias ll='ls -hlF'
190 alias la='ls -ha'
191 alias l='ls -halF'
192
193 ##############
194 # ps aliases #
195 ##############
196
197 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
198 if [ "$(uname)" = "Linux" ]; then
199 alias pscpu='_psresources --sort -%cpu|less -S'
200 alias psmem='_psresources --sort -%mem|less -S'
201 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
202 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
203 else
204 alias pscpu='_psresources -r|less -S'
205 alias psmem='_psresources -m|less -S'
206 alias pstime='ps -wAo pid,user,lstart,args|less -S'
207 fi
208
209 #################
210 # Other aliases #
211 #################
212
213 alias f='find . -iname'
214 if echo x | grep -q --color=auto x &> /dev/null; then
215 alias grep='grep --color=auto'
216 fi
217 alias rg='rg -p'
218 alias scp='scp -o ControlPath=none'
219 alias bc='bc -ql'
220 alias watch='watch -n1'
221 alias sudo='sudo ' # ability to use aliases with sudo
222 alias sudosu='sudo su -l -s /bin/bash'
223 alias python='PYTHONSTARTUP=~/.pythonrc.py python3'
224 alias webshare='python3 -mhttp.server'
225
226 if ! command -v pbcopy &> /dev/null; then
227 alias pbcopy='xsel --clipboard --input'
228 alias pbcopym='xsel --input'
229 alias pbpaste='xsel --clipboard --output'
230 alias pbpastem='xsel --output'
231 fi
232
233 # man with coloured headings and a terminal title
234 function man {
235 echo -ne "\\033]0;man $*\\007"
236
237 env \
238 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
239 LESS_TERMCAP_me=$'\E[0m' \
240 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
241 LESS_TERMCAP_ue=$'\E[0m' \
242 LC_CTYPE=C \
243 man "$@"
244 }
245
246 # Creates the directory if it doesn't exist, and changes into it
247 function mcd {
248 # shellcheck disable=SC2164
249 mkdir -p "$1" && cd "$1"
250 }
251
252 # Sets the nice and ionice priorities for the current shell to the lowest values
253 function slowshell {
254 ionice -c 3 -p $$
255 renice -n 19 -p $$
256 }
257
258 # SSH to an unknown host and print the new known_hosts entry
259 function ssh_new {
260 local new_known_hosts_file
261 new_known_hosts_file="$(mktemp)"
262 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo 'Connection ok'
263 cat "$new_known_hosts_file"
264 rm -f "$new_known_hosts_file"
265 }
266
267 # SSH without verifying host key
268 function ssh_unsafe {
269 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
270 }
271
272 ###########
273 # The end #
274 ###########
275
276 [ -r ~/.bashrc_local ] && source ~/.bashrc_local