How to make shortcuts in git bash?

How to make shortcuts in git bash?

I find the cdable_vars bash option very convenient for fast directory access. If you enable it you can use a bash variable name as your cd destination:

shopt -s cdable_vars
fomc=~/Documents/FolderOfMyChoice

And then:

cd fomc

Of course you will have to maintain a set of bash variables for your various favorite destinations, for instance in your ~/.bashrc or a file sourced by your ~/.bashrc. You can also define bash functions in your ~/.bashrc to simplify all this. I have a save [SHORTCUT] function that, when called in a directory, defines a variable with the same name as the directory and which value is its full path. It also adds (or updates) this definition in a ~/.dirs file that I source from my ~/.bashrc. The function takes an optional argument in case I want to use another shortcut than the directory name. I also have a delete [SHORTCUT] function that unsets one of these variables and removes its definition from ~/.dirs. Example of implementation (adapt to your own needs):

$ cat ~/.bashrc
...
shopt -s cdable_vars
dirsfile=~/.dirs
if [ -f $dirsfile ]; then
  source $dirsfile
fi

mySave () {
  d=$(pwd)
  b=$(basename $d)
  s=${1:-$b}

  [ $s = / ] && return
  [ -f $dirsfile ] || touch $dirsfile
  grep -q ^$s= $dirsfile && sed -i /^$s=/d $dirsfile
  printf %s=%sn $s $d >> $dirsfile
  declare -ng tmp=$s
  tmp=$d
}

myDelete () {
  d=$(pwd)
  b=$(basename $d)
  s=${1:-$b}
  
  [ $s = / ] && return
  [ -f $dirsfile ] && sed -i /^$s=/d $dirsfile
  declare -ng tmp=$s
  unset tmp
}

And then:

[/homes/doe/jhsdc] pwd
/homes/doe/jhsdc
[/homes/doe/jhsdc] mySave
[/homes/doe/jhsdc] cd /tmp
[/tmp] mySave foo
[/tmp] cd jhsdc
[/homes/doe/jhsdc] cd foo
[/tmp] myDelete jhsdc
[/tmp] cd jhsdc
bash: cd: jhsdc: No such file or directory

How to make shortcuts in git bash?

Leave a Reply

Your email address will not be published.