]> code.delx.au - dotfiles/blob - .bash/interactive
3d1810f0f018a2dc50a9139fac0f7b095ce72a13
[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 # Useful aliases
98 alias ls='ls --color=auto -v'
99 alias ll='ls -hlF'
100 alias la='ls -ha'
101 alias l='ls -halF'
102 alias f='find . -iname'
103 alias webshare='python3 -mhttp.server'
104 alias rm='rm -i'
105 alias less='less -R'
106 alias grep='grep --color=auto --exclude "*.svn-base"'
107 alias scp='scp -o ControlPath=none'
108 alias bc='bc -ql'
109 alias watch='watch -n1'
110 alias sudo='sudo ' # ability to use aliases with sudo
111 alias sudosu='sudo su -l -s /bin/bash'
112 alias _psresources='ps -weo pid,user:16,%cpu,%mem,stat,start_time,bsdtime,args'
113 alias pscpu='_psresources --sort -%cpu|less -S'
114 alias psmem='_psresources --sort -%mem|less -S'
115 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
116 alias pstime='ps -weo pid,user:16,lstart,args --sort start_time|less -S'
117 alias pbcopy='xsel --clipboard --input'
118 alias pbcopym='xsel --input'
119 alias pbpaste='xsel --clipboard --output'
120 alias pbpastem='xsel --output'
121
122
123
124 # Super man!
125 # Colourful headings
126 # Terminal title
127 function man {
128 echo -ne "\033]0;man $@\007"
129
130 env \
131 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
132 LESS_TERMCAP_me=$'\E[0m' \
133 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
134 LESS_TERMCAP_ue=$'\E[0m' \
135 man "$@"
136 }
137
138 # Usage: mcd somedir
139 # Creates the directory if it doesn't exist, and changes into it
140 function mcd {
141 mkdir -p "${1}" &&
142 cd "${1}"
143 }
144
145 # Usage: editf somefile
146 # Does a recursive search of the current directory for somefile, then edits it
147 function editf {
148 find . -iname "${1}" -exec $EDITORBG '{}' +
149 }
150
151 # Usage: edit somefile [otherfiles ...]
152 function edit {
153 $EDITORBG "$@"
154 }
155
156 # Sets the nice and ionice priorities for the current shell to the lowest values
157 function slowshell {
158 ionice -c 3 -p $$
159 renice -n 19 -p $$
160 }
161
162 # SSH to an unknown host and print the new known_hosts entry
163 function ssh_new {
164 local new_known_hosts_file="$(mktemp)"
165 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo Connection ok
166 cat "$new_known_hosts_file"
167 rm -f "$new_known_hosts_file"
168 }
169
170 # SSH without verifying host key
171 function ssh_unsafe {
172 ssh -o StrictHostKeyChecking=no "$@"
173 }
174
175
176 # Bash should check the terminal size after every command terminates
177 shopt -s checkwinsize
178
179 # Don't attempt to tab-complete an empty line
180 shopt -s no_empty_cmd_completion
181
182 # Prevent overwriting existing files on stdout redirection
183 set -o noclobber
184
185 # Better history
186 shopt -s histappend
187 shopt -s cmdhist
188 export HISTCONTROL="erasedups:ignoredups"
189 export HISTSIZE="100000"
190 export HISTTIMEFORMAT="%F %T "
191
192 # Local customisations
193 [ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"
194
195 # Load bash completion if available
196 [ -r "/etc/bash_completion" ] && source "/etc/bash_completion"
197