summaryrefslogtreecommitdiff
path: root/.config/bash/rc.d/10_aliases.sh
blob: 2e28a5dbcb702ac93053ba6ea4c4c0f078811dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/hint/bash

######################################################################
# Set up colors and settings for all the things                      #
######################################################################
if [ -x "`which dircolors`" ]; then
	eval "$(dircolors -p | cat - "${XDG_CONFIG_HOME}/dir_colors" |
		dircolors -b -)"
	alias ls='ls -1v --color=auto'
	alias dir='dir -v --color=auto'
	alias vdir='vdir -v --color=auto'

	for xgrep in ${PATH//:/\/*grep }/*grep; do
		if [ -f "$xgrep" ]; then
			xgrep=$(basename "$xgrep")
			if [ "$xgrep" != pgrep ]; then
				alias $xgrep="$xgrep --color=auto"
			fi
		fi
	done
	unset xgrep

	wdiff() {
		if [[ -t 1 ]]; then
			local old new off
			if type colordiff &>/dev/null; then
				eval "$(colordiff <(echo old) <(echo new)|sed -rn 's@(.*)[<>] (old|new)(.*)@\2='\''\1'\''\noff='\''\3'\''\n@p')"
			else
				new="$(tput setaf 2)"
				old="$(tput setaf 1)"
				off="$(tput sgr0)"
			fi
			command wdiff \
				-w "$old[-" \
				-x "-]$off" \
				-y "$new{+" \
				-z "+}$off" \
				"$@"
		else
			command wdiff "$@"
		fi
	}

	chardiff() {
		if [[ -t 1 ]]; then
			local old new off
			if type colordiff &>/dev/null; then
				eval "$(colordiff <(echo old) <(echo new)|sed -rn 's@(.*)[<>] (old|new)(.*)@\2='\''\1'\''\noff='\''\3'\''\n@p')"
			else
				new="$(tput setaf 2)"
				old="$(tput setaf 1)"
				off="$(tput sgr0)"
			fi
			command chardiff \
				-w "$old[-" \
				-x "-]$off" \
				-y "$new{+" \
				-z "+}$off" \
				"$@"
		else
			command chardiff "$@"
		fi
	}

	diff() {
		if [[ -t 1 ]]; then
			(
				set -o pipefail
				command diff "$@" | colordiff
			)
		else
			command diff "$@"
		fi
	}

	diffstat() {
		if [[ -t 1 ]]; then
			command diffstat -C "$@"
		else
			command diffstat "$@"
		fi
	}
else
	alias ls='ls -1v'
	alias dir='dir -v'
	alias vdir='vdir -v'
fi

######################################################################
# Set up the standard aliases for ls                                 #
######################################################################
alias ll='ls -l'
alias la='ls -a'
alias l='ls -CF'

######################################################################
# Some preferences for miscellaneous stuff                           #
######################################################################
#alias rm='gvfs-trash'
alias tree='tree --charset utf8'
alias cd=pushd
alias gitk='gitk --all --date-order'
alias userctl='systemctl --user'
alias mv='mv -i'

######################################################################
# These are actually functions :P                                    #
######################################################################
term-title() {
	local fmt=''
	case "$TERM" in
		screen|tmux) fmt='\ek%s\e\\';;
		xterm*|rxvt*) fmt='\e]0;%s\a';;
	esac
	printf "$fmt" "$*"
}
mvln() {
	local name=mvln
	local verbing=moving
	_cpln "$@"
}
cpln() {
	local name=cpln
	local verbing=copying
	_cpln "$@"
}
_cpln() {
	if [[ $# -lt 2 ]]; then
		libremessages error 'Usage: %s FILES... DEST' "$name"
		return 1
	fi
	declare -i i=1
	while [[ $i -lt $# ]]; do
		local src dst target link
		src="${!i}"
		dst="${!#}"
		if [[ ! -L "$src" ]]; then
			libremessages error 'Not a soft link: %s' "$src"
			return 1
		fi
		if [[ -d "$dst" ]]; then
			link="$dst/${src##*/}"
		else
			link="$dst"
		fi
		target=$(readlink -f -- "$src") || return 1
		ln -srT -- "$target" "$link"
		# using `cmp` instead of `[ "$src" -ef "$link" ]` has
		# the use-case where "$link" was a pre-existing link
		# to a duplicate of "$src".
		if ! cmp -- "$src" "$link"; then
			libremessages error 'Failed %s link: %s -> %s' "$verbing" "$src" "$link"
			return 1
		fi
		if [[ "$name" = mvln ]]; then
			rm -f -- "$src"
		fi
		i+=1
	done
}
jarls() {
	jar tf "$1" |
	    sed -n 's/\.class$//p' |
	    LC_ALL=C sort |
	    xargs -r -d $'\n' javap -classpath "$1"
}
tarls() {
	local file
	for file in "$@"; do
		bsdtar tf "$file" | sed "s|^|$file:|"
	done
}
jarmain() {
	jarls "$1" 2>/dev/null |
	    grep -E '(^[a-z]|public static void main\(java\.lang\.String\[\]\))' |
	    grep -B1 '^ '
}