# texray's ~/.bashrc # Author: Andreas Textor # Last change: 15.02.2010 #--------------------------------------------------------------------- # Aliases #--------------------------------------------------------------------- alias ls='COLUMNS=$COLUMNS ls -F --color=auto' alias ll='ls -l' alias l='ls -l' alias la='ls -lA' # show hidden alias lh='ls -lh' # human readable (size in MB etc.) alias lsd='du -D *' alias lsi='ls -lSr' # ls and sort by filesize alias lasi='ls -lASr' # show hidden and sort by filesize alias lx='ls -lXB' # sort by extension alias lk='ls -lSr' # sort by size, biggest last alias lc='ls -ltcr' # sort by and show change time, most recent last alias lu='ls -ltur' # sort by and show access time, most recent last alias lt='ls -ltr' # sort by date, most recent last if $(which most &>/dev/null); then alias lo='most' else alias lo='less' fi alias ..='cd ..' alias cd..='cd ..' #alias cp='cp -i' alias cp='progress_cp' # function, see below alias gi='grep -i' alias startx='exec startx -- -nolisten tcp' alias :q='exit' alias wget='wget -c --tries=235' alias vi='vim' alias filecount="ls -1|wc -l" alias df='better_df' # function, see below alias du='du -sh' alias sra="screen -d -R pts-1.`hostname`" # always (re-)attach "the" screen session alias ip='curl http://showip.spamt.net/' alias isb='rlwrap scala -deprecation -unchecked' alias rn='rename.pl' alias scpresume='rsync --partial --progress -rsh=ssh' alias dos2unix='perl -pi -e "s/\r//g"' alias osx2unix='perl -pi -e "s/\r/\n/g"' alias unix2dos='perl -pi -e "s/\n/\r\n/g"' #alias xtv='xinit `which bigterm` -- :1 -xf86config xorg.conf.tv' alias grep='egrep --color=always' alias fixresolution="xrandr -s 1920x1080" #--------------------------------------------------------------------- # Bash completion #--------------------------------------------------------------------- if [ -r /etc/bash_completion ]; then . /etc/bash_completion else # No bash completion found? Then just complete directories complete -d cd fi #--------------------------------------------------------------------- # Environment #--------------------------------------------------------------------- # ANSI Colors export C_NONE="\[\033[0m\]" export C_BLACK="\[\033[0;30m\]" export C_DARKGRAY="\[\033[1;30m\]" export C_RED="\[\033[0;31m\]" export C_LIGHTRED="\[\033[1;31m\]" export C_GREEN="\[\033[0;32m\]" export C_LIGHTGREEN="\[\033[1;32m\]" export C_BROWN="\[\033[0;33m\]" export C_YELLOW="\[\033[1;33m\]" export C_BLUE="\[\033[0;34m\]" export C_LIGHTBLUE="\[\033[1;34m\]" export C_PURPLE="\[\033[0;35m\]" export C_LIGHTPURPLE="\[\033[1;35m\]" export C_CYAN="\[\033[0;36m\]" export C_LIGHTCYAN="\[\033[1;36m\]" export C_LIGHTGRAY="\[\033[0;37m\]" export C_WHITE="\[\033[1;37m\]" # File colors [ -r ~/.dir_colors ] && eval `dircolors -b ~/.dir_colors` # PATH (append; default PATH is specified in /etc/profile) [ -d /usr/local/bin ] && PATH="/usr/local/bin:$PATH" [ ! `echo $PATH|grep acoc` ] && PATH="/usr/local/acoc/bin:$PATH" [ -d $HOME/bin ] && PATH="$HOME/bin:$PATH" export PATH # Manpath export MANPATH=/usr/share/man:$MANPATH # Prompt if [ "`whoami`" = "root" ]; then export PS1=""$C_LIGHTRED"\u"$C_LIGHTBLUE"@"$C_WHITE"\h"$C_NONE"["$C_YELLOW"\w"$C_NONE"]$"$C_NONE" " else export PS1=""$C_WHITE"\u"$C_LIGHTBLUE"@"$C_WHITE"\h"$C_NONE"["$C_YELLOW"\w"$C_NONE"]$"$C_NONE" " fi export PS2=""$C_WHITE">"$C_NONE" " # Bash Settings export HISTFILE="$HOME/.bash_history" export HISTCONTROL=ignoredups export HISTFILESIZE=3000 export HISTTIMEFORMAT="%H:%M > " export HISTIGNORE="&:bg:fg:ll:h" shopt -u mailwarn unset MAILCHECK # Editor ('vim' is the common default) export EDITOR=/usr/bin/vim export CVSEDITOR=$EDITOR export VISUAL=$EDITOR export XEDITOR=/usr/bin/gvim export VIM=/usr/share/vim # Pager ('less' is the common default) export PAGER=/bin/less export LESS=-R export LESSCHARSET="utf-8" export MANPAGER=/usr/bin/most # Misc settings and fixes for various programs export ignoreeof=0 export ECLIPSE_HOME=/opt/eclipse export OOO_FORCE_DESKTOP=gnome export _JAVA_AWT_WM_NONREPARENTING=1 export GDK_NATIVE_WINDOWS=1 export RI="--format ansi -T" [ "$TERM" = "screen.rxvt" ] && export TERM="screen" # Misc convenience vars vars export FH="textor@login2.cs.hs-rm.de" #--------------------------------------------------------------------- # Miscellaneous #--------------------------------------------------------------------- umask 022 # default file permissions not to be set (octal) #mesg y # enable any messages users may send you with `write` #ulimit -u 200000 # max. user processes setterm -blength 0 # no beeps on console setterm -blank 0 # don't blank console set -o notify # notify when bg job done set -b # report status if bg job terminated #set bell-style visible # goes to inputrc! set nobeep stty -ixon # run function on logout trap _exit EXIT # '-s' sets option, '-u' unsets option, '-q' supresses output of option. shopt -s cdable_vars # Make 'cd $VARIABLE' possible shopt -s cdspell # Minor typo correction for directory arguments to 'cd' shopt -s checkwinsize # Check window size after each command, and update LINES and COLUMNS if necessary shopt -s cmdhist # Save multi-line cmds in same history entry shopt -u dotglob # Include dot files in the results of pathname expansion shopt -u execfail # Non-interactive shells won't exit if 'exec' fails shopt -s expand_aliases # Aliases are expanded (see bash(1), 'ALIASES') shopt -s extglob # Extended pattern matching features (see bash(1), 'Pathname Expansion') shopt -s histappend # Append to HISTFILE when shell exits (else: overwrite) shopt -s hostcomplete # Enable hostname completion with words containing '@' shopt -u huponexit # Send SIGHUP to all jobs when an interactive login shell exits shopt -s interactive_comments # Make '# comment' possible on command line shopt -u lithist # (cmdhist must be set) multi-line cmds are saved to history with embedded newlines (else: semicolon separators) shopt -u no_empty_cmd_completion # (readline) completion on empty line lists cmds in PATH shopt -u nocaseglob # Ignore case in pathname expansion shopt -s progcomp # Enable programmable completion shopt -s sourcepath # 'source'/'.' uses PATH to find directory containing the file supplied as an argument shopt -u xpg_echo # 'echo' expands backslash-escape sequences by default #--------------------------------------------------------------------- # Utility functions - other functions depend on them #--------------------------------------------------------------------- # Function to run upon exit of shell. function _exit() { clear echo -e "${C_NONE}Logged out at `date`" } # Asks a y/n question. Usage: if ask "something"; then ... ; fi function ask() { echo -n "$@" '[Y/n] ' ; read ans case "$ans" in n*|N*) return 1 ;; *) return 0 ;; esac } # Require can be used by other functions to make sure that certain commands exist # before a whole process of things is started (which potentially leaves temp files and such) function require() { which "$1" &>/dev/null if [ "x$?" = "x0" ]; then return 0; fi if [ "x$2" = "xquiet" ]; then return 1; fi if ask "Command not found: $1. Continue?"; then return 0; fi return 1 } #--------------------------------------------------------------------- # Functions for general use #--------------------------------------------------------------------- # Find file by pattern function ff() { find . -type f -iname '*'$*'*' -ls ; } # Rot13 :) function rot13() { cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' } # Lowercases all files in current directory function lcase { IFS=$'\n'; for i in `find $1 -maxdepth 1 -name '*[A-Z]*'`; do mv "$i" "`echo $i|tr [A-Z] [a-z]`"; done } # Process related functions # Taken from Emanual Rouat's sample bashrc: http://tldp.org/LDP/abs/html/sample-bashrc.html function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; } function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; } # kill by process name function killps() { local pid pname sig="-TERM" # Default signal. if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then echo "Usage: killps [-SIGNAL] pattern" return; fi if [ $# = 2 ]; then sig=$1 ; fi for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) ; do pname=$(my_ps | awk '$1~var { print $5 }' var=$pid ) if ask "Kill process $pid <$pname> with signal $sig?" then kill $sig $pid fi done } # Prints bash usage statistics function recentlyused() { history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -rn|head -10 } # Recodes a video for use with the Meizu player function convertmeizu() { # SCALING #The previous example outputs a video of 320x240, but we are assuming that the input file is in 4:3 scale (like PAL). #If the source video has a different aspect ratio (for example 16:9), we will need a letterboxing option. Let's imagine # we downloaded a video having a resolution of 448x256. If we scale down the width to 320 then we have a proportional # height of 176. So we need 64 extra pixel, meaning a black band of 32 pixels on the top and another at the bottom. # The command will become like this: # # mencoder Massive-Attack_karmacoma_1995.avi -oac mp3lame -lameopts cbr:mode=2:br=96 -af resample=44100 -srate 44100 # -ofps 20 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:cbp:trell:vbitrate=300 -vf scale=320:176 -vf-add expand=:240 # -ffourcc XVID -o MA_karmacoma_95.avi # # # -vf scale=320:176 -vf-add expand=:240 # This means that the scaled video size will be 320x176 like said before, while the -vf-add option will add extra # black pixels over and under yuor frame in order to reach the 240 required height. Clever, eh? if [ $# -ne 1 ] || [ ! -e "$1" ]; then echo "Usage: convertmeizu file.video" return fi require mencoder && mencoder "$1" -oac mp3lame -lameopts cbr:mode=2:br=96 -af resample=44100 -srate 44100 -ofps 20 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:cbp:trell:vbitrate=300 -vf scale=320:240 -ffourcc XVID -o "${1%.*}-meizu.avi" } # Directory-local reminder files function cd() { builtin cd "$@" [ -f .cdtext ] && cat .cdtext } # directory hardlink: pseudo-copies a directory structure # recursively by cloning all the directories and hardlinking # the files within function dln() { case "$#" in 1) arg1="$1"; arg2=".";; 2) arg1="$1"; arg2="$2";; *) echo "Usage: dln dir [dir]"; return;; esac IFS=$'\n'; curdir="`pwd`" [ ! ${arg1:0:1} = "/" ] && arg1="`pwd`"/"`basename \"$arg1\"`" [ ! ${arg2:0:1} = "/" ] && arg2="`pwd`"/"$2" mkdir -p "$arg2"; cd "${a%/*}" #cd "$arg1" find "`basename \"$arg1\"`" -type d -exec mkdir -p "$arg2"/"{}" \; find "`basename \"$arg1\"`" -type f -exec ln "${arg1%/*}"/"{}" "$arg2"/"{}" \; cd "$curdir" } # Recursively removes id3v1 and id3v2 tags from all mp3s in $1 # USAGE: id3remove function id3remove() { require id3v2 && { IFS=$'\n'; for i in `find $1 -type f -iname '*.mp3'`; do id3v2 -D $i; done } } # Frontend for playing varous file types function mp() { ext="${1##*.}" case "$ext" in dm_68|DM_68) require demoplay && demoplay "$1";; rm|ram|RM|RAM) require realplay && realplay "$1";; mp3|MP3|Mp3|mP3) require mplayer && mplayer "$1" &>/dev/null;; swf|SWF) require gflashplayer && gflashplayer "$1";; flv|asf|ASF|asx|ASX|avi|AVI|m2v|M2V|mov|MOV|mp4|MP4|mpeg|MPEG|mpg|MPG|wmv|WMV|wma|WMA) require mplayer && mplayer $* &>/dev/null ;; ogg|OGG) require ogg123 && ogg123 "$1";; pdf|PDF) require pdftohtml && require links && a="$(mktemp /tmp/$(basename $0).XXXXXX)"; pdftohtml "$1" -stdout > "$a" && links -force-html "$a" && rm "$a";; wav|WAV) require play && play "$1";; mod|MOD|s3m|S3M) require mikmod && mikmod "$1";; mid|MID) require mikmod && timidity "$1";; *) echo "Unknown File type: $ext" esac } # Frontend for extracting compressed files function ep { ext="${1##*.}" case "$ext" in gz) [ ${1:$[${#1}-7]:7} = ".tar.gz" ] && tar xfvz "$1" || gunzip "$1";; tgz) tar xfvz "$1";; bz2) [ ${1:$[${#1}-8]:8} = ".tar.bz2" ] && tar xfvj "$1" || bunzip2 "$1";; tar) tar xfv "$1";; zip|pk3|jar) require unzip && a="${1%%.*}"; mkdir "$a"; mv "$1" "$a"; cd "$a"; unzip "$1"; mv "$1" ".."; cd "..";; rar) require unrar && a="${1%%.*}"; mkdir "$a"; mv "$1" "$a"; cd "$a"; unrar x "$1"; mv "$1" ".."; cd "..";; ace) require unace && a="${1%%.*}"; mkdir "$a"; mv "$1" "$a"; cd "$a"; unace x "$1"; mv "$1" ".."; cd "..";; 7z) require 7za && a="${1%%.*}"; mkdir "$a"; mv "$1" "$a"; cd "$a"; 7za x "$1"; mv "$1" ".."; cd "..";; z|Z) require uncompress && uncompress "$1";; *) echo "Unknown File type: $ext" esac } # Compile and run source files function run { ext="${1##*.}" case "$ext" in c) i="gcc" echo -e "$i -Wall -pedantic -std=c99 -o \"${1%.*}\" \"$1\" && ./\"${1%.*}\"" $i -Wall -pedantic -std=c99 -o "${1%.*}" "$1" && ./"${1%.*}" ;; cc|cpp) i="g++" echo -e "$i -Wall -pedantic -std=c99 -o \"${1%.*}\" \"$1\" && ./\"${1%.*}\"" $i -Wall -pedantic -std=c99 -o "${1%.*}" "$1" && ./"${1%.*}" ;; scala) require scalac && require scala && scalac "$1" && scala "${1%.*}" ;; *) echo "run not defined for file type";; esac } # command line interface to leo online translator function leo { if [ $# -ne 1 ]; then echo "Usage: leo " else require lynx && require html2text && { lynx -source "http://dict.leo.org/?search=$1&searchLoc=0&relink=on&spellToler=standard§Hdr=on&tableBorder=1&cmpType=relaxed&lang=en" | \ grep 'search results' | html2text -style pretty | sed -e 's/|//g' } fi } # display ansi colours function f() { esc="\033[" echo -e "\t 40\t 41\t 42\t 43\t 44 45\t46\t 47" for fore in 30 31 32 33 34 35 36 37; do line1="$fore " line2=" " for back in 40 41 42 43 44 45 46 47; do line1="${line1}${esc}${back};${fore}m Normal ${esc}0m" line2="${line2}${esc}${back};${fore};1m Bold ${esc}0m" done echo -e "$line1\n$line2" done echo "" echo "# Example:" echo "#" echo "# Type a Blinkin TJEENARE in Swedens colours (Yellow on Blue)" echo "#" echo "# ESC" echo "# | CD" echo "# | | CD2" echo "# | | | FG" echo "# | | | | BG + m" echo "# | | | | | END-CD" echo "# | | | | | |" echo "# echo -e '\033[1;5;33;44mTJEENARE\033[0m'" echo "#" echo "# Sedika Signing off for now ;->" } # copy files and directories with a progress bar. requires pv to be installed. # usage: progress_cp (sources can be files or directories) function progress_cp() { require pv quiet && { if [ $# -lt 2 ]; then echo "Usage: cp [ ...] " return fi echo -en "Calculating size...\r" if [ $# -eq 2 -a -f "$1" -a ! -d "$2" ]; then size=$(du -sb "$1" | cut -f1) pv -s $size -p -t -e "$1" > "$2" return fi if [ $# -eq 2 -a -d "$1" -a ! -d "$2" ]; then files="$1/*" command="tar -cC \"$1\" . --transform 's|$1|$(basename "$1")|S'" target="$2" else command="tar -cspP" files= while [ $# -gt 1 ]; do if [ -r "$1" ]; then f=$(echo "$1"|sed 's#/$##g') command="$command \"$f\" --transform 's|$f|$(basename "$f")|S'" files="$files \"$f\"" fi shift done target="$1" fi ducmd="du -sbc $files | tail -n 1 | cut -f1" size=$(eval $ducmd) mkdir -p "$target" pipe="$command | pv -s $size -p -e -t | tar -xPC "$target" " eval $pipe } || { /bin/cp $* } } # Use pydf as df replacement function better_df() { require pydf quiet && { pydf|grep -v virtual|grep -v none|grep -v tmpfs } || { /bin/df $* } } # Function for remote-controlling a running screen-session (virtually type in the buffers) # Usage: screen_remote function screen_remote() { screen -X register 1 "$1" screen -p $2 -X paste 1 screen -p $2 -X stuff $'\n' }