# Aliases and similar functions which can be used by all shells (supporting
# them).

# Copyright (C) 2011-2015  Simon Ruderich
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.


# Shortcuts for often used programs.
alias c=clear
alias d=cd
alias e=elinks
alias g=git
alias h=htop
alias l=ls
alias p=less # p for pager
alias s=ssh
alias t=tig
alias v=vim
alias x=exit
# Shortcuts for a little less used programs.
alias ga='git annex'
alias gr=grep
alias gri='grep -i'
alias grr='grep -r'
alias grri='grep -ri'
alias mc='make clean'
alias mj='make -j$(nproc)'
alias mu=mutt
alias rs=reset # like git's reset alias
alias sa=ssh-add
alias ta=task
alias te=tree
alias ti=timew

# systemd ... (sc is provided as shell script for better completion); redirect
# stderr because my color-stderr hack breaks systemd's terminal detection.
jc() {
    journalctl "$@" 2>&1
}
lc() {
    loginctl "$@" 2>&1
}


unalias mv cp 2>/dev/null
# Ask for confirmation before overwriting files. Especially useful when moving
# to a different directory. No alias for `rm` because I specify the files to
# remove directly, so I know what will happen.
alias mv='mv -i'
# Additionally preserve all file attributes when copying, this includes
# copying symbolic links as is without dereferencing them.
alias cp='cp -i -a'


# Make sure there is no alias named ls as it causes problems with the
# following ls function on (at least) bash 4.0.35.
unalias ls 2>/dev/null
# Improved ls which displays the files in columns (-C), visualizes
# directories, links and other special files (-F), uses human file sizes (-h)
# and pages everything through less.
#
# If available use GNU ls with colorized output. If it isn't available try
# normal ls which needs CLICOLOR_FORCE so it displays colors when used with a
# pager. If none work no colors are used.
#
# See `setup.sh` for details. LS_* are replaced with the appropriate values
# when this file is generated.
ls() {
    LS_ARGS -C -F -h "$@" 2>&1 | less -S
}

unalias ll lt la lal lat 2>/dev/null
# List the files in list format with access rights, etc.
alias ll='ls -l'
# List the files sorted by last modification date.
alias lt='ls -l -t'
# List all files.
alias la='ls -a'
# List all files in list format with access rights, etc.
alias lal='ls -al'
# List all files sorted by last modification date.
alias lat='ls -al -t'


# Make going up directories simple.
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# Automatically use unified diffs.
alias diff='diff -u'

# COLUMN is set to `| column -t` if it's available, empty otherwise.

# Display all files (-s), use human readable sizes (-h) and display the
# complete size (-c).
alias du='du -shc'
# Use human readable sizes and format it nicely, thanks to climagic
# (http://twitter.com/climagic/status/49623386762129408).
df() {
    command df -hP "$@" COLUMN
}

# Highlight matched strings. Doesn't work with a pager!
alias grep='grep --color=auto'

# Pipe output through less.
tree() {
    command tree -C "$@" | less -S
}

# Groff versions >= 1.23 don't support LESS_TERMCAP_* to color man pages, set
# GROFF_NO_SGR as workaround. Also set LANG to C so that hyphens are not
# replaced with unicode making it impossible to search for options containg
# "-" or "--" in many man pages (these are errors in the man page but very
# common; Debian reverted this behavior in 1.23.0-3 but other distributions
# might not).
man() {
    GROFF_NO_SGR=1 LANG=C command man "$@"
}

# Better viewer for info pages .. just pipe everything into less.
info() {
    command info "$@" 2>/dev/null | less
}

# Using su (or sudo) as root to a less privileged user might allow the other
# user to run arbitrary commands as root. See also Debian bugs #628843 and
# #657784.
if test "`id -u`" -eq 0; then
    su() {
        echo 'never su as root' >&2
        return 1
    }
fi

# Use short `ip` output per default.
alias ip='ip -brief -color=auto'

# Calendar with traditional output (-b), weeks starting on Monday (-M), week
# numbers (-w) and the previous, current and next month (-3)
alias cal='ncal -b -M -w -3'

# Group volumes on mdadm devices instead of displaying them multiple times
alias lsblk='lsblk --merge'

# vim: ft=sh
