Bash notes

As a regular linux user I have some hints and tips how to setup bash:
  • Create a ~/.bash_aliases file and store all your aliases and functions there. You'll need to source the file in ~/.bashrc, see below.
  • For common directories you visit create an alias that cd's to that folder starting with _. Using the underscore is short and sweet and doesn't pollute your namespace as much. Here are two examples (the latter also changes the window title):
alias _mydir='cd /home/scott/my/dir'
alias _other='cd /home/scott/src/projects/other;echo -ne "\033]2;Other Project\007"'
  • Use these cd aliases:
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
  • Use these ls aliases. (h is for "human" file sizes, F is for adding a trailing / for directories)
alias ls='ls -hF --color=auto'
alias ll='ls -lhF --color=auto'
alias la='ls -AF --color=auto'
alias lrt='ls -lrthF'
function ld {
ls -l $* | grep ^d;
}
  • Add common misspellings as you come across them:
alias gerp='grep'
alias chgpr='chgrp'
  • Put in commands that you only need once in a while and you often forget. Give it a really long name so there's no collision and just type "alias" to find them again. Here are two examples:
alias who-has-my-sound='lsof | egrep "dsp|snd"'
alias my-programs-plus-params='ps -U $USER -fwww | cut -b 49- | grep -v "ps -U"'
  • I use the newer gvim with tabs but you have to remember pass the -p parameter, ugh:
alias gvim='gvim -geometry 110x60 -p'
  • Make a directory and cd to it in one step (thanks kpd for the suggestion):
function mkd() {
mkdir $1
cd $1
}
For ~/.bashrc here are my suggestions:
# Add bash completion
BASH_COMPLETEION="${BASH_COMPLETION:-/etc/bash_completion}"
# My aliases (lots)
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

# Have a huge history
export HISTSIZE=50000
export HISTCONTROL=ignoredups
shopt -s histappend

# Turn off any buzzing or bells
setterm -blength 0

# Allow Ctrl-S to work as forward search by disabling XON/XOFF (^S/^Q)
stty -ixon
This list is useful for me (if I ever need to setup a new machine), I hope it's useful for you as well.

Comments

kpd said…
Great post! Here are some .bashrc favorites:

alias c='clear'
alias h='history'
alias 'hg=history | grep -i'

alias p=/c/Python25/python

# 'N' new directory
n()
{
mkdir $1;
cd $1;
}
Scott Kirkwood said…
I really like the n function, great idea.
I'm not keen on having really short names, since I suppose that I type pretty fast.

Popular posts from this blog

Shortest Sudoku solver in Python

Seven Segment Display in Inkscape