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