views:

32

answers:

5

Looking for a solution to quickly navigate to long paths in a shell (particularly Max OS X Terminal.app).

Say my path is ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently

Instead of cd ~/This/Is/A/....

I would like to be able to store favorites/bookmark directories so I could do "cd myPath"

Are there any binaries or tools available to do something like this?

A: 

Why not having a symlink ?

ln -s ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently bmark
cd bmark
Nicolas Viennot
Good idea, but I'd rather not bog down my file system with even more entries. I already have a very structured file system, I just need to navigate it via command line easier/quicker.
Jonathan Dumaine
A: 

You can use aliases (stick them in your ~/.bash_profile if you want them to always load)

alias cd_bmark1='cd ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently'

Then use by just typing

cd_bmark1

into the console

Jamie Wong
which will not work if you use anything else than bash (think about GUI with open/save dialogs)
Nicolas Viennot
@Pafy OP said: 'Instead of cd ~/This/Is/A/....' I figured he was referring to console
Jamie Wong
Sure, but if you "sudo su" it won't work anymore :)
Nicolas Viennot
This would 'work', but it's not a very elegant solution. Also, Pafy has a point about the su shell.
Jonathan Dumaine
+1  A: 

What you probably should look at using is go-tool. Must have for any time of shell environment. Works on both Unix and Windows.

fuzzy lollipop
Yes, this is indeed what I am looking for. It's not as feature rich as Apparix, however.
Jonathan Dumaine
A: 

Alas, I've found the packages 'Apparix' and 'Goto' which together make the stuff dreams are made of for us terminal junkies.

here: http://www.micans.org/apparix/ and here: http://sitaramc.googlepages.com/goto-considered-useful.html

Naturally, I had trouble installing Apparix, but I figured it out in the end.

How To Install Apparix on Mac OS X:

Steps:

1) Download the tarball from Apparix's homepage
2) Unpack the tarball, cd to the unpacked folder
3) Run this command ./configure --prefix=$HOME/local && make && make install
4) Now copy and paste the following into your ~/.bash_profile

################################
#
#
#           Apparix
#
#
################################

export PATH=${PATH}:$HOME/local/bin


function to () {
  if test "$2"; then
    cd "$(apparix --try-current-first -favour rOl "$1" "$2" || echo .)"
  elif test "$1"; then
    if test "$1" == '-'; then
      cd -
    else
      cd "$(apparix --try-current-first -favour rOl "$1" || echo .)"
    fi
  else
    cd $HOME
  fi
}

#optinally, "bm" can be changed to whatever you like, "bookmark" for instance.
function bm () {
  if test "$2"; then
    apparix --add-mark "$1" "$2";
  elif test "$1"; then
    apparix --add-mark "$1";
  else
    apparix --add-mark;
  fi
}
function portal () {
  if test "$1"; then
    apparix --add-portal "$1";
  else
    apparix --add-portal;
  fi
}
# function to generate list of completions from .apparixrc
function _apparix_aliases ()
{ cur=$2
  dir=$3
  COMPREPLY=()
  nullglobsa=$(shopt -p nullglob)
  shopt -s nullglob
  if let $(($COMP_CWORD == 1)); then
    # now cur=<apparix mark> (completing on this) and dir='to'
    # Below will not complete on subdirectories. swap if so desired.
    # COMPREPLY=( $( cat $HOME/.apparix{rc,expand} | grep "j,.*$cur.*," | cut -f2 -d, ) )
    COMPREPLY=( $( (cat $HOME/.apparix{rc,expand} | grep "\<j," | cut -f2 -d, ; ls -1p | grep '/$' | tr -d /) | grep "\<$cur.*" ) )
  else
    # now dir=<apparix mark> and cur=<subdirectory-of-mark> (completing on this)
    dir=`apparix --try-current-first -favour rOl $dir 2>/dev/null` || return 0
    eval_compreply="COMPREPLY=( $(
      cd "$dir"
      \ls -d $cur* | while read r
      do
        [[ -d "$r" ]] &&
        [[ $r == *$cur* ]] &&
          echo \"${r// /\\ }\"
      done
    ) )"
  eval $eval_compreply
  fi
  $nullglobsa
  return 0
}
# command to register the above to expand when the 'to' command's args are
# being expanded
complete -F _apparix_aliases to


################################
#
#
#         End Apparix
#
#
################################

5) That's it. You should now have Apparix up and running on OS X (further install info and usage is on Apparix's homepage).

Jonathan Dumaine
+1  A: 

I know you already found an answer that worked for you, but a couple of more lightweight suggestions that might help others looking for similar things

  • If your directories are relatively fixed, just long and far away from each other, you can use the CDPATH environment variable to add directories to the search path when typing the "cd" command. If the directory name you try to cd to isn't in the current directory, the other entries in your CD path will also be looked at (and it's also tab complete aware, at least in bash and zsh).

  • Switching to zsh rather than bash and using the excellent directory stacks abilities. With it, you can maintain a history of directories that you've visited, view the history with the "dh" alias, and easily switch to a directory by using quick shortcuts (ex: cd -3 to switch to the 3rd directory in your history stack).

Ted Naleid