#!/bin/echo Do not execute this script; instead, source # http://www.runslinux.net/projects/bashrc/bashrc # .bashrc # Copyright (C) 2005 Chris Povirk # Ideas and code here come from a variety of other sources. # Some are cited in the file. I will try to get together a comprehensive list. # # This program 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 2 # of the License, or (at your option) any later version. # # This program 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 program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # /etc/skel/.bashrc: # $Header: /var/cvsroot/gentoo-src/rc-scripts/etc/skel/.bashrc,v 1.9 2004/07/22 02:34:08 agriffis Exp $ # # This file is sourced by all *interactive* bash shells on startup, # including some apparently interactive shells such as scp and rcp # that can't tolerate any output. # PATH stuff. for POSSIBLE_PATH_DIR in "$HOME/bin" "$HOME/afsbin"; do [[ -d "$POSSIBLE_PATH_DIR" ]] && [[ -x "$POSSIBLE_PATH_DIR" ]] && export PATH="$PATH:$POSSIBLE_PATH_DIR" done # Use GNU ls, etc. on unixs machines by moving /usr/local/bin to the front of the path. if [[ -x /usr/local/bin/ls ]]; then PATH=`echo $PATH | sed -e 's#\(.*\):/usr/local/bin#/usr/local/bin:\1#'` fi # Test for an interactive shell. There is no need to set anything # past this point for scp and rcp, and it's important to refrain from # outputting anything in those cases. if [[ $- != *i* ]]; then # Shell is non-interactive. Be done now return fi # Shell is interactive. It is okay to produce output at this point, # though this example doesn't produce any. Do setup for # command-line interactivity. # Special prompt for Anthony, as requested. Show the last two components of the current directory, e.g.: # / # /home # /home/cpovirk # cpovirk/.mozilla # .mozilla/firefox if [[ "$USER" == "ajz3" || "$USER" == "crazyivan" ]]; then IS_ANTHONY=true PROMPT_DIRSTRING='$SWD' OTHER_PROMPT_COMMAND='if [[ "$PWD" == "/" ]]; then SWD="/"; else CURRENT_BASENAME=`basename $PWD`; PARENT=`dirname $PWD`; if [[ "$PARENT" == "/" ]]; then SWD="/$CURRENT_BASENAME"; else PARENT_BASENAME=`basename $PARENT`; SWD="$PARENT_BASENAME/$CURRENT_BASENAME"; if [[ `dirname $PARENT` == "/" ]]; then SWD="/$SWD"; fi; fi; fi' USERNAME_PITT=ajz3 USERNAME_DREAMHOST=crazyivan else IS_ANTHONY=false PROMPT_DIRSTRING='\W' OTHER_PROMPT_COMMAND='true' # Do nothing. USERNAME_PITT=crp11 USERNAME_DREAMHOST=crp11 fi # TODO nobody (not even less on Gentoo) likes rxvt-unicode # TODO it seems that unixs lynx is in color if TERM=screen. # Set up TERM variables. # vt100 and xterm have no color in vim (at least on unixs), but if we call them xterm-color, they will. # (vt100 for F-Secure SSH.) # This may well be the case for some other terms, so I'm putting them here. # Also set up a variable to indicate whether to set up the title functions. # TODO gnome-terminal, or however it reports itself case $TERM in screen) TERM_IS_COLOR=true TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=false TERM_NOT_RECOGNIZED_BY_SUN_UTILS=false TERM_CAN_TITLE=true ;; xterm-color|color_xterm|rxvt|Eterm|screen*) # screen.linux|screen-w TERM_IS_COLOR=true TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=false TERM_NOT_RECOGNIZED_BY_SUN_UTILS=true TERM_CAN_TITLE=true ;; linux) TERM_IS_COLOR=true TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=false TERM_NOT_RECOGNIZED_BY_SUN_UTILS=true TERM_CAN_TITLE=false ;; xterm|vt100) TERM_IS_COLOR=true TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=true TERM_NOT_RECOGNIZED_BY_SUN_UTILS=false TERM_CAN_TITLE=true ;; *xterm*|eterm|rxvt*) TERM_IS_COLOR=true TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=true TERM_NOT_RECOGNIZED_BY_SUN_UTILS=true TERM_CAN_TITLE=true ;; *) TERM_IS_COLOR=false TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM=false TERM_NOT_RECOGNIZED_BY_SUN_UTILS=false TERM_CAN_TITLE=false ;; esac # dircolors... make sure that we have a color terminal, dircolors exists, and ls supports it. if $TERM_IS_COLOR && ( dircolors --help && ls --color ) &> /dev/null; then # For some reason, the unixs machines need me to use $HOME instead of ~ # List files from highest priority to lowest. As soon as the loop finds one that works, it will exit. for POSSIBLE_DIR_COLORS in "$HOME/.dir_colors" "/etc/DIR_COLORS"; do [[ -f "$POSSIBLE_DIR_COLORS" ]] && [[ -r "$POSSIBLE_DIR_COLORS" ]] && eval `dircolors -b "$POSSIBLE_DIR_COLORS"` && break done alias ls="ls --color=auto" alias ll="ls --color=auto -l" else # No color, so put a slash at the end of directory names, etc. to differentiate. alias ls="ls -F" alias ll="ls -lF" fi # Report in KB increments, not 512-byte ones. alias du="du -k" # uncomment the following to activate bash-completion: [[ -f /etc/profile.d/bash-completion ]] && source /etc/profile.d/bash-completion # Additional complete functions can be enabled by symlinking them from # /usr/share/bash-completion to /etc/bash_completion.d # Window titling for xterm, etc. and for screen. if $TERM_CAN_TITLE; then PROMPT_CONTENT='${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}' function _setPromptCommandToDisplayXTermTitle() { export PROMPT_COMMAND="$OTHER_PROMPT_COMMAND; echo -ne \"\033]0;$*\007\"" } function _setScreenTitle() { echo -ne "\033k$*\033\\" } function title() { if [[ "$TERM" == "screen" ]]; then _setScreenTitle "$*" _setPromptCommandToDisplayXTermTitle "$* <[$WINDOW]$PROMPT_CONTENT>" else _setPromptCommandToDisplayXTermTitle "$* <$PROMPT_CONTENT>" fi } function untitle() { if [[ "$TERM" == "screen" ]]; then _setScreenTitle `basename $SHELL` _setPromptCommandToDisplayXTermTitle "[$WINDOW]$PROMPT_CONTENT" else _setPromptCommandToDisplayXTermTitle "$PROMPT_CONTENT" fi } untitle else export PROMPT_COMMAND="$OTHER_PROMPT_COMMAND" fi # Basic prompt from /etc/profile # "Reset"-type escape sequence at the beginning added so that the dark vim background is restored when leaving vim in aterm+screen. # \[ and \] tell Bash to ignore the characters inside when calculating prompt length. # http://users.dslextreme.com/~craig.lawson/linux_notes/bashrc.html # Now that I have switched to double quotes, I need to escape the escaped \$ in order to get a # prompt as root. if $TERM_IS_COLOR; then # Color prompt if [[ `whoami` = 'root' ]]; then export PS1="\[\033[00m\033[01;31m\]\h \[\033[01;34m\]$PROMPT_DIRSTRING \\\$ \[\033[00m\]" else export PS1="\[\033[00m\033[01;32m\]\u@\h \[\033[01;34m\]$PROMPT_DIRSTRING \\\$ \[\033[00m\]" fi # Color grep. # Make sure grep accepts the color argument before setting it. ( echo dummyline | grep --color=auto dummyline &> /dev/null ) && alias grep="grep --color=auto" else # Monochrome prompt if [[ `whoami` = 'root' ]]; then export PS1="\h $PROMPT_DIRSTRING \\\$ " else export PS1="\u@\h $PROMPT_DIRSTRING \\\$ " fi # TODO grep color set to underline/reverse video? fi export PS2="> " # This will be modified below if TERM=xterm is needed. alias lynx='lynx -accept_all_cookies' if which screen &> /dev/null; then SCREENEXISTS=true else SCREENEXISTS=false fi # Some old programs don't understand xterm-color. if $TERM_NOT_RECOGNIZED_BY_SUN_UTILS; then function _xtermalias() { which "$1" &> /dev/null && alias "$1"="TERM=xterm \"$1\"" } # Does lynx understand xterm-color? # Don't forget to keep -accept_all_cookies. # if ! ( echo q && echo y ) | lynx /dev/null &> /dev/null if which lynx &> /dev/null; then LYNXYEAR=`lynx -version | head -n 1 | sed -e 's/.*(.......//' | sed -e 's/.$//'` if [[ $LYNXYEAR -le 2001 ]]; then alias lynx='TERM=xterm lynx -accept_all_cookies' fi fi LESSVERSION=`less --version | head -n 1 | sed -e 's/less //'` if [[ `echo "n = 0; if ( $LESSVERSION < 381 ) n = 1; n;" | bc` -eq 1 ]]; then _xtermalias less fi # Not sure of any better way to test for this. if [[ `uname` == 'SunOS' ]]; then _xtermalias more fi case `top -v 2>&1 | head -n 1` in *procps*) ;; *) _xtermalias top ;; esac if $SCREENEXISTS; then SCREENMAJORVERSION=`screen --version | sed -e 's/^Screen version //' | sed -e 's/\..*$//'` if [[ `echo "n = 0; if ( $SCREENMAJORVERSION <= 3 ) n = 1; n;" | bc` -eq 1 ]]; then _xtermalias screen fi fi if which pine &> /dev/null; then PINEVERSION=`( pine -v | sed -e 's/^Pine //' | sed -e 's/ .*//' ) 2>/dev/null` # See if we have version 4.56 or older. if [[ `echo "n = 0; if ( $PINEVERSION <= 4.56 ) n = 1; n;" | bc` -eq 1 ]]; then _xtermalias pine fi fi fi # When I emerged dvdrip, it told me this: # * for Perl 5.8.x you have to set PERLIO to read TOC properly # * for bash: export PERLIO=stdio # * for csh: setenv PERLIO stdio # * into your /.rc export PERLIO=stdio # The manpage for 'true' is used below because a) it is short and b) it is available on all systems. # Note also that we call `which man` in case man has already been redefined, likely by an earlier sourcing of this script. MANLOC=`which man` # Test to determine if MANPAGER or PAGER is called. # My original plan was to assign MANPAGER and PAGER to different commands (as below) but to test their output. # In retrospect, the way I do it now is cleaner, but it came about only because SunOS man ignores PAGER (using cat instead) if its output is being captured. if MANPAGER=false PAGER=true $MANLOC true; then USES_MANPAGER_FOR_MAN=false USES_PAGER_FOR_MAN=true else USES_MANPAGER_FOR_MAN=true USES_PAGER_FOR_MAN=false fi 2> /dev/null # Instead of checking for the existence and location of vim repeatedly, let's do it once and save the results. if VIMLOC=`which vim 2> /dev/null`; then VIMEXISTS=true else VIMEXISTS=false fi # vim makes a good man reader and general pager, so use it if it's available. if $VIMEXISTS; then # Set up vim as man reader. # http://forums.gentoo.org/viewtopic.php?t=80756 # TODO show title again if we use K to view another man page and then return to the first. # Trying to reduce the amount of ugly quoting by using these functions. function _printvimpagercommand() { cat <<-END vim -R -M -c "map q :q<CR>" -c "set foldlevel=99" -c "set notitle" - END } function _printvimmanpagercommand() { # Set read-only mode, man formatting, etc. # Also, set up a title of the form 'LS(1) - man' # Find the title by searching line by line from the start of the file until we find a non-empty series of non-spacing characters at the end of a line. # On my Gentoo systems, the title will be on the first line (and it will also be at the start of the line); however, this is not the case on SunOS. # redraw! is necessary because the echo command that sets the title messes up the aterm background. echo -n 'col -b | ' echo -n `_printvimpagercommand` echo -n ' ' # \047 is octal for the single quote. # Using class [not tab or space] instead of \S because no number of backslashes before the S seems to work. cat <<-END -c "set ft=man" -c "let titlelnum = 1 | let pagetitle = \"\" | while pagetitle == \"\" | let pagetitle = matchstr(getline(titlelnum), \"[^ ]*$\") | let titlelnum = titlelnum + 1 | endwhile | let titlestring = pagetitle | execute \"silent !echo -ne \047\033];\" . titlestring . \" - man\007\047\"" -c "redraw!" END } function _printfilterpagerforpagerprinter() { # No } in _printvimmanpagercommand or this will break. # Use a Perl program and <> so that it accepts either a stream or a filename. # Also, make sure STDOUT is a tty (http://www.unix.org.ua/orelly/perl/cookbook/ch15_03.htm). If it is not, just dump to STDOUT instead of opening vim, which would misbehave. This allows things like 'man bash | grep case'. (Note that piping through 'col -b' is still important here.) echo "perl -e 'if(-t STDOUT) { open(VIM, q{| `$1`}); while(<>) { print VIM; } close(VIM); } else { open(COL, q{| col -b}); while(<>) { print COL; } close(COL); }'" } function _printmanpager() { _printfilterpagerforpagerprinter _printvimmanpagercommand } function _printpager() { _printfilterpagerforpagerprinter _printvimpagercommand } if $USES_MANPAGER_FOR_MAN; then # Newer version of man, like on my computer. Uses MANPAGER. export MANPAGER=`_printmanpager` else # Doesn't recognize MANPAGER. # PAGER may be used by other commands, so create a function for man only. function _notermman() { ( export PAGER="`_printmanpager`"; $MANLOC "$@" ) } # Regardless of how man gets its input, we want to set up TERM properly. # TODO why can't these be aliases? if $TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM; then function man() { TERM=xterm-color _notermman "$@" } else function man() { _notermman "$@" } fi fi # TODO set up perldoc alias? # # Set up vim as PAGER. # TODO see if this actually works. Fine for perldoc, but '$PAGER filename' doesn't work. Should it? # TODO consider adding col -b to vim command even here to deal with perldoc, etc. # export PAGER=`_printpager` # export PAGER=`_printmanpager` export PAGER=`which less` else # vim does not exist. less will be the pager, and we need to know if it recognizes this TERM. if $TERM_NOT_RECOGNIZED_BY_SUN_UTILS; then # TODO copied from above LESSVERSION=`less --version | head -n 1 | sed -e 's/less //'` if [[ `echo "n = 0; if ( $LESSVERSION < 381 ) n = 1; n;" | bc` -eq 1 ]]; then _xtermalias man fi fi # TODO set up perldoc, etc. aliases? or will it use my 'less' alias? I'm assuming not... export PAGER=`which less` fi function _xtermcoloralias() { which "$1" &> /dev/null && alias "$1"="TERM=xterm-color \"$1\"" } # javalab computers have no readlink, but they have Perl... # This will create a limited version of readlink that at least supports -f as the first argument, I think. # TODO this works... but not on javalab, which must have a different version of Cwd or something, because it fails when called on non-directories #if ! which readlink &> /dev/null #then # function readlink() # { # perl -e 'use Cwd; my $canonicalize = 0; if($ARGV[0] eq "-f") { $canonicalize = 1; shift; } ; die "readlink: wrong number of arguments\n" unless @ARGV == 1; my $file = shift; exit 1 unless -e $file; if($canonicalize) { my $realpath = Cwd::realpath($file); print "$realpath\n"; exit 0; } else { my $link = readlink $file; if(!$link) { exit 1; } else { print "$link\n"; exit 0; } }' -- "$@" # } #fi VILOC=`readlink -f \`which vi\` 2> /dev/null` # If vi and view are not vim, then they're not going to recognize some TERMs. if $TERM_NOT_RECOGNIZED_BY_SUN_UTILS; then if [[ -n "$VILOC" && "$VIMLOC" != "$VILOC" ]]; then _xtermcoloralias vi fi if VIEWLOC=`readlink -f \`which view\` 2> /dev/null` && [[ "$VIEWLOC" != "$VIMLOC" ]]; then _xtermcoloralias view fi fi # TODO it would be nice if we didn't check vi twice. if $VIMEXISTS; then # Set up TERM=xterm-color for vim and its many variations. if $TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM; then # Do this manually to save time, as .bashrc is getting a bit slow. # There is no need to call _xtermcoloralias() because we have already verified that vim exists. alias vim='TERM=xterm-color vim' # If any of these programs exist and are linked to vim, then set TERM=xterm-color for them. for PROG in ex view rvim rview vi vimdiff; do if PROGLOC=`readlink -f \`which $PROG\` 2> /dev/null` && [[ "$VIMLOC" == "$PROGLOC" ]]; then _xtermcoloralias "$PROG" fi done fi # If vi exists and is NOT linked to vim (the opposite of what was done above), then alias it to vim. # Also, set TERM=xterm-color if appropriate. # If vi is linked to vim, then it was dealt with above. if [[ "$VIMLOC" != "$VILOC" ]]; then if $TERM_NOT_RECOGNIZED_AS_COLOR_BY_VIM; then alias vi='TERM=xterm-color vim' else alias vi='vim' fi fi fi # List files from highest priority to lowest. As soon as the loop finds one that works, it will exit. for POSSIBLE_INPUTRC in "$HOME/.inputrc" "/etc/inputrc"; do [[ -f "$POSSIBLE_INPUTRC" ]] && [[ -r "$POSSIBLE_INPUTRC" ]] && export INPUTRC="$POSSIBLE_INPUTRC" && break done # Easy access to Gaim logs. LOGDIR="$HOME/.gaim/logs/aim/beigetangerine" [[ -d $LOGDIR ]] && alias cdlogs="cd $LOGDIR" # Don't think this is actually a good idea. ## Try to avoid accumulating multiple copies of the same directory. #function cdpath() #{ # [[ -d $1 ]] && export CDPATH="${CDPATH/:$1/}:$1" #} # #cdpath "$HOME" #cdpath "$HOME/public/html" #cdpath "$LOGDIR" HOSTNAME=`hostname` [[ "$HOSTNAME" == 'unixs1.cis.pitt.edu' ]] || alias unixs="ssh $USERNAME_PITT@unix.cis.pitt.edu" [[ "$HOSTNAME" == 'java1h.javalab.cs.pitt.edu' ]] || alias javalab="ssh $USERNAME_PITT@java1h.javalab.cs.pitt.edu" [[ "$HOSTNAME" == 'baht' ]] || alias dreamhost="ssh $USERNAME_DREAMHOST@crazyivan.org" # Check window size after each command to avoid annoying effects of resizing. shopt -s checkwinsize # Fix minor typos in cd commands. shopt -s cdspell # Do not exit on CTRL+D (especially seems to be a problem for me in screen). # (Well, actually, exit if I press it 100 times, but for practical purposes, do not exit.) export IGNOREEOF=100 # http://f0rked.com/core/irssi#Learning_Screen if $SCREENEXISTS; then alias attach='screen -raAd' fi if $VIMEXISTS; then export EDITOR="$VIMLOC" function vimwhich() { # If the which command fails, we don't want to run vim. WHICH=`which "$@"` && vim "$WHICH" } fi if FIREFOXLOC=`which firefox 2> /dev/null`; then export BROWSER="$FIREFOXLOC" fi function llwhich() { # If the which command fails, we don't want to run ll. WHICH=`which "$@"` && ll "$WHICH" } # Notify me immediately when a backgrounded command dies. set -o notify # Don't include .keep, CVS, and Subversion special directories in tab-completion or globbing. IGNORE='.keep:._keepme' for D in CVS .svn; do IGNORE="$IGNORE:$D:$D/:*/$D:*/$D/" done export FIGNORE="$IGNORE" export GLOBIGNORE="$IGNORE" # Run find with a bunch of '-not -path' expressions corresponding to the IGNORE entries above. # TODO doesn't appear to work on unixs, though that's not a big deal function f() { # The sensible way doesn't work for some reason, so we send it through perl. A=`echo -n "find" "$@" "-not -path '*/"; echo -n $IGNORE | sed -e "s/:/*' -not -path '*/g"; echo "'"` # $A perl -e "system(q[$A]);"; } # CVS should ignore Vim swap files. # TODO doesn't seem to work -- use $HOME/.cvsignore #export CVSIGNORE="$CVSIGNORE:.*.swp" function up() { # Do in a subshell. This way we can do the "real" cd in one shot, which allows me to use 'cd -' to return to where I was. cd `for I in \`seq 1 $1\`; do cd ..; done; pwd` # Also works: # cd `perl -e "print '../' x $1;"` } # Prints a horizontal line (bold and red in color terminals). function hr() { if $TERM_IS_COLOR; then echo -ne "\033[01;31m" fi for I in `seq 1 $COLUMNS`; do echo -n '-' done if $TERM_IS_COLOR; then echo -ne "\033[00m" fi echo } if SSHLOC=`which ssh 2> /dev/null`; then export RSYNC_RSH="$SSHLOC" fi