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