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