Bash aliases and functions

Aliases are a great thing to put into your ~/.bashrc or ~/.bash_aliases file to speed up your use of the command line and functions are even better. So I'm going to share a few that I've collected and created.

alias x='startx' # I launch my desktop from the command line
alias reboot='sudo shutdown -r now' # ...and reboot from there too
alias shutdown='sudo shutdown -h now' # ...and shutdown
alias nano='nano -Siw' # smooth scroll, auto-indent, no text wrap
alias df='df -h' # uses abbreviated size formats rather than bits
alias la='ls -a' # list even hidden files
alias ll='ls -l' # displays additional file info
alias ls='ls –color=auto' # color list output
eval `dircolors -b` # use more colors
alias grep='grep –color=auto' # color grep output


Here's some commands to add to your ~/.bashrc to make man pages easier to read by using colors. You can edit them to your liking or try and find some online, these use the Arch colors.

# Color manpages (Arch style)
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;38;5;74m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[38;5;246m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[04;38;5;146m'


This next section I think is pretty unique. I was a big fan of Gnome-Do when I was using Ubuntu. When I switched to Arch I wanted to install only the bare minimum unless I needed the extra functionality. I looked at all the requirements for Gnome-Do and wanted something less bulky, especially all those mono libraries. Then I looked at other application launchers. Krunner (all those KDE dependancies, no thanks) and Launchy. Launchy would have been fine but it lacked the functionality of Gnome-Do and for some reason it wanted to open everything with Firefox (maybe because I'm using LXDE instead of GNOME? Another minimalist install choice). So I looked at the command line for solutions. I use Tilda, a neat little drop-down terminal, with very little dependancies. A shortcut pops it up for quick command line work and puts it away when I don't need it, very handy. The next set of shortcuts gives the command line and especially tilda, quick app-launching capabilities. The ampersand (&) character at the end forks the command to the background allowing me to continue using Tilda as a terminal.

alias pcm='pcmanfm' # launches PcmanFM (already backgrounds)
alias ff='firefox &'
alias calc='galculator &'
alias owrit='soffice -writer' # OO Writer (appending a file path opens it)
alias ocalc='soffice -calc' # OO Calculator (appending a file path opens it)
alias reconk='killall conky && conky -d' # very handy for conky tweaking


Now for some Arch specific shortcuts for the Pacman package manager. You can find versions of these for most distributions online. The alias names are pretty self-explanatory.

alias upgrade='sudo yaourt -Syu --aur --devel'
alias deorphan='sudo pacman -Rs $(pacman -Qtdq)'
alias optimize='sudo pacman -Sc && sudo pacman -Syy && sudo pacman-optimize && sudo sync'
alias maintain='upgrade && deorphan && optimize'


If you find yourself constantly editing config files you might want to add shortcuts to them.

alias xinitrc='nano ~/.xinitrc'
alias bashrc='nano ~/.bashrc'
alias aliases='nano ~/.bash_aliases'
alias xorgconf='sudo nano /etc/X11/xorg.conf'
alias grubmenu='sudo nano /boot/grub/menu.lst'
alias conkyrc='leafpad ~/.conkyrc &'


Here's how to launch firefox to a specific site. I set up several of my frequently visited bookmarks (gmail, greader, facebook, wikipedia...) this way.

alias [nickname]='firefox “[address]” &'

Here's a function I found that easily extracts a bunch of different compressed file formats.

# syntaxt: extract [file]
extract () {
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 "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}


Now for the best part, I'm going to show you how to use a bash function to launch a google search!

# Function to search google
function g() {
firefox "http://www.google.com/search?&num=100&q=${@}" &
}


The syntax for creating your own searches is pretty easy. The name of the function comes right before the brackets, it's also what you're going to use to start the command, my keyword for google searches here is “g”. Firefox or whatever you're browser is comes next with the web site of the search in quotation marks. To find this address go to the search engine, enter something and look at the web address. Replace your particular search terms with the symbols ${@}. These symbols represent all the words after the name of the command.

Update June 29, 2009: Launching programs from the command line has a default behaviour of letting the app print messages to your terminal. Something you really don't want when you also want to use the terminal for other things. Nixcraft has a handy tutorial on how to redirect the stdout of a program so that it behaviour is essential quiet. Very useful with programs like Firefox which don't have a -quiet command line option and can print a lot of messages.

Update December 18, 2009: Here's a handy alias to vacuum your firefox sqlite database and keep it snappy:
alias fasterfox='for f in ~/.mozilla/firefox-3.5/*/*.sqlite; do sqlite3 $f 'VACUUM;'; done'

0 comments:

Post a Comment