Alternative for Quick Cli Filesystem Navagation Shortcutting

I saw this great blog post about an easy way to create and remove shortcuts for commandline navagation to often used directories. In the past I would often manually make bash aliases for commonly used directories, so I was psyched to automate this. I much prefer just typing the DirName instead of the jump DirName used in Jeroen’s version, so I modified his commands with my own. Here are the commands I came up with.

1
2
3
4
5
6
7
8
9
function mark {
  echo "alias $1='cd $(pwd)'" >> ~/Dropbox/Config/bash_aliases ; source ~/Dropbox/Config/bash_aliases
}
function unmark {
  sed -e "/^alias $1='cd/d" -i ~/Dropbox/Config/bash_aliases ; source ~/Dropbox/Config/bash_aliases
}
function marks {
  cat ~/Dropbox/Config/bash_aliases | grep "='cd" | sed -e 's/alias //' -e "s/='cd / \=\> /" -e "s/'$//" -e "/^  echo /d" -e "/&&/d" -e "/^  sed/d"
}

This may not be the most elegant way of doing things (that sed command is fairly heinous) but it works great.

BTW keeping my bash_aliases file inside of dropbox is amazing…aliases and functions instantly appear on all my machines. It’s rad.

P.S. originally I had the unmark command looking like this

1
2
3
4
}
function unmark {
  sed -e "/^alias $1/d" -i ~/.bash_aliases ; source ~/.bash_aliases
}

which seems like a only minor modification from the current version. This seemingly minor change of alias $1 –> alias $1='cd looks like nothing, but one time I accidentally typed

1
$mark

which created the line alias ='cd ~/WhereverIWasWhenIDidThis' in my ~/Dropbox/Config/bash_aliases realising I just made a empty alias I immediatly tried removing the alias with

1
$unmark

which proceded to delete all aliases in my ~/Dropbox/Config/bash_aliases!!! That was interesting, thank goodness for dropbox backing things up (another reason to keep config files in dropbox). Let me know what you think in the comments or hit me up on twitter.

Comments

Comments