aboutsummaryrefslogtreecommitdiff
path: root/shellrc.d/common
blob: 8a30f3b2ad98c56b563aea5d5ecee57482c49268 (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
# vim: ft=sh:

alias ls='ls --color=auto'
eval $(dircolors -b)
alias ll='ls -lh'
alias df='df -h'
alias du='du -h'
alias grep='grep --color=auto'
alias ip='ip -c'
alias git='LANG=en_GB git'
alias gdb='gdb -q'
alias cgdb='cgdb -q'
alias octave='octave-cli -q'
alias ssh='TERM="xterm-256color" ssh'
alias gst='git status'
alias v='vim'
alias watch='watch -n 1 -c -t -d'

export LESS=-R
export LESS_TERMCAP_mb=$'\E[1;31m'
export LESS_TERMCAP_md=$'\E[1;36m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[1;32m'
[ -x /usr/bin/src-hilite-lesspipe.sh ] && export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"

# Systemd aliases if we are running systemd
if pidof systemd >/dev/null 2>/dev/null; then
	alias sctl='sudo systemctl'
	alias usctl='systemctl --user'
	alias jrn='journalctl'
	alias ujrn='jrn --user'
fi


# Run process in background
tobg() {
	"$@" >/dev/null 2>&1 &
}

# Generate random password (optionally takes length of password as first argument)
genpasswd() {
	local l=$1
	[ -n "$l" ] || l=16
	tr -dc A-Za-z0-9_ < /dev/urandom | head -c "$l" | xargs
}

# Generate random hex number of given lenght
genhex() {
	if [ -z "$1" ]; then
		echo "Size is required as first argument!" >&2
		return 1
	fi
	tr -dc 0-9A-F < /dev/urandom | head -c "$1" | xargs
}

# Automatic branch merger (merge branch, push it to server and remove branch)
# Expects name of the branch as argument
# It fails if it's not fast forward merge and if there is fixup! commit.
gitbmerge() {
	(
		set -e
		if [ -n "$(git log --grep="^fixup\!" HEAD.."$1")" ]; then
			echo "First squash fixups!"
			exit 1
		fi
		local WT="$(git worktree list | sed -nE "/\[$1\]/{s/([^ ]+) .*/\1/p}")"
		if [ -n "$WT" ]; then
			rm -r "$WT"
			git worktree prune
		fi
		git merge --ff-only "$1" && git push && git branch -d "$1" && git push origin :"$1"
	)
}

# Create new branch from HEAD
gitbnew() {
	git branch "$1" HEAD
	local NW="$(git rev-parse --show-toplevel)-$1"
	git worktree add "$NW" $1
	cd "$NW"
	git submodule update --init --recursive
}

# Run given command every second
dorepeat() {
	while true; do
		"$@"
		sleep 1
		echo
	done
}

# Clear all ssh control masters
ssh-clear() {
	rm -rf ~/.cache/ssh
	mkdir -p ~/.cache/ssh
}

# Chroot to bash to given path
chroot-bash() {
	sudo chroot "$1" /bin/bash
}

# Run command with inotifywait
# First argument has to be files then -- is expected and everything else
# is command to be executed when file changes.
inrun () {
	(
	set -e
	local TMPFS="$(mktemp --tmpdir inrun.XXXXXXXX)"
	trap "rm '$TMPFS'; trap '' EXIT; exit 0" EXIT INT QUIT TERM ABRT
	while [ $# -gt 0 -a "$1" != "--" ]; do
		echo "$1" >> "$TMPFS"
		shift
	done
	if [ $# -le 1 ]; then
		echo "Usage: inrun FILE.. -- COMMAND"
		return 1
	fi
	shift
	while true; do
		inotifywait -qe close_write --fromfile "$TMPFS" || true
		"$@" || true
	done
	)
}