Skip to content

Instantly share code, notes, and snippets.

@JakubTesarek
Last active October 27, 2021 19:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JakubTesarek/8840983 to your computer and use it in GitHub Desktop.
Save JakubTesarek/8840983 to your computer and use it in GitHub Desktop.
.bashrc file with useful shortcuts. The file can update itself using `updaterc`. Also contains detection of platform it's running on
# if not running interactively, don't do anything
[ -z "$PS1" ] && return
# Detect platform
platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
platform='linux'
elif [[ "$unamestr" == 'FreeBSD' ]]; then
platform='freebsd'
fi
# load global configuration
if [ -f /etc/bashrc ]; then
source /etc/bashrc
fi
# Easy extract
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar 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
}
# Creates directory then moves into it
function mkcdr {
mkdir -p -v $1
cd $1
}
# Creates an archive from given directory
mktar() { tar cvf "${1%%/}.tar" "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
# Listing
if [[ $platform == 'linux' ]]; then
alias ls='ls --color=auto'
elif [[ $platform == 'freebsd' ]]; then
alias ls='ls -G'
fi
alias ll='ls -lA'
alias la='ls -A'
alias tree='tree -Cs'
# Files and directories
alias cp='cp -i'
alias mv='mv -i'
alias mkdir='mkdir -p -v'
# Navigation
alias back='cd $OLDPWD'
alias home='cd ~/'
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."
# Editor
alias nano='nano -W -m'
alias edit='nano'
# .bashrc
alias reloadrc='source ~/.bashrc'
alias updaterc='mv ~/.bashrc ~/.bashrc! -f; wget -P ~/ https://gist.github.com/JakubTesarek/8840983/raw/.bashrc --no-check-certificate; source ~/.bashrc'
# load user configuration
if [ -f ~/.bashrcu ]; then
source ~/.bashrcu
fi
@amirbakhtiari
Copy link

how use this code?
add this code to .bashrc ????

@JakubTesarek
Copy link
Author

Exactly. You can either copy this code to .bashrc into your home folder or just copy the last command:

mv ~/.bashrc ~/.bashrc! -f; wget -P ~/ https://gist.github.com/JakubTesarek/8840983/raw/.bashrc --no-check-certificate; source ~/.bashrc

to your terminal and it will automaticly install latest version for you

@Elijen
Copy link

Elijen commented Feb 6, 2014

Love the extract function! 👍

@stekycz
Copy link

stekycz commented Feb 6, 2014

It looks that ls --color=auto cannot be used on Mac :-( There is some difference to setup listing colors. There must be some environment variable used for this purpose. Could it be detected somehow and based on the result create or not the alias?

@JakubTesarek
Copy link
Author

stekycz - I think it can. bashr is just simple bash file so you can write any valid bash script in there.

You can probably use this (not tested on OsX)

platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
   platform='linux'
elif [[ "$unamestr" == 'FreeBSD' ]]; then
   platform='freebsd'
fi

if [[ $platform == 'linux' ]]; then
   alias ls='ls --color=auto'
elif [[ $platform == 'freebsd' ]]; then
   alias ls='ls -G'
fi

@jakubboucek
Copy link

Is alias home='cd ~/' necessary? Just call empty command cd.

cd

Example on my server:

Burns:somefolder bukaj$ cd
Burns:~ bukaj$ 

@JakubTesarek
Copy link
Author

@stekycz I've added platform detection to the file. You should be able use the colored ls now. I'll be glad if you let me know if you let me know when you found any other diferences between linux and Mac so I can improve the script.

@JakubTesarek
Copy link
Author

jakubboucek - thanks, I know. The reason for this is that I just like writing home to get home. Some gameservers like minecraft etc. use \home to teleport player to home location and I'm used to it. Also I have similiar shortcuts to music, downloads etc. so I have home so its consistent.

@brablc
Copy link

brablc commented Feb 6, 2014

Nice, I will share some of my tips:

  • Check https://github.com/clvv/fasd if you want to speed up directory navigation.
  • Mac's uname returns Darwin. You can use cd - instead of cd $OLDPWD.
  • May be it is enough to have one cd .. alias and chain them ..;..;...

Something I have learnt only recently (regrettably) - sharing prompt history among multiple terminals:

PROMPT_COMMAND="history -a; history -n"
shopt -s histappend

You may want to force editor to be used in more applications:

export EDITOR=nano
export VISUAL=$EDITOR

Well it should actually read vim and not nano ;-) There is even more fun hidden in .vimrc - https://github.com/brablc/dotvim . You are still young and can become vim hero (unlike me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment