类UNIX平台上使用人数较多的Shell主要有Bash,Fish和Zsh,Bash是大多数系统默认的Shell,Fish属于开箱即用型,功能也不错,但跟Bash兼容性不是特别好,Zsh功能最为强大,但需要折腾一下。虽然默认情况下,Bash在功能和界面上都不如Fish和Zsh,但通过适当配置~/.bashrc
也能让它焕然一新,下面是我的配置,它实现了快速备份文件,以程序名杀死进程,彩色manpage和类似于Zsh的x解压缩等功能
# Alias
alias vi='vim --noplugin'
alias mkdir='mkdir -p'
alias rm='rm --interactive --verbose'
alias cp='cp --interactive --verbose'
alias mv='mv --interactive --verbose'
alias tree='tree -Csuh'
alias diff='colordiff'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto --ignore-case'
alias egrep='egrep --color=auto --ignore-case'
alias cman='man -M /usr/share/man/zh_CN'
# List only the directories
alias lsd='ls -d */'
# Run the last command, and copy the output to clipboard
alias cl='bash -c "$(fc -ln -1)" | pbcopy'
# Display the top ten running processes - sorted by memory usage
alias memory='ps aux | sort -nk +4 | tail'
# Get the 10 biggest files/folders for the current direcotry
alias big='du -s * | sort -n | tail'
alias top10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
# Function
# One command to extract compressed files
alias x='extract'
extract() {
local opt
local OPTIND=1
while getopts "hv" opt; do
case "$opt" in
h)
cat <<End-Of-Usage
Usage: ${FUNCNAME[0]} [option] <archives>
options:
-h show this message and exit
-v verbosely list files processed
End-Of-Usage
return
;;
v)
local -r verbose='v'
;;
?)
extract -h >&2
return 1
;;
esac
done
shift $((OPTIND-1))
[ $# -eq 0 ] && extract -h && return 1
while [ $# -gt 0 ]; do
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2|*.tbz|*.tbz2) tar "x${verbose}jf" "$1" ;;
*.tar.gz|*.tgz) tar "x${verbose}zf" "$1" ;;
*.tar.xz) xz --decompress "$1"; set -- "$@" "${1:0:-3}" ;;
*.tar.Z) uncompress "$1"; set -- "$@" "${1:0:-2}" ;;
*.bz2) bunzip2 "$1" ;;
*.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;;
*.pax.gz) gunzip "$1"; set -- "$@" "${1:0:-3}" ;;
*.gz) gunzip "$1" ;;
*.pax) pax -r -f "$1" ;;
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;;
*.rar) unrar x "$1" ;;
*.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;;
*.tar) tar "x${verbose}f" "$1" ;;
*.txz) mv "$1" "${1:0:-4}.tar.xz"; set -- "$@" "${1:0:-4}.tar.xz" ;;
*.xz) xz --decompress "$1" ;;
*.zip|*.war|*.jar) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7za x "$1" ;;
*) echo "'$1' cannot be extracted via extract" >&2;;
esac
else
echo "extract: '$1' is not a valid file" >&2
fi
shift
done
}
# Create a directory and enter it
cde () {
mkdir -p $1
cd $1
}
# Kill processes by name
kp () {
ps aux | grep $1 > /dev/null
mypid=$(pidof $1)
if [ "$mypid" != "" ]; then
kill -9 $(pidof $1)
if [[ "$?" == "0" ]]; then
echo "PID $mypid ($1) has been killed."
fi
else
echo "Nothing has been killed."
fi
return;
}
# back up files
bak(){
cp $1{,.bak}
}
# Misc
# Make Vim the default editor
EDITOR=vim
export EDITOR
export GIT_EDITOR=vim
# Colorful manpages
export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode – red
export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode – bold, magenta
export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us)
export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode
export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode – yellow
export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode
export LESS_TERMCAP_us=$(printf '\e[04;36m') # enter underline mode – cyan
写入配置以后,需要重启终端或者执行source ~/.bashrc
才能生效。另外,如果你需要你类似于oh-my-zsh那样方便强大的Bash配置的话,推荐安装bash-it,它本身也是参考oh-my-zsh
写的。