aboutsummaryrefslogtreecommitdiff
path: root/misc/.bashrc
blob: e964fbd8a0296684d3bf5f88a072cea8637b33ff (plain) (blame)
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
# If not running interactively, don't do anything.  This too is taken
# from Debian 9's bashrc.
case $- in
  *i*) ;;
  *) return;;
esac

# use emacs for editor
export EDITOR="emacsclient -c -a emacs"         # $EDITOR opens in terminal
export VISUAL="emacsclient -c -a emacs"         # $VISUAL opens in GUI mode

export LC_ALL="en_AU.utf8"

# Shorter version of a common command that it used herein.
_checkexec ()
{
    command -v "$1" > /dev/null
}

# Machine-specific and personal settings
if [[ -f ~/.bashrc_local ]]; then
  source ~/.bashrc_local
fi

## prompts
# git prompt
if [[ -f /usr/share/git/completion/git-prompt.sh ]]; then
  source /usr/share/git/completion/git-prompt.sh
elif [[ -f /etc/bash_completion.d/git-prompt ]]; then
  source /etc/bash_completion.d/git-prompt
elif [[ -f /usr/share/git-core/contrib/completion/git-prompt.sh ]]; then
  source /usr/share/git-core/contrib/completion/git-prompt.sh
fi

# prompt
RESET="\[\033[0m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\033[0;33m\]"
if [ -n "$SSH_CONNECTION" ]; then
  PS_INFO="$GREEN\u@\h$RESET:$BLUE\w"
else
  PS_INFO="$RESET$BLUE\w"
fi
PS_GIT="$YELLOW\$(__git_ps1)"
PS_TIME="\[\033[\$((COLUMNS-10))G\] $RED[\t]"
PS1="\${PS_FILL}\[\033[0G\]${PS_INFO}${PS_GIT}${PS_TIME}\n${RESET}\$ "
export PS1

# Use bash-completion, if available
[[ $PS1 && -f /usr/share/bash-completion/bash_completion ]] && \
  . /usr/share/bash-completion/bash_completion

# Enable tab completion when starting a command with 'sudo'
[ "$PS1" ] && complete -cf sudo

# Include my scripts in the PATH.
if [ -d "$HOME"/bin ]
then
  PATH=$PATH:"$HOME"/bin
fi

if [ -d "$HOME"/.local/bin ]
then
  PATH=$PATH:"$HOME"/.local/bin
fi

# from ~/.bashrc_local, adding more to PATH
if [[ $(type -t my_local_add_path) == function ]]; then
  my_local_add_path
fi

export PATH

# Default pager.  Note that the option I pass to it will quit once you
# try to scroll past the end of the file.
if [ "$TERM" = "dumb" ]
then
  export PAGER="cat"
  alias less="cat"
else
  export PAGER="less --quit-at-eof"
fi

export MANPAGER="$PAGER"

# Enter directory and list contents
cd ()
{
    if [ -n "$1" ]
    then
        builtin cd "$@" && ls -pvA --color=auto --group-directories-first
    else
        builtin cd ~ && ls -pvA --color=auto --group-directories-first
    fi
}

# Back up a file. Usage "backupthis <filename>"
backupthis ()
{
    cp -riv $1 ${1}-$(date +%Y%m%d%H%M).backup;
}

# append to history file. don't overwrite it.
shopt -s histappend
# increase history size to infinity
export HISTSIZE=-1
export HISTFILESIZE=-1
# ignore ls and history in history
export HISTIGNORE="ls:history"
export HISTTIMEFORMAT="[%F %T] "
export HISTCONTROL=ignoreboth

# aliases
test -s ~/.alias && . ~/.alias || true
alias shutdown='sudo /sbin/shutdown -hP now'
alias reboot='sudo reboot'
alias wifi-menu='sudo /usr/bin/wifi-menu'
alias cd-='cd -'
alias cd#='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ls='ls --color=always'
alias l.='ls -d .* --color=auto'
alias ll='ls -l'
alias updatedb='sudo updatedb'
# wget it like it's curl
alias wgets="wget -O- 2>/dev/null"
# get url behind one redirection
alias wgre="wget --spider --max-redirect=0"
# wget with spoofing googlebot
alias wggb='wget --header="X-Forwarded-For: 66.249.66.1" --user-agent="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"'
# this allows to open an url with tor-browser from within emacs, see
# `my-browse-url-tor-browser'
alias tor-browser="tor-browser --allow-remote"
alias which='alias | which -i'
alias chx="chmod +x"
# dedicated emacs instances for emms and erc, see `my-profile' in my
# emacs init files
alias emms='EMACS_PROFILE=emms emacs'
alias erc='EMACS_PROFILE=erc emacs'

# Safer default for cp, mv, rm.  These will print a verbose output of
# the operations.  If an existing file is affected, they will ask for
# confirmation.  This can make things a bit more cumbersome, but is a
# generally safer option.
alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -iv'

# git
alias gcgrep='git log --pretty=format:"%h %ad - %an: %<(80,trunc)%s" | grep'

# pacman package manager
if _checkexec pacman
then
  # General package management
  alias pSyu="sudo pacman -Syu"   # system upgrade
  alias pSyyu="sudo pacman -Syyu" # when updating mirrors
  alias pD="sudo pacman -D"       # set `--asdeps` or `--asexplicit`

  # Search remote database and download packages
  alias pSs="pacman -Ss"      # search remote for package
  alias pS="sudo pacman -S"   # sync download
  alias pU="sudo pacman -U"   # install local user package

  # Query the file database
  alias pF="pacman -F"
  pFw()
  {
    pacman -F $(which "$1")
  }

  # Search local database
  alias pQs="pacman -Qs"      # query list
  alias pQmq="pacman -Qmq"    # list foreign packages
  alias pQdt="pacman -Qdt"    # list orphans
  alias pQe="pacman -Qe"      # explicitly installed packages

  # Inspect packages (remote and local)
  alias pSi="pacman -Si"      # remote package details
  alias pQi="pacman -Qi"      # local package details
  alias pQl="pacman -Ql"      # local package file lists
  alias pQo="pacman -Qo"      # which package owns this file
  alias pQc="pacman -Qc"      # show changelog

  # Remove packages
  alias pRs="sudo pacman -Rs"     # remove package
  alias pRnsc="sudo pacman -Rnsc" # remove package recursively
  # similar to apt autoremove
  alias pRat="sudo pacman -Qdtq | sudo pacman -Rs -"

  # Clear cache
  alias pcache1="sudo paccache -rk 1" # remove cache except last item
  alias pcache0="sudo paccache -ruk0" # remove all cache

  # make an aur package
  paur () {
    pkg_name="${1%.tar.gz}"
    tar xvf "$pkg_name.tar.gz"
    cd "$pkg_name"
    makepkg
  }
fi

alias systart="sudo systemctl start"
alias systop="sudo systemctl stop"
alias sysnow="sudo systemctl enable --now"
alias sysdis="sudo systemctl disable"
alias systat="systemctl status"
alias sysres="sudo systemctl restart"
alias sysrelo="sudo systemctl reload"

# from ~/.bashrc_local, more aliases
if [[ $(type -t my_local_aliases) == function ]]; then
  my_local_aliases
fi

#ibus
export GTK_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export QT_IM_MODULE=ibus

#fix small vlc interface
export QT_AUTO_SCREEN_SCALE_FACTOR=0

# ensure $XDG_RUNTIME_DIR is set
if [ -z "$XDG_RUNTIME_DIR" ]; then  # It's not already set
  XDG_RUNTIME_DIR=/run/user/$UID  # Try systemd created path
  if [ ! -d "$XDG_RUNTIME_DIR" ]; then
    # systemd-created directory doesn't exist
    XDG_RUNTIME_DIR=/tmp/$USER-runtime
    if [ ! -d "$XDG_RUNTIME_DIR" ]; then  # Doesn't already exist
      mkdir -m 0700 "$XDG_RUNTIME_DIR"
    fi
  fi
fi
# Check dir has got the correct type, ownership, and permissions
if ! [[ -d "$XDG_RUNTIME_DIR" && -O "$XDG_RUNTIME_DIR" &&
    "$(stat -c '%a' "$XDG_RUNTIME_DIR")" = 700 ]]; then
  echo "\$XDG_RUNTIME_DIR: permissions problem with $XDG_RUNTIME_DIR:" >&2
  ls -ld "$XDG_RUNTIME_DIR" >&2
  XDG_RUNTIME_DIR=$(mktemp -d /tmp/"$USER"-runtime-XXXXXX)
  echo "Set \$XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" >&2
fi

# start the agent automatically and make sure that only one
# ssh-agent process runs at a time
if ! pgrep -u "$USER" ssh-agent > /dev/null; then
  ssh-agent -t 600 > "$XDG_RUNTIME_DIR/ssh-agent.env"
fi
if [[ ! "$SSH_AUTH_SOCK" ]]; then
  source "$XDG_RUNTIME_DIR/ssh-agent.env" >/dev/null
fi

# calibre
export CALIBRE_TEMP_DIR=$HOME/tmp

# ccache
export USE_CCACHE=1
export CCACHE_EXEC=/usr/bin/ccache

# cvs
export CVS_RSH="ssh"

# from ~/.bashrc_local, export more stuff
if [[ $(type -t my_local_export) == function ]]; then
  my_local_export
fi

# ghostscript, extract pages into a new file
# gs-extract 4 11 page-4-thru-11.pdf original.pdf
gs-extract() {
  gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=$1 -dLastPage=$2 \
     -sOutputFile=$3 $4
}

# ghostscript, merge files: gs-merge merged.pdf 1.pdf 2.pdf
gs-merge() {
  gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$*
}