summaryrefslogtreecommitdiffstats
path: root/.bash_aliases
blob: 771f4e0e7397f31e72692f113fc5f2bf29b616c7 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias drm="docker rm"
alias dps="docker ps"
alias dimg="docker images"

function da() {
    docker start $1 && docker attach $1
}

# enable color support of ls and also add handy aliases
if which dircolors >/dev/null 2>&1; then
    [[ -r ~/.dircolors ]] && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
    alias Pgrep='grep -P --color=auto'
fi

alias ggrep='git grep --color=auto'
alias gegrep='git grep -E --color=auto'
alias gPgrep='git grep -P --color=auto'
alias info='info --vi-keys'
alias less='less -i'
alias vim="${EDITOR}"
[[ -x "${HOME}/.cabal/bin/cabal" ]] && alias cabal="${HOME}/.cabal/bin/cabal"
alias scpnpw='scp -i ~/.ssh/id_rsa_nopw'
alias sshnpw='ssh -i ~/.ssh/id_rsa_nopw'
# convert czech subtitles to encoding cp1250
alias convert_subtitles='enconv -L czech -x cp1250'
if which hub >/dev/null 2>&1; then
    eval "$(hub alias -s bash)"
fi
if [[ "${TERM256COLORS}" = 1 ]]; then
    alias tmux='tmux -2'
fi

unset alarm
function alarm() {
    mplayer -noconsolecontrols -nolirc -quiet -vo none \
        /usr/share/sounds/freedesktop/stereo/complete.oga >/dev/null &
    notify-send 'Alarm!'
}

unset max
function max() { echo "$@" | tr ' ' '\n' | sort -n -r | head -n 1; }
unset min
function min() { echo "$@" | tr ' ' '\n' | sort -n    | head -n 1; }
alias gl="git log --graph --pretty=format:'%Cred%h%Creset %Cgreen%G?%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --max-count=`max 10 $((LINES/4))`"
alias gbl="git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname);%(committerdate);%(authorname)' | sed 's/refs\/heads\///g' | column -t -s ';'"
if [[ -z "$(type -t __git_complete ||:)" ]]; then
    for pth in \
        /run/current-system/sw/etc/bash_completion.d/git-completion.bash \
        /usr/share/bash-completion/completions/git;
    do
        if [[ -e "${pth}" ]]; then
            source "${pth}"
            break
        fi
    done
    unset pth
fi
alias ganx="git annex"
unset ganx
function  ganx-rm-remote() {
    local remote="$1"
    git annex dead "${remote}" && \
        git remote remove "${remote}" && \
        git annex forget --force --drop-dead
}
if [[ "$(type -t __git_complete)" == "function" ]]; then
    __git_complete gl _git_log
    __git_complete girS _git_rebase
    __git_complete ganx _git_annex
fi

unset gfoap
function gfoap() {
    # git fetch origin and push
    # Arguments will be passed to git push.
    local branch=`git rev-parse --abbrev-ref HEAD`
    if [[ -z "${branch}" ]]; then
        printf "failed to get branch name" >&2
        return 1
    fi
    git fetch origin || return 1
    git merge "origin/${branch}" || return 1
    git push "$@"
}

unset girS
function girS() {
    if [[ "$#" == 0 || ( "$#" == 1 && "$(git log --format=oneline "$1"...HEAD | wc -l)" == 1 ) ]];
    then
        git commit --amend -S --no-edit
    else
        git rebase -i --exec 'git commit --amend -S --no-edit' $@
    fi
}

unset gitconfigprivate
function gitconfigprivate() {
    local key="${GNUPG_PRIVATE_SIGNING_KEY:-B834C2F93A782BD2390FC1B2DAFF346DC3ABDF76}"
    git config user.name        'Michal Minář'
    git config user.email       'mic.liamg@gmail.com'
    git config user.signingkey  "${key}"
    git config github.token     'michojel'
}

unset gitconfigwork
function gitconfigwork() {
    local key="${GNUPG_WORK_SIGNING_KEY:-992EC0D60D52C7231D0C241DED8FF0CA0500B770}"
    git config user.name        'Michal Minář'
    git config user.email       'miminar@redhat.com'
    git config user.signingkey  "${key}"
    git config github.token     'miminar'
    git config github.user      '4a00719f7fb01931f3a9729972dae10e'
}

unset currency_convert
function currency_convert() {
    if [[ $# != 3 ]]; then
        printf "currency_convert <amount> <source-currency> <target-currency>\n" >&2
        return 1
    fi
    curl -s "http://www.google.com/finance/converter?a=$1&from=$2&to=$3" | sed '/res/!d;s/<[^>]*>//g';
}

unset nohist
function nohist() {
    bash --rcfile <(cat ~/.bashrc; \
        echo "export HISTFILE=/dev/null; [[ -e ~/.bash_history ]] && history -r ~/.bash_history")
}

# Make all text output to stderr red
# Usage:
#   errcolor command [<args>]...
unset errcolor
function errcolor() {
    (set -o pipefail;"$@" 2>&1>&3|sed $'s,.*,\e[31m&\e[m,'>&2)3>&1
}

unset unrpm
function unrpm() {
    rpm2cpio "$1" | cpio -idmv
}

unset gitroot
function gitroot() {
    local dir="$(git rev-parse --show-toplevel)"
    local usage="Usage: gitroot [-h | -p | -i]\n
Options:\n  -h  print help\n  -p  push directory
  -i  just print the git root directory\n"
    if [[ $# = 0 ]]; then
        cd "${dir}"
    elif [[ $1 = -p ]]; then
        shift
        pushd "${dir}" "$@"
    elif [[ $1 = -h ]]; then
        printf "${usage}"
    elif [[ $1 = -i ]]; then
        echo "${dir}"
    elif [[ $1 =~ ^- ]]; then
        printf "${usage}" >2
        return 1
    fi

    cd "${dir}/$1"
}

unset rxvtinfo
function rxvtinfo() {
    infocmp rxvt-256color | ssh $@ "mkdir -p .terminfo && cat >/tmp/ti && tic /tmp/ti"
    infocmp rxvt-unicode-256color | ssh $@ "mkdir -p .terminfo && cat >/tmp/ti && tic /tmp/ti"
}

unset swap
function swap() {
    # swap two file name's
    # http://stackoverflow.com/questions/1115904/shortest-way-to-swap-two-files-in-bash
    local tmpfile="$(mktemp $(dirname "$1")/XXXXXX)"
    mv "$1" "${tmpfile}"
    mv "$2" "$1"
    mv "${tmpfile}" "$2"
}

unset abspath
function abspath {
    # convert relative path to absolute one
    # http://www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html#post4309783
    if [[ -d "$1" ]]
    then
        pushd "$1" >/dev/null
        pwd
        popd >/dev/null
    elif [[ -e $1 ]]
    then
        pushd "$(dirname $1)" >/dev/null
        echo "$(pwd)/$(basename $1)"
        popd >/dev/null
    else
        echo "$1 does not exist!" >&2
        return 127
    fi
}

unset unmonpkgs
function unmonpkgs {
  local username="${USER}"
  local f="$(mktemp)"
  local i
  curl -s "https://fedoraproject.org/wiki/Upstream_release_monitoring" | grep '^ * ' | awk '{print $2}' > "${f}"
  for i in $(pkgdb-cli list --user "${username}" | head -n -1 | awk "{print \$1}"); do
    grep "^${i}\$" "${f}" > /dev/null
    if [ "$?" != "0" ]; then
        echo -e "\033[1;31m${i}\033[0m"
        while read line; do
          [ "${line}" == "" ] && continue
          [ "${line}" == " " ] && continue
          (echo "${line}" | grep '\*' > /dev/null) || continue
          (echo "${i}" | grep "^${line}" > /dev/null) &&
            echo -e " ...\033[1;33m   Possible match (\033[1;31m${i}\033[0m): ${line}\033[0m"
        done < "${f}"
    fi
  done
  rm "${f}"
}

unset gocd
function gocd {
    cd `go list -f '{{.Dir}}' "$1"`
}

unset ansh
function ansh() {
    local args="" a
    local index=0
    for a in "$@"; do
        [[ "$index" -gt 0 ]] && args+=" "
        args+="$(printf '%s\n' "${a:-}" | sed -e 's/"/\\"/g' -e 's/.*\s\+.*/"\0"/')"
        index="$(($index+1))"
    done
    set -x
    ansible all -i hosts -m shell -b -a "${args:-}"
    set +x
}

unset ssh_rsa_encrypt
function ssh_rsa_encrypt() {
    local OPTIND opt OPTARG outfile="" infile
    local pubkey="~/.ssh/id_rsa.pub.pkcs8"
    while getopts ':hi:o:' opt; do
        case "$opt" in
        h) cat <<EOF
Usage: ssh_rsa_encrypt [options] [INPUT_FILE...]

Encrypt the given files using an ssh public key. Only small files can be encrypted this way. If no
file is given, the standard input will be encrypted instead.

Make sure to provide a public key in pkcs8 format. To make one from an RSA key, use:

    fn="\${fn:-~/.ssh/id_rsa.pub}"
    ssh-keygen -e -f "\${fn}" -m PKCS8 > \${fn}.pkcs8

Options:
  -h   Show this help and quit.
  -i   Path to the public key in pkcs8 format (defaults to ~/.ssh/id_rsa.pub.pkcs8).
  -o   Output file. If there are more input files, they will be concatenated.
       The default is to store the encrypted version in \$INPUT_FILE.enc
EOF
            return 0
            ;;
        i) pubkey="${OPTARG}";  ;;
        o) outfile="${OPTARG}"; ;;
        *) echo "Unrecognized option '${opt}'. See help!" >&2; return 1; ;;
        esac
    done
    if [[ ! "${pubkey}" =~ \.pkcs8$ && "$(file "${pubkey}")" =~ private ]]; then
        for suf in .pub.pkcs8 .pkcs8; do
            if [[ -e "${pubkey}$suf" ]]; then
                pubkey="${pubkey}$suf"
                break
            fi
        done
    fi
    shift "$((OPTIND - 1))"
    [[ -n "${outfile:-}" && "${outfile}" != '-' ]] && truncate --size 0 "${outfile}"
    # output the encrypted contents to the standard output
    [[ "${outfile:-}" == '-' ]] && outfile="/dev/fd/1"
    # read the contents from the standard input if no file is given
    [[ $# == 0 ]] && set -- "/dev/fd/0"
    for infile in "$@"; do
        local o="${outfile:-${infile}.enc}"
        [[ -z "${outfile}" && "${infile}" == "/dev/fd/0" ]] && o="/dev/fd/1"
        [[ -z "${outfile:-}" && "${o}" != "/dev/fd/1" ]] && truncate --size 0 "${o}"
        if ! openssl rsautl -encrypt -pubin -inkey "${pubkey}" -in "${infile}" -out "${o}"; then
            local rc=$?
            [[ "$o" != "/dev/fd/1" ]] && rm "${o}"
            return "$rc"
        fi
    done
}

unset ssh_rsa_decrypt
function ssh_rsa_decrypt() {
    local OPTIND opt OPTARG outfile="" infile
    local privatekey="~/.ssh/id_rsa"
    while getopts ':hi:o:' opt; do
        case "$opt" in
        h) cat <<EOF
Usage: ssh_rsa_decrypt [options] [INPUT_FILE...]

Decrypt the given files using a private ssh key. Their contents must be a result of encryption by
the corresponding public key. If no file is given, standard input is read instead.

Options:
  -h   Show this help and quit.
  -i   Path to the private key (defaults to ~/.ssh/id_rsa).
  -o   Output file. If there are more input files, they will be concatenated.
       The default is to output the contents to the standard output.
EOF
            return 0
            ;;
        i) privatekey="${OPTARG}";  ;;
        o) outfile="${OPTARG}";     ;;
        *) echo "Unrecognized option '${opt}'. See help!" >&2; return 1; ;;
        esac
    done
    shift "$((OPTIND - 1))"
    [[ -n "${outfile:-}" && "${outfile}" != '-' ]] && truncate --size 0 "${outfile}"
    # output the decrypted contents to the standard output
    [[ "${outfile:--}" == '-' ]] && outfile="/dev/fd/1"
    # read ecrypted contents from the standard input
    [[ "$#" == 0 ]] && set -- "/dev/fd/0"
    for infile in "$@"; do
        openssl rsautl -decrypt -ssl -inkey "${privatekey}" -in "${infile}" -out "${outfile}"
    done
}

# ex:ts=4:et:sw=4:ft=sh