]> code.delx.au - dotfiles/blob - .bash/interactive
screen prompt change
[dotfiles] / .bash / interactive
1 #! bash
2
3
4
5 ##################
6 # Terminal setup #
7 ##################
8
9 # Check for unsupported TERM variable
10 if ! tput init &> /dev/null; then
11 echo "Warning! TERM=$TERM unsupported, using TERM=xterm"
12 export TERM=xterm
13 fi
14
15 # Disable CTRL-s / CTRL-q
16 stty -ixon
17
18 # Sets colour scheme in apps like Vim
19 export COLORFGBG="15;0"
20
21
22
23 #############
24 # Fancy PS1 #
25 #############
26
27 # Revision control status for git, hg, svn
28
29 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
30 function my_git_ps1 {
31 find_up_recurse .git || return
32 GIT_PS1_SHOWDIRTYSTATE=1 \
33 GIT_PS1_SHOWUNTRACKEDFILES=1 \
34 __git_ps1 2> /dev/null
35 }
36
37 function my_hg_ps1 {
38 find_up_recurse .hg || return
39 b="$(hg branch 2>/dev/null)" || return
40 s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
41 echo -n " ($b"
42 [ -n "$s" ] && echo -n " $s"
43 echo -n ")"
44 }
45
46 function my_svn_ps1 {
47 find_up_recurse .svn || return
48 s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
49 [ -z "$s" ] && return
50 echo -n " ($s)"
51 }
52
53 # Two line prompt
54
55 PS1=''
56 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
57 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
58
59 if ! [[ "$TERM" =~ ^screen ]]; then
60 PS1="$PS1"'\n\$ '
61 else
62 PS1="$PS1"'\n\[\033k\033\\\]\$> '
63 fi
64
65
66 ###############
67 # xterm title #
68 ###############
69
70 # hostname:workingdir
71 PROMPT_COMMAND='echo -ne "\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\007"'
72
73
74
75 #################################
76 # Display return codes on error #
77 #################################
78
79 function print_exit_code {
80 _exit_msg="\033[01;33mexit code: $?\033[00m"
81 if [ -z "${BASH_SOURCE[1]}" ]; then
82 echo -e "$_exit_msg"
83 fi
84 unset _exit_msg
85 }
86 trap print_exit_code ERR
87
88
89
90 ################
91 # bash options #
92 ################
93
94 # Bash should check the terminal size after every command terminates
95 shopt -s checkwinsize
96
97 # Don't attempt to tab-complete an empty line
98 shopt -s no_empty_cmd_completion
99
100 # Prevent overwriting existing files on stdout redirection
101 set -o noclobber
102
103 # Better history
104 shopt -s histappend
105 shopt -s cmdhist
106 export HISTCONTROL="erasedups:ignoredups:ignorespace"
107 export HISTSIZE="100000"
108 export HISTTIMEFORMAT="%F %T "
109
110
111
112 ##########################
113 # ls aliases and colours #
114 ##########################
115
116 # GNU ls colours
117 eval $(TERM=xterm dircolors 2> /dev/null)
118
119 # BSD ls colours
120 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
121
122 # Lets find the ls
123 if ls --color=auto -v &> /dev/null; then
124 alias ls='ls --color=auto -v'
125 elif gls --color=auto -v &> /dev/null; then
126 alias ls='gls --color=auto -v'
127 elif ls -G &> /dev/null; then
128 alias ls='ls -G'
129 else
130 alias ls='ls -F'
131 fi
132 alias ll='ls -hlF'
133 alias la='ls -ha'
134 alias l='ls -halF'
135
136
137
138 ##############
139 # ps aliases #
140 ##############
141
142 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
143 if [ "$(uname)" = "Linux" ]; then
144 alias pscpu='_psresources --sort -%cpu|less -S'
145 alias psmem='_psresources --sort -%mem|less -S'
146 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
147 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
148 else
149 alias pscpu='_psresources -r|less -S'
150 alias psmem='_psresources -m|less -S'
151 alias pstime='ps -wAo pid,user,lstart,args|less -S'
152 fi
153
154
155 ##################
156 # Useful aliases #
157 ##################
158
159 alias f='find . -iname'
160 alias webshare='python3 -mhttp.server'
161 if echo x | grep -q --color=auto x &> /dev/null; then
162 alias grep='grep --color=auto'
163 fi
164 alias scp='scp -o ControlPath=none'
165 alias bc='bc -ql'
166 alias watch='watch -n1'
167 alias sudo='sudo ' # ability to use aliases with sudo
168 alias sudosu='sudo su -l -s /bin/bash'
169
170 if ! which pbcopy &> /dev/null; then
171 alias pbcopy='xsel --clipboard --input'
172 alias pbcopym='xsel --input'
173 alias pbpaste='xsel --clipboard --output'
174 alias pbpastem='xsel --output'
175 fi
176
177 # Super man!
178 # Colourful headings
179 # Terminal title
180 function man {
181 echo -ne "\033]0;man $@\007"
182
183 env \
184 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
185 LESS_TERMCAP_me=$'\E[0m' \
186 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
187 LESS_TERMCAP_ue=$'\E[0m' \
188 man --encoding ascii "$@"
189 }
190
191 # Usage: mcd somedir
192 # Creates the directory if it doesn't exist, and changes into it
193 function mcd {
194 mkdir -p "${1}" &&
195 cd "${1}"
196 }
197
198 # Usage: editf somefile
199 # Does a recursive search of the current directory for somefile, then edits it
200 function editf {
201 find . -iname "${1}" -exec $EDITORBG '{}' +
202 }
203
204 # Usage: edit somefile [otherfiles ...]
205 function edit {
206 $EDITORBG "$@"
207 }
208
209 # Sets the nice and ionice priorities for the current shell to the lowest values
210 function slowshell {
211 ionice -c 3 -p $$
212 renice -n 19 -p $$
213 }
214
215 # SSH to an unknown host and print the new known_hosts entry
216 function ssh_new {
217 local new_known_hosts_file="$(mktemp)"
218 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo Connection ok
219 cat "$new_known_hosts_file"
220 rm -f "$new_known_hosts_file"
221 }
222
223 # SSH without verifying host key
224 function ssh_unsafe {
225 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
226 }
227
228
229
230 ###########
231 # The end #
232 ###########
233
234 # Local customisations
235 [ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"
236
237 # Load bash completion if available
238 [ -r "/etc/bash_completion" ] && source "/etc/bash_completion"
239