Page 1 of 1

Show us you .bashrc !

PostPosted: Sep 9th, '12, 16:16
by doktor5000
I'm posting this here as it was proposed by one of our german Mageia users, that some of my aliases and functions
might be useful to others, and to start a collection-type thread where others could also post their .bashrc or snippets of it with explanations
so everyone could grab a bit here and there.

For those that don't know, .bashrc is a hidden settings file, which gets sourced (executed) everytime you open
a terminal (for a user which has it's shell set to bash, which is the default in Mageia) so you have all your custom
settings, nice little shortcuts, color and prompt definitions and such, in additions to the systemwide one in /etc/bashrc.

For some explanations about bash, aliases, functions and other things discussed here, you may want to have a look at the following links beforehand:
The Linux Documentation Project - Bash Guide for Beginners
Advanced Bash-Scripting Guide - Aliases
Advanced Bash-Scripting Guide - Functions
Bash Guide for Beginners - What are aliases?

And FWIW, you can show the currently enabled aliases via alias command and you can also temporarily unset one alias via the unalias command, analogue to the handling of environment variables.

This is mine (i don't have more stuff in there as normally i don't connect to other boxes from my workstation, and don't need no advanced/intelligent prompt definitions, colourprompt and the default Mageia prompt definition is just fine for me)

Code: Select all
[doktor5000@Mageia2 ~]$ cat .bashrc
# .bashrc

# User specific aliases and functions
function ufn() {
  urpmf -f -m --name "$@" | sort
}

function rg() {
  rpm -qa | grep -i "$@" | sort
}


alias uq='urpmq -a'
alias ri='rpm -qi'
alias rq='rpm -q --requires'
alias rp='rpm -q --provides'
alias ua='sudo urpmi.update -av'
alias ub='sudo urpmi --buildrequires'
alias up='sudo urpmi'
alias mvn='mv ~/rpm/RPMS/noarch/*.rpm ~/rpm/RPMS/x86_64'
alias ra='rpm --addsign ~/rpm/RPMS/x86_64/*.rpm && rpm --addsign ~/rpm/SRPMS/*.rpm'
alias rb='rpmbuild -ba --clean'
alias re='rpmbuild --rebuild'
alias rba='rpmbuild -ba --clean --sign'
alias rbi='rpmbuild -bi --short-circuit'
alias rbp='rpmbuild -bp'
alias rbc='rpmbuild -bc --short-circuit'
alias dv='desktop-file-validate'
alias sudo='sudo -E'
alias be='sudo urpmi.update --no-ignore backports && sudo urpmi.update -av'
alias bd='sudo urpmi.update --ignore backports'
alias te='sudo urpmi.update --no-ignore testing && sudo urpmi.update -av'
alias td='sudo urpmi.update --ignore testing'
alias de='sudo urpmi.update --no-ignore debug && sudo urpmi.update -av'
alias dbgd='sudo urpmi.update --ignore debug'
alias scc='sudo LC_ALL=C mcc'
alias mdb='mgarepo maintdb get'



#SVN stuff
export EDITOR=kwrite

#PHONON_PULSEAUDIO_DEBUG=3
#KMIX_PULSEAUDIO_DEBUG=3
#export PHONON_VLC_DEBUG=3
#export PHONON_DEBUG=1
HISTSIZE=100000

shopt -s histappend
  PROMPT_COMMAND='history -a'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


Some explanations for the more interesting ones:

Code: Select all
function ufn() {
  urpmf -f -m --name "$@" | sort
}

Previously i also had this as an alias, but as i wanted the results to be sorted, this is not possible with aliases AFAIK, so now this is a functions.
Searches all currently enabled repositories for a given package name or part of a name, and shows the full name and version information, together with the media information (in which repository a package is located) - the results are alphabetically sorted --> this is quite useful, and i use it extensively

Code: Select all
function rg() {
  rpm -qa | grep -i "$@" | sort
}

Searches in the names of all installed packages for a given string (name, part of a name, version) and ignores case sensitivity, displays the results alphabetically sorted --> this is quite useful, and i use it extensively

Code: Select all
alias up='sudo urpmi'

Due to my sudo setup --> this is quite useful, and i use it extensively

Code: Select all
alias sudo='sudo -E'

This one is interesting, since quite some time some of the drakxtools only open the ncurses-based version when launched from a terminal, this was probably caused by some change of the default sudo setup or the upstream sudo configuration. -E preserves environment variables of the current user for the sudo environment.

Code: Select all
alias be='sudo urpmi.update --no-ignore backports && sudo urpmi.update -av'
alias bd='sudo urpmi.update --ignore backports'
alias te='sudo urpmi.update --no-ignore testing && sudo urpmi.update -av'
alias td='sudo urpmi.update --ignore testing'
alias de='sudo urpmi.update --no-ignore debug && sudo urpmi.update -av'
alias dbgd='sudo urpmi.update --ignore debug'


Those can be quite handy. Those that end with an "e" enable the according repositories, and update the hdlists, those that end with a "d" disable the according repositories. Quite useful if you need to handle testing, backports or debug media and makes it easier f.ex. to only selectively install something from repositories, which was best-practice when dealing with backports repositories under Mandriva.

Code: Select all
alias scc='sudo LC_ALL=C mcc'

Only saves some repetitive typing, this one enables me to easily launch Mageia Control Center in english, as i need the names of the menu points and stuff like that in english for support reasons. It is also best practice to prefix any command by LC_ALL=C if it gives non-english output


Code: Select all
rpm -qa --qf "%{SIZE}\t%{NAME}-%{VERSION}\n" | sort -rn

This one is not in my bashrc, as i actually never really need it, but it may be quite useful to some. It queries all installed packages, and lists them by their size, in descending order.

Code: Select all
HISTSIZE=100000

shopt -s histappend
  PROMPT_COMMAND='history -a'

Those belong together, and they are golden for me. It raises the size of the shell history, and the two latter definitions set up the history in such a way that the history from every open terminal session gets appended live to the shell history, and does not get overwritten. Really handy if you use the history command or Ctrl+R (recursive history search in bash) to search for commands thatyou run already some time ago.



More bashrc magic:

What useful things can one add to one's .bashrc?
Show us your .bashrc! - thread in ubuntuforums.org
shell-fu.org bashrc
The Ultimate Bashrc File at gnome-look.org

Re: Show us you .bashrc !

PostPosted: Sep 9th, '12, 16:22
by doktor5000
Ahh, and please don't forget: If you have specific questions on the rpm/urpm queries, please open a separate thread, as this one is specifically about bashrc and the more shell-specific stuff, and not so much about the underlying commands.

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 19:46
by MauRice
Mine:

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

uname -r

#WAN & LAN IP settings
myip ()
{
INTERFACE=`cat /proc/net/arp | grep -m 1 -v Device | awk '{print $6}'`

/usr/bin/wget -np -c http://www.watismijnip.be --output-document /tmp/index.html."$$" 2>/dev/null && export PRPID="$$"
WAN_IP=`grep -i 'IP adres :' /tmp/index.html."$PRPID" | cut -d':' -f2 | tr -d '[:blank:]' | cut -d'.' -f1-4`
LAN_IP=`/sbin/ifconfig $INTERFACE | grep 'inet addr:' | cut -d: -f2 | cut -d' ' -f1 | tr -d '[:blank:]'`
export {WAN,LAN}_IP && echo -en "\nWAN IP: $WAN_IP\n" && echo -en "LAN IP: $LAN_IP\n\n" && /bin/rm -f /tmp/index.html."$PRPID"
}

if ping -c 1 -w 1 http://www.google.be &>/dev/null; then
myip;
else
echo -en "\nThere is currently no\nConnection to the internet\nPlease check your settings! \n\n";
fi

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 20:51
by tom_
I like coloured prompts :

this for the users
Code: Select all
export PS1='\[\e[0;34m\]\u@\h \[\e[0;32m\]\W]\$\[\e[0m\] '


and red for root

Code: Select all
export PS1='\[\e[0;31m\]\u@\h \W]\$\[\e[0m\] '

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 21:09
by MauRice
Tom,

Me too, I install colorprompt

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 21:11
by doktor5000
tom_ wrote:I like coloured prompts :
[...]

You can have that way easier, just install colorpromt package
[doktor5000@Mageia2 ~]$ rpm -qi colorprompt-0.1-3.mga1
Name : colorprompt
[...]
Summary : Make the user prompt in bash different colors depending on your user
Description :
A little bash profile extension to color your user prompt appropriately.
It will make regular logins green and root logins red.

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 22:33
by ah7013
Am we allowed to post other shell configs? If so I'll post my .zshrc when I get home. My .bashrc is just the default one because I don't use bash as my primary shell any more.

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 22:36
by doktor5000
If there's interesting stuff in it, just post it with a few comments :D

Re: Show us you .bashrc !

PostPosted: Sep 11th, '12, 22:52
by ah7013
Cool, will have to do that when I get home tonight because I'm on a Windows 7 PC right now and have no access to my .zshrc :D . Some of the things in it may be zsh specific though (I have some zsh autoload functions in it which may not work in bash but i'll test that tonight).

Re: Show us you .bashrc !

PostPosted: Sep 12th, '12, 09:33
by ah7013
Here's my .zshrc
Code: Select all
HISTFILE=~/.zsh_history
HISTSIZE=2000
SAVEHIST=2000
DIRSTACKSIZE=50

autoload -Uz compinit
compinit
autoload -Uz promptinit
promptinit
prompt adam1

setopt correctall hist_ignore_space no_beep noclobber mark_dirs share_history append_history multios interactivecomments autopushd pushdsilent pushdignoredups always_to_end prompt_subst autocd extendedglob

zstyle ':completion:*:warnings' format 'Sorry - no matches'
zstyle ':completion:*' menu select
zstyle ':completion:*:commands' rehash 1
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric
zstyle -e ':completion::*:*:*:hosts' hosts 'reply=(${=${${(f)"$(cat ~/.ssh/known_hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })'

alias rmf='rm -rf'
alias pc='perl -c'
alias p_c='perl_checker'
export EDITOR='vim'
export BROWSER='firefox'

bindkey -v


And some explanations of the interesting parts:
Code: Select all
autoload -Uz promptinit
promptinit
prompt adam1

This is an inbuilt zsh feature that sets a custom prompt. 'adam1' is my favourite out of the inbuilt themes but there are other inbuilt ones as well. Namely:
Code: Select all
andrew@localhost ~ % prompt -l
Currently available prompt themes:
adam1 adam2 bart bigfade clint elite2 elite fade fire off oliver pws redhat suse walters zefram


then:
Code: Select all
setopt correctall hist_ignore_space no_beep noclobber mark_dirs share_history append_history multios interactivecomments autopushd pushdsilent pushdignoredups always_to_end prompt_subst autocd extendedglob

sets a lot of zsh options. I'll just go over the more interesting ones:
correctall: offers spelling correction e.g:
Code: Select all
andrew@localhost ~ % ls /hmoe
zsh: correct '/hmoe' to '/home' [nyae]? y
andrew/  lost+found/

autocd: allows me to change directory without having to prepend 'cd'. e.g:
Code: Select all
andrew@localhost ~ % /etc
andrew@localhost /etc % skel
andrew@localhost /etc/skel %

extendedglob: allows to do things like recursive globbing e.g.
Code: Select all
andrew@localhost ~ % ls -l /usr/**/urpm/sys.pm
-rw-r--r-- 1 root root 9909 Mar 28 01:17 /usr/lib/perl5/vendor_perl/5.14.2/urpm/sys.pm

Lots of good information on the setopt options here:
http://zsh.sourceforge.net/Doc/Release/Options-Index.html

then:
Code: Select all
zstyle -e ':completion::*:*:*:hosts' hosts 'reply=(${=${${(f)"$(cat ~/.ssh/known_hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })'

allows you to do tab completion when using the ssh command e.g.
Code: Select all
andrew@localhost ~ % ssh andrew@vmhost[tab][tab]
vmhost1  vmhost2

it uses ~/.ssh/known_hosts to do tab completion.

and lastly:
Code: Select all
bindkey -v

I prefer vi key bindings so this makes the shell use them.

I found a lot of the stuff for my .zshrc from various places on the internet including this very useful page:
http://grml.org/zsh/zsh-lovers.html

Re: Show us you .bashrc !

PostPosted: Sep 12th, '12, 18:49
by jensm
nothing special

Code: Select all
# .bashrc
# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
extract() {
        if [[ -f "$1" ]]; then
                case $1 in
                        *.tar.bz2)  tar -jxvf $1;;
                        *.tar.gz)   tar -zxvf $1;;
                        *.bz2)      bunzip2 $1   ;;
                        *.gz)       gunzip $1   ;;
                        *.jar)      unzip $1       ;;
                        *.rar)      unrar x $1   ;;
                        *.tar)      tar -xvf $1   ;;
                        *.tbz2)     tar -jxvf $1;;
                        *.tgz)      tar -zxvf $1;;
                        *.zip)      unzip $1      ;;
                        *.Z)        uncompress $1;;
                           *)       echo "Unable to extract '$1' :: Unknown extension"
                 esac
        else
                echo "File ('$1') is not a valid file!"
        fi
}

Re: Show us you .bashrc !

PostPosted: Sep 12th, '12, 22:09
by doktor5000
tar nowadays extracts most archive types it was linked against. Apart from that, there's the unp script http://www.debianhelp.co.uk/unp.htm but unfortunately seems whis was not imported yet into Mageia ...

Re: Show us you .bashrc !

PostPosted: Sep 15th, '12, 03:58
by digigold
My alias adds to .bashrc:

Code: Select all
alias ls='ls --color=auto -a'
alias ll='ls --color=auto -lash'
alias lf='ls --color=always -lash | less -R'

Passing color=always to ls while using the -R argument with less allows for color with less.

Code: Select all
alias urpmif='urpmi --downloader=wget --allow-force'
alias uaa='urpmi --downloader=wget --allow-force --auto-update && urpme --auto-orphan && urpmi --clean'

Re: Show us you .bashrc !

PostPosted: Sep 20th, '12, 13:57
by viking60
Code: Select all
   # .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
   . /etc/bashrc
fi
# Check for an interactive session
#[ -z "$PS1" ] && return

#alias ls='ls --color=auto'
#PS1='[\u@\h \W]\$ '
bash_prompt_cmd() {
RTN=$?

if [ -f .alias ]; then
    . .alias
fi

#Her kommer smiley
smiley() {
    if [ $1 == 0 ] ; then
echo ":)"
    else
echo ":("
       fi
    }
smileyc() {
    if [ $1 == 0 ] ; then
echo $GREEN
else
echo $RED
       fi
    }
 if [ $(tput colors) -gt 0 ] ; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RST=$(tput op)
fi
smiley=$(smiley $RTN)
smileyc=$(smileyc $RTN)
        local CY="\[\e[1;31m\]" # Each is 12 chars long
        local BL="\[\e[1;34m\]"
        local WH="\[\e[1;37m\]"
        local BR="\[\e[0;33m\]"
        local RE="\[\e[1;31m\]"
        local PROMPT="${CY}$"
        [ $UID -eq "0" ] && PROMPT="${RE}#"

        # Add the first part of the prompt: username,host, and time
        local PROMPT_PWD=""
        local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
        local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 )) #Len adjust for colors, time and var
        local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
        local startpos=""

        PROMPT_PWD="${PWD/#$HOME/~}"
        local overflow_prefix="..."
        local pwdlen=${#PROMPT_PWD}
        local maxpwdlen=$(( COLUMNS - ps_len ))
        # Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
        [ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

        if [ $pwdlen -gt $maxpwdlen ] ; then
                startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
                PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
        fi     
        export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
}
PROMPT_COMMAND=bash_prompt_cmd


This is my smiley terminal: When I enter a correct comand I get a green smiley :)and when i enter a wrong one i get a red :(
ImageImage

If you add this to your .baschrc
Code: Select all
if [ -f .alias ]; then
    . .alias
fi

It will read all your aliases from the file ~/.alias (it helps to keep your .bashrc clean)

So here is the content of my .alias
Code: Select all
alias cd..='cd ..'
alias df='df -h -x supermount'
alias ls='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell'
alias dir='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell'
alias copy='cp -i'
alias move='mv -i'
alias ping='ping -c 10'
alias lsd='/bin/ls -FAv |grep /$ | column'
alias ifconfig='cdsbin; ifc; cd'
alias cdsbin='cd /sbin'
alias ifc='./ifconfig'
alias l='ls -l --color=auto'
alias uuid='ls -l /dev/disk/by-uuid'
alias ssh_mysite='ssh -p 123456 me@mysite.com'
alias ls4='ls | pr -T4 -W$COLUMNS'
alias ls5='ls | pr -T5 -W$COLUMNS'
alias ls6='ls | pr -T6 -W$COLUMNS'
alias ls7='ls | pr -T7 -W$COLUMNS'
alias ls8='ls | pr -T8 -W$COLUMNS'
alias ls9='ls | pr -T9 -W$COLUMNS'
alias ls10='ls | pr -T10 -W$COLUMNS'
alias lastinstalled="rpm -qa --last | less"

I use the guake dropdown terminal so I often like to show my listings in more columns (ls4-10)
The cdsbin,ifc and cd aliases are combined into the ifconfig alias.
You could have used the full root path instead you say? :D
Yes I could Image