LDP/LDP/guide/docbook/abs-guide/bashrc

817 lines
23 KiB
Bash
Raw Normal View History

2009-01-22 14:43:09 +00:00
#=============================================================
2003-05-12 14:45:05 +00:00
#
2009-01-22 14:43:09 +00:00
# PERSONAL $HOME/.bashrc FILE for bash-3.0 (or later)
# By Emmanuel Rouat <no-email>
2001-07-10 14:25:50 +00:00
#
2009-01-22 14:43:09 +00:00
# Last modified: Sun Nov 30 16:27:45 CET 2008
2001-07-10 14:25:50 +00:00
# This file is read (normally) by interactive shells only.
# Here is the place to define your aliases, functions and
# other interactive features like your prompt.
2001-10-15 14:21:41 +00:00
#
2009-01-22 14:43:09 +00:00
# The majority of the code here assumes you are on a GNU
# system (most likely a Linux box) and is based on code found
# on Usenet or internet. See for instance:
2003-05-12 14:45:05 +00:00
#
2009-01-22 14:43:09 +00:00
# http://tldp.org/LDP/abs/html/index.html
# http://www.caliban.org/bash/
# http://www.shelldorado.com/scripts/categories.html
# http://www.dotfiles.org/
2001-07-10 14:25:50 +00:00
#
2009-01-22 14:43:09 +00:00
# This bashrc file is a bit overcrowded -- remember it is just
# just an example. Tailor it to your needs.
#
#
#=============================================================
2001-07-10 14:25:50 +00:00
# --> Comments added by HOWTO author.
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
# Source global definitions (if any)
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
if [ -f /etc/bashrc ]; then
2001-10-15 14:21:41 +00:00
. /etc/bashrc # --> Read /etc/bashrc, if present.
2001-07-10 14:25:50 +00:00
fi
#-------------------------------------------------------------
2009-01-22 14:43:09 +00:00
# Automatic setting of $DISPLAY (if not set already).
# This works for linux - your mileage may vary. ...
2003-05-12 14:45:05 +00:00
# The problem is that different types of terminals give
2009-01-22 14:43:09 +00:00
# different answers to 'who am i' (rxvt in particular can be
# troublesome).
# I have not found a 'universal' method yet.
2001-07-10 14:25:50 +00:00
#-------------------------------------------------------------
2003-05-12 14:45:05 +00:00
function get_xserver ()
{
case $TERM in
2009-01-22 14:43:09 +00:00
xterm )
2005-02-09 20:53:43 +00:00
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
# Ane-Pieter Wieringa suggests the following alternative:
# I_AM=$(who am i)
# SERVER=${I_AM#*(}
# SERVER=${SERVER%*)}
XSERVER=${XSERVER%%:*}
2009-01-22 14:43:09 +00:00
;;
aterm | rxvt)
# Find some code that works here. ...
;;
2003-05-12 14:45:05 +00:00
esac
}
2001-07-10 14:25:50 +00:00
if [ -z ${DISPLAY:=""} ]; then
2003-05-12 14:45:05 +00:00
get_xserver
2009-01-22 14:43:09 +00:00
if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || \
${XSERVER} == "unix" ]]; then
DISPLAY=":0.0" # Display on local host.
else
DISPLAY=${XSERVER}:0.0 # Display on remote host.
2001-07-10 14:25:50 +00:00
fi
fi
2003-05-12 14:45:05 +00:00
export DISPLAY
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
# Some settings
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
ulimit -S -c 0 # Don't want any coredumps.
2001-07-10 14:25:50 +00:00
set -o notify
set -o noclobber
set -o ignoreeof
set -o nounset
2009-01-22 14:43:09 +00:00
#set -o xtrace # Useful for debuging.
2001-07-10 14:25:50 +00:00
2003-05-12 14:45:05 +00:00
# Enable options:
2001-10-15 14:21:41 +00:00
shopt -s cdspell
2001-07-10 14:25:50 +00:00
shopt -s cdable_vars
2001-10-15 14:21:41 +00:00
shopt -s checkhash
shopt -s checkwinsize
2001-07-10 14:25:50 +00:00
shopt -s sourcepath
2009-01-22 14:43:09 +00:00
shopt -s no_empty_cmd_completion
2003-05-12 14:45:05 +00:00
shopt -s cmdhist
shopt -s histappend histreedit histverify
2009-01-22 14:43:09 +00:00
shopt -s extglob # Necessary for programmable completion.
2003-05-12 14:45:05 +00:00
# Disable options:
shopt -u mailwarn
2009-01-22 14:43:09 +00:00
unset MAILCHECK # Don't want my shell to warn me of incoming mail.
2003-05-12 14:45:05 +00:00
export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
2009-01-22 14:43:09 +00:00
export HISTTIMEFORMAT="%H:%M > "
2003-05-12 14:45:05 +00:00
export HISTIGNORE="&:bg:fg:ll:h"
2009-01-22 14:43:09 +00:00
export HOSTFILE=$HOME/.hosts # Put list of remote hosts in ~/.hosts ...
2003-05-12 14:45:05 +00:00
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
# Greeting, motd etc...
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
# Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
2001-10-15 14:21:41 +00:00
NC='\e[0m' # No Color
2001-07-10 14:25:50 +00:00
# --> Nice. Has the same effect as using "ansi.sys" in DOS.
2009-01-22 14:43:09 +00:00
# Looks best on a terminal with black background.....
2006-12-20 21:11:55 +00:00
echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}\
${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"
2001-07-10 14:25:50 +00:00
date
2003-05-12 14:45:05 +00:00
if [ -x /usr/games/fortune ]; then
2009-01-22 14:43:09 +00:00
/usr/games/fortune -s # Makes our day a bit more fun.... :-)
2001-10-15 14:21:41 +00:00
fi
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
function _exit() # Function to run upon exit of shell.
2001-07-10 14:25:50 +00:00
{
echo -e "${RED}Hasta la vista, baby${NC}"
}
2003-05-12 14:45:05 +00:00
trap _exit EXIT
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2003-05-12 14:45:05 +00:00
# Shell Prompt
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
if [[ "${DISPLAY%%:0*}" != "" ]]; then
2003-05-12 14:45:05 +00:00
HILIT=${red} # remote machine: prompt will be partly red
else
HILIT=${cyan} # local machine: prompt will be partly cyan
fi
# --> Replace instances of \W with \w in prompt functions below
#+ --> to get display of full path name.
2001-07-10 14:25:50 +00:00
function fastprompt()
{
unset PROMPT_COMMAND
case $TERM in
2001-10-15 14:21:41 +00:00
*term | rxvt )
2009-01-22 14:43:09 +00:00
PS1="${HILIT}[\h]$NC \W > \[\033]0;\${TERM} [\u@\h] \w\007\]" ;;
linux )
PS1="${HILIT}[\h]$NC \W > " ;;
2001-10-15 14:21:41 +00:00
*)
2009-01-22 14:43:09 +00:00
PS1="[\h] \W > " ;;
2001-07-10 14:25:50 +00:00
esac
}
2009-01-22 14:43:09 +00:00
_powerprompt()
{
LOAD=$(uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g")
}
2001-07-10 14:25:50 +00:00
function powerprompt()
{
2009-01-22 14:43:09 +00:00
PROMPT_COMMAND=_powerprompt
case $TERM in
*term | rxvt )
PS1="${HILIT}[\A - \$LOAD]$NC\n[\u@\h \#] \W > \
\[\033]0;\${TERM} [\u@\h] \w\007\]" ;;
2001-10-15 14:21:41 +00:00
linux )
2009-01-22 14:43:09 +00:00
PS1="${HILIT}[\A - \$LOAD]$NC\n[\u@\h \#] \W > " ;;
2001-10-15 14:21:41 +00:00
* )
2009-01-22 14:43:09 +00:00
PS1="[\A - \$LOAD]\n[\u@\h \#] \W > " ;;
esac
2001-07-10 14:25:50 +00:00
}
2006-12-20 21:11:55 +00:00
powerprompt # This is the default prompt -- might be slow.
2009-01-22 14:43:09 +00:00
# If too slow, use fastprompt instead. ...
2001-07-10 14:25:50 +00:00
#===============================================================
#
2001-10-15 14:21:41 +00:00
# ALIASES AND FUNCTIONS
2001-07-10 14:25:50 +00:00
#
2009-01-22 14:43:09 +00:00
# Arguably, some functions defined here are quite big.
2001-07-10 14:25:50 +00:00
# If you want to make this file smaller, these functions can
2009-01-22 14:43:09 +00:00
# be converted into scripts and removed from here.
2001-10-15 14:21:41 +00:00
#
2001-07-10 14:25:50 +00:00
# Many functions were taken (almost) straight from the bash-2.04
# examples.
#
#===============================================================
#-------------------
# Personnal Aliases
#-------------------
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# -> Prevents accidentally clobbering files.
2003-05-12 14:45:05 +00:00
alias mkdir='mkdir -p'
2001-07-10 14:25:50 +00:00
alias h='history'
alias j='jobs -l'
2009-01-22 14:43:09 +00:00
alias which='type -a'
2001-07-10 14:25:50 +00:00
alias ..='cd ..'
alias path='echo -e ${PATH//:/\\n}'
2009-01-22 14:43:09 +00:00
alias libpath='echo -e ${LD_LIBRARY_PATH//:/\\n}'
2006-12-20 21:11:55 +00:00
alias print='/usr/bin/lp -o nobanner -d $LPDEST'
2009-01-22 14:43:09 +00:00
# Assumes LPDEST is defined (default printer)
2006-12-20 21:11:55 +00:00
alias pjet='enscript -h -G -fCourier9 -d $LPDEST'
2009-01-22 14:43:09 +00:00
# Pretty-print using enscript
alias du='du -kh' # Makes a more readable output.
2003-05-12 14:45:05 +00:00
alias df='df -kTh'
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
# The 'ls' family (this assumes you use a recent GNU ls)
#-------------------------------------------------------------
alias ll="ls -l --group-directories-first"
alias ls='ls -hF --color' # add colors for filetype recognition
alias la='ls -Al' # show hidden files
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
alias lm='ls -al |more' # pipe through 'more'
alias lr='ls -lR' # recursive ls
alias tree='tree -Csu' # nice alternative to 'recursive ls'
# If your version of 'ls' doesn't support --group-directories-first try this:
# function ll(){ ls -l "$@"| egrep "^d" ; ls -lXB "$@" 2>&-| \
# egrep -v "^d|total "; }
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-10-15 14:21:41 +00:00
# tailoring 'less'
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
alias more='less'
export PAGER=less
export LESSCHARSET='latin1'
2006-12-20 21:11:55 +00:00
export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-'
2009-01-22 14:43:09 +00:00
# Use this if lesspipe.sh exists
2001-07-10 14:25:50 +00:00
export LESS='-i -N -w -z-4 -g -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
# spelling typos - highly personnal and keyboard-dependent :-)
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
# A few fun ones
#-------------------------------------------------------------
function xtitle() # Adds some text in the terminal frame.
2001-10-15 14:21:41 +00:00
{
2003-05-12 14:45:05 +00:00
case "$TERM" in
2001-10-15 14:21:41 +00:00
*term | rxvt)
echo -n -e "\033]0;$*\007" ;;
2003-05-12 14:45:05 +00:00
*)
2009-01-22 14:43:09 +00:00
;;
2001-07-10 14:25:50 +00:00
esac
}
2009-01-22 14:43:09 +00:00
# aliases that use xtitle
2001-07-10 14:25:50 +00:00
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'
alias ncftp="xtitle ncFTP ; ncftp"
2003-05-12 14:45:05 +00:00
# .. and functions
2009-01-22 14:43:09 +00:00
function man()
2001-07-10 14:25:50 +00:00
{
2003-05-12 14:45:05 +00:00
for i ; do
2009-01-22 14:43:09 +00:00
xtitle The $(basename $1|tr -d .[:digit:]) manual
command man -F -a "$i"
2003-05-12 14:45:05 +00:00
done
2001-07-10 14:25:50 +00:00
}
2006-12-20 21:11:55 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
# Make the following commands run in background automatically:
#-------------------------------------------------------------
function te() # Wrapper around xemacs/gnuserv ...
2001-07-10 14:25:50 +00:00
{
if [ "$(gnuclient -batch -eval t 2>&-)" == "t" ]; then
2001-10-15 14:21:41 +00:00
gnuclient -q "$@";
2001-07-10 14:25:50 +00:00
else
2003-05-12 14:45:05 +00:00
( xemacs "$@" &);
2001-07-10 14:25:50 +00:00
fi
}
2009-01-22 14:43:09 +00:00
function soffice() { command soffice "$@" & }
function firefox() { command firefox "$@" & }
function xpdf() { command xpdf "$@" & }
#-------------------------------------------------------------
# File & string-related functions:
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
2003-05-12 14:45:05 +00:00
# Find a file with a pattern in name:
2009-01-22 14:43:09 +00:00
function ff() { find . -type f -iname '*'$*'*' -ls ; }
2006-12-20 21:11:55 +00:00
2003-05-12 14:45:05 +00:00
# Find a file with pattern $1 in name and Execute $2 on it:
2006-12-20 21:11:55 +00:00
function fe()
2009-01-22 14:43:09 +00:00
{ find . -type f -iname '*'${1:-}'*' -exec ${2:-file} {} \; ; }
2006-12-20 21:11:55 +00:00
2009-01-22 14:43:09 +00:00
# Find a pattern in a set of files and highlight them:
# (needs a recent version of egrep)
2003-05-12 14:45:05 +00:00
function fstr()
2001-07-10 14:25:50 +00:00
{
2003-05-12 14:45:05 +00:00
OPTIND=1
local case=""
local usage="fstr: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
while getopts :it opt
do
case "$opt" in
i) case="-i " ;;
*) echo "$usage"; return;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" -lt 1 ]; then
echo "$usage"
2001-10-15 14:21:41 +00:00
return;
2001-07-10 14:25:50 +00:00
fi
2009-01-22 14:43:09 +00:00
find . -type f -name "${2:-*}" -print0 | \
xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
2001-07-10 14:25:50 +00:00
}
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
function cuttail() # cut last n lines in file, 10 by default
2001-07-10 14:25:50 +00:00
{
nlines=${2:-10}
2001-10-15 14:21:41 +00:00
sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1
2001-07-10 14:25:50 +00:00
}
function lowercase() # move filenames to lowercase
{
for file ; do
filename=${file##*/}
case "$filename" in
*/*) dirname==${file%/*} ;;
*) dirname=.;;
esac
nf=$(echo $filename | tr A-Z a-z)
newname="${dirname}/${nf}"
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
echo "lowercase: $file --> $newname"
else
echo "lowercase: $file not changed."
fi
done
}
2009-01-22 14:43:09 +00:00
function swap() # Swap 2 filenames around, if they exist
{ #(from Uzi's bashrc).
local TMPFILE=tmp.$$
[ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
[ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
[ ! -e $2 ] && echo "swap: $2 does not exist" && return 1
mv "$1" $TMPFILE
2003-05-12 14:45:05 +00:00
mv "$2" "$1"
mv $TMPFILE "$2"
2001-07-10 14:25:50 +00:00
}
2009-01-22 14:43:09 +00:00
function extract() # Handy Extract Program.
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
2003-05-12 14:45:05 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-10-15 14:21:41 +00:00
# Process/system related functions:
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
2006-12-20 21:11:55 +00:00
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
function killps() # Kill by process name.
2001-10-15 14:21:41 +00:00
{
2009-01-22 14:43:09 +00:00
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
2001-10-15 14:21:41 +00:00
}
2009-01-22 14:43:09 +00:00
function my_ip() # Get IP adresses.
2001-10-15 14:21:41 +00:00
{
2006-12-20 21:11:55 +00:00
MY_IP=$(/sbin/ifconfig ppp0 | awk '/inet/ { print $2 } ' | \
sed -e s/addr://)
MY_ISP=$(/sbin/ifconfig ppp0 | awk '/P-t-P/ { print $3 } ' | \
sed -e s/P-t-P://)
2001-10-15 14:21:41 +00:00
}
2009-01-22 14:43:09 +00:00
function ii() # Get current host related info.
2001-10-15 14:21:41 +00:00
{
2009-01-22 14:43:09 +00:00
echo -e "\nYou are logged on ${RED}$HOST"
echo -e "\nAdditionnal information:$NC " ; uname -a
echo -e "\n${RED}Users logged on:$NC " ; w -h
echo -e "\n${RED}Current date :$NC " ; date
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Memory stats :$NC " ; free
my_ip 2>&- ;
echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
echo -e "\n${RED}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
echo -e "\n${RED}Open connections :$NC "; netstat -pan --inet;
echo
2001-10-15 14:21:41 +00:00
}
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
# Misc utilities:
2009-01-22 14:43:09 +00:00
#-------------------------------------------------------------
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
function repeat() # Repeat n times command.
2001-07-10 14:25:50 +00:00
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do # --> C-like syntax
2001-10-15 14:21:41 +00:00
eval "$@";
2001-07-10 14:25:50 +00:00
done
}
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
function ask() # See 'killps' for example of use.
2001-07-10 14:25:50 +00:00
{
echo -n "$@" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
2009-01-22 14:43:09 +00:00
function corename() # Get name of app that created a corefile.
{
for file ; do
echo -n $file : ; gdb --core=$file --batch | head -1
done
}
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
#=========================================================================
# PROGRAMMABLE COMPLETION - ONLY SINCE BASH-2.04
# Most are taken from the bash 2.05 documentation and from Ian McDonald's
# 'Bash completion' package (http://www.caliban.org/bash/#completion).
# You will in fact need bash more recent than 3.0 for some features.
#=========================================================================
if [ "${BASH_VERSION%.*}" \< "3.0" ]; then
echo "You will need to upgrade to version 3.0 \
for full programmable completion features."
return
2001-07-10 14:25:50 +00:00
fi
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
shopt -s extglob # Necessary,
#set +o nounset # otherwise some completions will fail.
2001-07-10 14:25:50 +00:00
complete -A hostname rsh rcp telnet rlogin r ftp ping disk
complete -A export printenv
complete -A variable export local readonly unset
complete -A enabled builtin
complete -A alias alias unalias
complete -A function function
complete -A user su mail finger
2009-01-22 14:43:09 +00:00
complete -A helptopic help # Currently, same as builtins.
2001-07-10 14:25:50 +00:00
complete -A shopt shopt
complete -A stopped -P '%' bg
complete -A job -P '%' fg jobs disown
complete -A directory mkdir rmdir
2001-10-15 14:21:41 +00:00
complete -A directory -o default cd
2003-05-12 14:45:05 +00:00
# Compression
complete -f -o default -X '*.+(zip|ZIP)' zip
complete -f -o default -X '!*.+(zip|ZIP)' unzip
complete -f -o default -X '*.+(z|Z)' compress
complete -f -o default -X '!*.+(z|Z)' uncompress
complete -f -o default -X '*.+(gz|GZ)' gzip
complete -f -o default -X '!*.+(gz|GZ)' gunzip
complete -f -o default -X '*.+(bz2|BZ2)' bzip2
complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2
2009-01-22 14:43:09 +00:00
complete -f -o default -X '!*.+(zip|ZIP|z|Z|gz|GZ|bz2|BZ2)' extract
# Documents - Postscript,pdf,dvi.....
complete -f -o default -X '!*.+(ps|PS)' gs ghostview ps2pdf ps2ascii
complete -f -o default -X '!*.+(dvi|DVI)' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.+(pdf|PDF)' acroread pdf2ps
complete -f -o default -X \
'!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv
2001-10-15 14:21:41 +00:00
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
2003-05-12 14:45:05 +00:00
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps
2009-01-22 14:43:09 +00:00
complete -f -o default -X \
'!*.+(doc|DOC|xls|XLS|ppt|PPT|sx?|SX?|csv|CSV|od?|OD?|ott|OTT)' soffice
2003-05-12 14:45:05 +00:00
# Multimedia
2009-01-22 14:43:09 +00:00
complete -f -o default -X \
'!*.+(gif|GIF|jp*g|JP*G|bmp|BMP|xpm|XPM|png|PNG)' xv gimp ee gqview
2003-05-12 14:45:05 +00:00
complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321
complete -f -o default -X '!*.+(ogg|OGG)' ogg123
2009-01-22 14:43:09 +00:00
complete -f -o default -X \
'!*.@(mp[23]|MP[23]|ogg|OGG|wav|WAV|pls|m3u|xm|mod|s[3t]m|it|mtm|ult|flac)' xmms
complete -f -o default -X \
'!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|asf|vob|VOB|bin|dat|vcd|\
ps|pes|fli|viv|rm|ram|yuv|mov|MOV|qt|QT|wmv|mp3|MP3|ogg|OGG|\
ogm|OGM|mp4|MP4|wav|WAV|asx|ASX)' xine
2001-10-15 14:21:41 +00:00
2003-05-12 14:45:05 +00:00
complete -f -o default -X '!*.pl' perl perl5
2009-01-22 14:43:09 +00:00
2001-10-15 14:21:41 +00:00
# This is a 'universal' completion function - it works when commands have
2003-05-12 14:45:05 +00:00
# a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
2009-01-22 14:43:09 +00:00
# Needs the '-o' option of grep
# (try the commented-out version if not available).
2003-05-12 14:45:05 +00:00
2009-01-22 14:43:09 +00:00
# First, remove '=' from completion word separators
# (this will allow completions like 'ls --color=auto' to work correctly).
COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
_get_longopts()
2003-05-12 14:45:05 +00:00
{
2009-01-22 14:43:09 +00:00
#$1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \
#grep ^"$2" |sort -u ;
$1 --help | grep -o -e "--[^[:space:].,]*" | grep -e "$2" |sort -u
2003-05-12 14:45:05 +00:00
}
2009-01-22 14:43:09 +00:00
_longopts()
2001-10-15 14:21:41 +00:00
{
2009-01-22 14:43:09 +00:00
local cur
cur=${COMP_WORDS[COMP_CWORD]}
case "${cur:-*}" in
-*) ;;
*) return ;;
2001-10-15 14:21:41 +00:00
esac
case "$1" in
2009-01-22 14:43:09 +00:00
\~*) eval cmd="$1" ;;
*) cmd="$1" ;;
2001-10-15 14:21:41 +00:00
esac
2009-01-22 14:43:09 +00:00
COMPREPLY=( $(_get_longopts ${1} ${cur} ) )
2001-10-15 14:21:41 +00:00
}
2009-01-22 14:43:09 +00:00
complete -o default -F _longopts configure bash
complete -o default -F _longopts wget id info a2ps ls recode
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
_tar()
2001-07-10 14:25:50 +00:00
{
2009-01-22 14:43:09 +00:00
local cur ext regex tar untar
2001-07-10 14:25:50 +00:00
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
2009-01-22 14:43:09 +00:00
# If we want an option, return the possible long options.
case "$cur" in
-*) COMPREPLY=( $(_get_longopts $1 $cur ) ); return 0;;
2001-07-10 14:25:50 +00:00
esac
2009-01-22 14:43:09 +00:00
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $( compgen -W 'c t x u r d A' -- $cur ) )
return 0
fi
case "${COMP_WORDS[1]}" in
?(-)c*f)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
+([^Izjy])f)
ext='tar'
regex=$ext
;;
*z*f)
ext='tar.gz'
regex='t\(ar\.\)\(gz\|Z\)'
;;
*[Ijy]*f)
ext='t?(ar.)bz?(2)'
regex='t\(ar\.\)bz2\?'
;;
*)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
2001-07-10 14:25:50 +00:00
esac
2009-01-22 14:43:09 +00:00
if [[ "$COMP_LINE" == tar*.$ext' '* ]]; then
# Complete on files in tar file.
#
# Get name of tar file from command line.
tar=$( echo "$COMP_LINE" | \
sed -e 's|^.* \([^ ]*'$regex'\) .*$|\1|' )
# Devise how to untar and list it.
untar=t${COMP_WORDS[1]//[^Izjyf]/}
COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \
2>/dev/null ) )" -- "$cur" ) )
return 0
2001-07-10 14:25:50 +00:00
else
2009-01-22 14:43:09 +00:00
# File completion on relevant files.
COMPREPLY=( $( compgen -G $cur\*.$ext ) )
2001-07-10 14:25:50 +00:00
fi
2009-01-22 14:43:09 +00:00
return 0
}
complete -F _tar -o default tar
_make()
{
local mdef makef makef_dir="." makef_inc gcmd cur prev i;
COMPREPLY=();
cur=${COMP_WORDS[COMP_CWORD]};
prev=${COMP_WORDS[COMP_CWORD-1]};
case "$prev" in
-*f)
COMPREPLY=($(compgen -f $cur ));
return 0
;;
esac;
case "$cur" in
-*)
COMPREPLY=($(_get_longopts $1 $cur ));
return 0
;;
esac;
# make reads `GNUmakefile', then `makefile', then `Makefile'
if [ -f ${makef_dir}/GNUmakefile ]; then
makef=${makef_dir}/GNUmakefile
elif [ -f ${makef_dir}/makefile ]; then
makef=${makef_dir}/makefile
elif [ -f ${makef_dir}/Makefile ]; then
makef=${makef_dir}/Makefile
else
makef=${makef_dir}/*.mk # Local convention.
fi
# Before we scan for targets, see if a Makefile name was
# specified with -f ...
2001-07-10 14:25:50 +00:00
for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
2009-01-22 14:43:09 +00:00
if [[ ${COMP_WORDS[i]} == -f ]]; then
# eval for tilde expansion
eval makef=${COMP_WORDS[i+1]}
break
2001-10-15 14:21:41 +00:00
fi
2001-07-10 14:25:50 +00:00
done
2009-01-22 14:43:09 +00:00
[ ! -f $makef ] && return 0
# deal with included Makefiles
makef_inc=$( grep -E '^-?include' $makef | \
sed -e "s,^.* ,"$makef_dir"/," )
for file in $makef_inc; do
[ -f $file ] && makef="$makef $file"
done
2001-07-10 14:25:50 +00:00
2009-01-22 14:43:09 +00:00
# If we have a partial word to complete, restrict completions to
# matches of that word.
if [ -n "$cur" ]; then gcmd='grep "^$cur"' ; else gcmd=cat ; fi
COMPREPLY=( $( awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \
{split($1,A,/ /);for(i in A)print A[i]}' \
$makef 2>/dev/null | eval $gcmd ))
2001-07-10 14:25:50 +00:00
}
2009-01-22 14:43:09 +00:00
complete -F _make -X '+($*|*.[cho])' make gmake pmake
2001-07-10 14:25:50 +00:00
2001-10-15 14:21:41 +00:00
2009-01-22 14:43:09 +00:00
_killall()
2001-07-10 14:25:50 +00:00
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# get a list of processes (the first sed evaluation
# takes care of swapped out processes, the second
# takes care of getting the basename of the process)
COMPREPLY=( $( /usr/bin/ps -u $USER -o comm | \
2001-10-15 14:21:41 +00:00
sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
awk '{if ($0 ~ /^'$cur'/) print $0}' ))
2001-07-10 14:25:50 +00:00
return 0
}
2001-10-15 14:21:41 +00:00
complete -F _killall killall killps
2001-07-10 14:25:50 +00:00
2003-05-12 14:45:05 +00:00
2009-01-22 14:43:09 +00:00
# A meta-command completion function for commands like sudo(8), which need to
# first complete on a command, then complete according to that command's own
# completion definition - currently not quite foolproof,
# but still quite useful (By Ian McDonald, modified by me).
_meta_comp()
2003-05-12 14:45:05 +00:00
{
local cur func cline cspec
2009-01-22 14:43:09 +00:00
2003-05-12 14:45:05 +00:00
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
2009-01-22 14:43:09 +00:00
cmdline=${COMP_WORDS[@]}
if [ $COMP_CWORD = 1 ]; then
COMPREPLY=( $( compgen -c $cur ) )
2003-05-12 14:45:05 +00:00
else
2009-01-22 14:43:09 +00:00
cmd=${COMP_WORDS[1]} # Find command.
cspec=$( complete -p ${cmd} ) # Find spec of that command.
# COMP_CWORD and COMP_WORDS() are not read-only,
# so we can set them before handing off to regular
# completion routine:
# Get current command line minus initial command,
cline="${COMP_LINE#$1 }"
# split current command line tokens into array,
COMP_WORDS=( $cline )
# set current token number to 1 less than now.
COMP_CWORD=$(( $COMP_CWORD - 1 ))
# If current arg is empty, add it to COMP_WORDS array
# (otherwise that information will be lost).
if [ -z $cur ]; then COMP_WORDS[COMP_CWORD]="" ; fi
if [ "${cspec%%-F *}" != "${cspec}" ]; then
# if -F then get function:
func=${cspec#*-F }
func=${func%% *}
eval $func $cline # Evaluate it.
else
func=$( echo $cspec | sed -e 's/^complete//' -e 's/[^ ]*$//' )
COMPREPLY=( $( eval compgen $func $cur ) )
fi
2003-05-12 14:45:05 +00:00
fi
2009-01-22 14:43:09 +00:00
2003-05-12 14:45:05 +00:00
}
2009-01-22 14:43:09 +00:00
complete -o default -F _meta_comp nohup \
eval exec trace truss strace sotruss gdb
complete -o default -F _meta_comp command type which man nice time
2003-05-12 14:45:05 +00:00
2001-07-10 14:25:50 +00:00
# Local Variables:
# mode:shell-script
# sh-shell:bash
2001-10-15 14:21:41 +00:00
# End: