我的.bashrc
对于linux, 相信每个朋友都会有自己的使用习惯. 也会总结出不少的 alias 等等.
下面是我的.bashrc, 希望对对朋友们有借鉴作用.
如果有不明白的, 欢迎随时交流. 😃
export PS2="-> "
export PS4='+{$LINENO:${FUNCNAME[0]}}'
set -o vi
# ignore case
bind "set completion-ignore-case on"
# do NOT limit the size of core file
ulimit -c unlimited
export PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"'
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
shopt -s autocd # auto "cd" when entering just a path
# ls ## {{{
alias ls='\ls -F --color=auto'
alias LS='ls'
alias sl='ls'
alias s='ls'
alias l='ls -lh'
alias la='ls -A'
alias ll='l -A'
alias l.='la -d .*'
alias ll.='ll -d .*'
alias lr='ls -R' # recursive ls
alias lm='la | less'
alias lld='__Lld'
alias lf='__Lf'
alias llta='ll -rt' # sort by date ascent
alias llxa='ll -BX' # sort by extension ascent
alias llza='ll -rS' # sort by size ascent
alias lltd='ll -t' # sort by date descent
alias llxd='ll -rBX' # sort by extension descent
alias llzd='ll -S' # sort by size descent
alias llt='llta' # sort by date (ascent)
alias llx='llxa' # sort by extension (ascent)
alias llz='llza' # sort by size (ascent)
alias lta='l -rt' # sort by date ascent
alias lxa='l -BX' # sort by extension ascent
alias lza='l -rS' # sort by size ascent
alias ltd='l -t' # sort by date descent
alias lxd='l -rBX' # sort by extension descent
alias lzd='l -S' # sort by size descent
alias lt='lta' # sort by date (ascent)
alias lx='lxa' # sort by extension (ascent)
alias lz='lza' # sort by size (ascent)
# }}}
## cd ## {{{
alias cd.=''
alias cd..='cd ..'
alias ..='cd ..'
alias ..1='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
alias cd1='..'
alias cd2='..2'
alias cd3='..3'
alias cd4='..4'
alias cd5='..5'
alias cd-='cd -'
alias cn0='__NthPath 0'
alias cn1='__NthPath 1'
alias cn2='__NthPath 2'
alias cn3='__NthPath 3'
alias cn4='__NthPath 4'
alias cn5='__NthPath 5'
alias cn6='__NthPath 6'
alias cn7='__NthPath 7'
alias cn8='__NthPath 8'
alias cn9='__NthPath 9'
alias cn='__NthPath '
# }}}
alias sudo='sudo '
## Modified commands ## {{{
alias diff='colordiff'
alias more='less'
alias df='df -h'
alias du='du -c -h'
alias du1='du --max-depth=1'
alias du.='du -s .'
alias mkdir='mkdir -p'
alias nano='nano -w'
alias ping='ping -c 3'
alias dmesg='dmesg -HL'
alias date='date "+%A, %B %d, %Y [%T]"'
alias hist='history | grep' # requires an argument
alias openports='ss --all --numeric --processes --ipv4 --ipv6'
alias pgp='ps -aux | grep ' # requires an argument
alias clr='clear'
alias vi='vim'
alias g++='g++ -std=c++14 '
alias gg='g++ -std=c++14 -g '
alias cl='clang++ -std=c++14 '
alias clg='clang++ -std=c++14 -g '
#}}}
## Privileged access ## {{{
if [ $UID -ne 0 ]; then
alias scat='sudo cat'
alias svi='sudo vi'
alias root='sudo -s'
alias sreboot='sudo systemctl reboot'
alias spoweroff='sudo systemctl poweroff'
alias snetctl='sudo netctl'
fi
#}}}
## pacman alias ## {{{
alias pacupg='sudo pacman -Syu' # Synchronize with repositories and then upgrade packages that are out of date on the local system.
alias pacin='sudo pacman -S' # Install specific package(s) from the repositories
alias pacins='sudo pacman -U' # Install specific package not from the repositories but from a file
alias pacre='sudo pacman -R' # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns' # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacout='pacrem'
alias pacrep='pacman -Si' # Display information about a given package in the repositories
alias pacreps='pacman -Ss' # Search for package(s) in the repositories
alias pacloc='pacman -Qi' # Display information about a given package in the local database
alias paclocs='pacman -Qs' # Search for package(s) in the local database
alias paclo="pacman -Qdt" # List all packages which are orphaned
alias pacc="sudo pacman -Scc" # Clean cache - delete all not currently installed package files
alias paclf="pacman -Ql" # List all files installed by a given package
alias pacexpl="pacman -D --asexp" # Mark one or more installed packages as explicitly installed
alias pacimpl="pacman -D --asdep" # Mark one or more installed packages as non explicitly installed
# '[r]emove [o]rphans' - recursively remove ALL orphaned packages
alias pacro="pacman -Qtdq > /dev/null && sudo pacman -Rns \$(pacman -Qtdq | sed -e ':a;N;$!ba;s/\n/ /g')"
# Additional pacman alias examples
alias pacupd='sudo pacman -Sy && sudo abs' # Update and refresh the local package and ABS databases against repositories
alias pacinsd='sudo pacman -S --asdeps' # Install given package(s) as dependencies
alias pacmir='sudo pacman -Syy' # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist
alias pacunlock="sudo rm /var/lib/pacman/db.lck" # Delete the lock file /var/lib/pacman/db.lck
alias paclock="sudo touch /var/lib/pacman/db.lck" # Create the lock file /var/lib/pacman/db.lck
#}}}
## for myself ## {{{
#WM='pantheon-files'
#WM='spacefm'
WM='thunar'
#WM=nautilus
#WM=pcmanfm
alias o='${WM} '
alias o.='${WM} "${PWD}"'
alias so='sudo ${WM} '
alias so.='so "${PWD}"'
DL=${HOME}/Downloads
DT=${HOME}/Desktop
DM=${HOME}/Documents
alias cddl='cd "${DL}"'
alias cddt='cd "${DT}"'
alias cddm='cd "${DM}"'
alias odl='o "${DL}"'
alias odt='o "${DT}"'
alias odm='o "${DM}"'
#}}}
# arch offline wiki
export wiki_browser=firefox
alias ws='wiki-search '
alias wsh='wiki-search-html '
if [ -f ~/.cmd ]
then
. ~/.cmd
__PS1
alias grep="$(__Colorized grep)"
alias egrep="$(__Colorized egrep)"
alias fgrep="$(__Colorized fgrep)"
fi
以 __ 开头的是自定义的函数, 在另一个文件(.cmd) 中
这个文件中, 名称为小写的是 直接可以在命令行中调用的, 比如 proxy, 可以直接设置代理
而以__开头的大写函数, 则是为了让其它指令或者别名调用, 不推荐在shell中直接调用(这也是取这么难写名字的原因)
function __PS1()
{
local normalColor="\[\e[38;5;22m\]"
local urgentColor="\[\e[0;31m\]"
local pathColor="\[\e[1;34m\]"
local endColor="\[\e[m\]"
local tip='\$ '
local good="${normalColor}${tip}${endColor}"
local bad="${urgentColor}${tip}${endColor}"
local path=${pathColor}'\w '${endColor}
local colorizedTip="\$(if [[ \$? == 0 ]]; then echo \"${good}\"; else echo \"${bad}\"; fi)"
export PS1=${path}${colorizedTip}
}
function __Colorized()
{
[ -n "$1" ] && echo "$1 --color=auto"
}
function mkcd()
{
mkdir -p "$@" && eval cd "\"\$$#\"";
}
function __NthPath()
{
[ $1 -eq 0 ] && cd / && return 0
cd "$(echo "${PWD}" | cut -d / -f -$(($1 + 1)))"
}
function cdls()
{
local dir=$1
if [[ -z "${dir}" ]]
then
dir=${HOME}
fi
if [[ -d "${dir}" ]]
then
cd "${dir}"
ls
else
echo "bash: cdls: '${dir}': Directory not found"
fi
}
function __CreateNotesIfNotExist()
{
if [[ $# -eq 1 && ! -f $1 ]]
then
touch $1
fi
}
function note()
{
file="${HOME}/.${FUNCNAME}"
# if file doesn't exist, create it
__CreateNotesIfNotExist ${file}
if ! (($#))
then
# no arguments, print file
cat "${file}"
elif [[ "$1" == "-c" ]]
then
# clear file
> "${file}"
else
# add all arguments to file
printf "%s\n" "$*" >> "${file}"
fi
}
function todo()
{
file="${HOME}/.${FUNCNAME}"
# if file doesn't exist, create it
__CreateNotesIfNotExist ${file}
if ! (($#));
then
cat "${file}"
elif [[ "$1" == "-l" ]]
then
nl -b a "${file}"
elif [[ "$1" == "-c" ]]
then
> ${file}
elif [[ "$1" == "-r" ]]
then
nl -b a "${file}"
printf "----------------------------\n"
read -p "Type a number to remove: " number
ex -sc "${number}d" "${file}"
else
printf "%s\n" "$*" >> "${file}"
fi
}
function calc()
{
echo "scale=3;$@" | bc -l
}
function __Proxy()
{
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
local proxy=$1
if [ $# -eq 0 ]
then
proxy="3.36.125.228:9400"
fi
export http_proxy=${proxy}
export https_proxy=${http_proxy}
export ftp_proxy=${http_proxy}
export rsync_proxy=${http_proxy}
echo "proxy environment variable set."
}
function proxy()
{
local proxy=" " ##### default proxy
if [ $# -eq 1 ]
then
proxy=${1}
fi
__Proxy "${proxy}"
}
function proxyoff()
{
unset http_proxy
unset https_proxy
unset ftp_proxy
unset rsync_proxy
echo -e "proxy environment variable removed."
}
function __Lf()
{
if [[ $# -eq 0 ]]
then
l | grep ^[^d] | awk '{print $NF}'
else
l -d "$@"
fi
}
function __Lld()
{
if [[ $# -eq 0 ]]
then
l | grep ^[d] | awk '{print $NF}'
else
l -d "$@"
fi
}