From owner-freebsd-questions@FreeBSD.ORG Fri Jun 19 19:07:57 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 97FCE106571C for ; Fri, 19 Jun 2009 19:07:57 +0000 (UTC) (envelope-from vogelke@hcst.com) Received: from beta.hcst.com (beta.hcst.com [192.52.183.241]) by mx1.freebsd.org (Postfix) with ESMTP id 556378FC1A for ; Fri, 19 Jun 2009 19:07:56 +0000 (UTC) (envelope-from vogelke@hcst.com) Received: from beta.hcst.com (localhost [127.0.0.1]) by beta.hcst.com (8.13.8/8.13.8/Debian-3) with ESMTP id n5JJ7ueb022146 for ; Fri, 19 Jun 2009 15:07:56 -0400 Received: (from vogelke@localhost) by beta.hcst.com (8.13.8/8.13.8/Submit) id n5JJ7u8Z022145; Fri, 19 Jun 2009 15:07:56 -0400 Received: by kev.msw.wpafb.af.mil (Postfix, from userid 32768) id 2F839BEE7; Fri, 19 Jun 2009 15:05:55 -0400 (EDT) To: freebsd-questions@freebsd.org In-reply-to: <4A3994BC.1000600@ibctech.ca> (message from Steve Bertrand on Wed, 17 Jun 2009 21:13:32 -0400) Organization: Oasis Systems Inc. X-Disclaimer: I don't speak for the USAF or Oasis. X-GPG-ID: 1024D/711752A0 2006-06-27 Karl Vogel X-GPG-Fingerprint: 56EB 6DBF 4224 C953 F417 CC99 4C7C 7D46 7117 52A0 Message-Id: <20090619190555.2F839BEE7@kev.msw.wpafb.af.mil> Date: Fri, 19 Jun 2009 15:05:55 -0400 (EDT) From: vogelke+unix@pobox.com (Karl Vogel) Subject: Re: Changing my login directory X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: vogelke+unix@pobox.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Jun 2009 19:07:58 -0000 >> On Wed, 17 Jun 2009 21:13:32 -0400, >> Steve Bertrand said: S> I've got a couple of jails now that I use exclusively for Perl S> development. As soon as I log into the box via SSH, my first command is S> _always_ "cd devel/something". I'd like to make it so that '~' remains S> /home/steve, but when I log in, I would prefer to be dropped immediately S> into /home/steve/devel. This reminded me of how I deal with lots of directories. If you have a project with files spread all over the place, here's a way to navigate between 20-25 directories with no more than 3-4 keystrokes. First, install the "grabchars" program: http://examples.oreilly.com/upt3/split/grabchars/grabchars/ It's very useful for capturing and validating keystrokes from within a shell script. For example: ans=`grabchars -q'Answer y or n: '` will print "Answer y or n: ", and it will store your reply after pressing just one key, no need to hit return. Combine this with a function to change your current directory and you're in business; it has to be done in a shell function rather than a separate script because it modifies your current shell working directory. Second, create a file holding the directories you use the most: me% cat $HOME/.cdlist 0 /home/vogelke 1 /home/vogelke/today 2 /home/vogelke/notebook/2007/0414/new-homepage ... 8 /doc/html/htdocs/blog/posts 9 /doc/sitelog/server1 a /doc/sitelog/server2 b /doc/sitelog/server3 ... o /home/vogelke/src The first field is any digit, and the lowercase letters a-o. You can use more letters, but I've found 25 choices to be more than sufficient. The second field is the full path to the directory. Finally, make a function using "grabchars" that will display this list, prompt for a single character, and immediately change to that directory. I use "jd" for the function name, but if you're not using the letter 'j' for any commands, you can shorten this to three keystrokes. Here's the function setup for the Korn and bash shells: # Jump to a directory. jd () { clear cat $HOME/.cdlist local prompt="dir: " local ans=`grabchars -d0 -L -c '[0-9a-z]' -q"$prompt"` set X `grep "^$ans" $HOME/.cdlist` case "$#" in (3) cd $3; echo; echo "pwd: `/bin/pwd`" ;; (*) echo no such entry ;; esac } # http://stackoverflow.com/questions/794951/coloring-directory-name-in-ksh # Dump the PS1 stuff if you don't want to change your prompt. chdir () { command cd "$@" CWDH=${PWD%/*} /bin/pwd >> $HOME/.cdlist.new PS1="${CWDH##*/}/${PWD##*/} ->" export PS1 } alias cd=chdir Here's the function setup for the Z-shell: # Jump to a directory. jd () { clear cat $HOME/.cdlist local prompt="dir: " local ans=`grabchars -d0 -L -c '[0-9a-z]' -q"$prompt"` set X `grep "^$ans" $HOME/.cdlist` case "$#" in (3) chdir $3 && echo && echo "pwd: `/bin/pwd`" ;; (*) echo no such entry ;; esac } # This function is run after you change directories. chpwd () { /bin/pwd >> ~/.cdlist.new } To find the most often-used directories, the "chdir" and "chpwd" functions above will store the name of every directory you cd to in "$HOME/.cdlist.new". To find your most popular directories: me% sort ~/.cdlist.new | uniq -c | sort -n | tail -- Karl Vogel I don't speak for the USAF or my company I don't want to achieve immortality through my work; I want to achieve immortality through not dying. --Woody Allen