[lug] CDPATH?

Chris Riddoch riddochc at gmail.com
Thu Apr 14 12:44:51 MDT 2016


Here's a clever thing I found recently, for the zsh users.  Credit
goes to http://chneukirchen.org/dotfiles/.zshrc
(There's a lot of useful things there besides this.)

Here's how to use it. Let's say you've been navigating the directory
hierarchy of the source code for some java project, and you've reached
the point where typing '../' even one more time will make you want to
defenestrate something.  This 'up' function can help.  Like so:

tmp $ mkdir -p a/b/c/d/e/f/g
~/tmp
tmp $ cd a/b/c/d/e/f/g
~/tmp/a/b/c/d/e/f/g
g $ up 4
~/tmp/a/b/c
c $ up a
~/tmp/a
a $ up 1
~/tmp

And the code:

# up [|N|pat] -- go up 1, N or until basename matches pat many directories
#   just output directory when not used interactively, e.g. in backticks
up() {
  local op=print
  [[ -t 1 ]] && op=cd
  case "$1" in
    '') up 1;;
    -*|+*) $op ~$1;;
    <->) $op $(printf '../%.0s' {1..$1});;
    *) local -a seg; seg=(${(s:/:)PWD%/*})
       local n=${(j:/:)seg[1,(I)$1*]}
       if [[ -n $n ]]; then
         $op /$n
       else
         print -u2 up: could not find prefix $1 in $PWD
         return 1
       fi
  esac
}

On Thu, Apr 14, 2016 at 10:48 AM, Tyler Cipriani
<tyler at tylercipriani.com> wrote:
> Here's a useful thing that I've setup recently, it's a variation on
> Jeroen Janssens's method[0] of quick filesystem naviagation:
>
> In your ~/.bashrc (or other shell initialization file)
>
> 1. Create a mark function:
>
>     mark() { mkdir -p "$HOME/.marks"; ln -s "$(pwd)" "$HOME/.marks/$1" }
>
> 2. Alias cd to cd -P:
>
>     cd='cd -P'
>
> 3. Make sure "$HOME/.marks" is in your CDPATH variable:
>
>     export CDPATH="$HOME/.marks"
>
> To use this system, simply use the mark function when you're in a
> directory to create an alias for that directory that you can cd to
> from anywhere in the filesystem:
>
>     (ノ^ヮ^)ノ*:・゚✧ pwd
>     /home/tyler
>     (ノ^ヮ^)ノ*:・゚✧ mkdir -p tmp/some/really/deep/file/structure/omg/so/long
>     (ノ^ヮ^)ノ*:・゚✧ cd !$
>     cd tmp/some/really/deep/file/structure/omg/so/long
>     (ノ^ヮ^)ノ*:・゚✧ mark deep
>     (ノ^ヮ^)ノ*:・゚✧ cd
>     (ノ^ヮ^)ノ*:・゚✧ pwd
>     /home/tyler
>     (ノ^ヮ^)ノ*:・゚✧ cd deep
>     /home/tyler/.marks/deep
>     (ノ^ヮ^)ノ*:・゚✧ pwd
>     /home/tyler/tmp/some/really/deep/file/structure/omg/so/long
>
> Tada!
>
> Tyler
>
> 0. http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
>
>
> On 16-04-14 06:26:34, Jeffrey S. Haemer wrote:
>> Jed,
>>
>> Another helpful tweak for cd is "shopt -s globstar"
>>
>> shopt -s globstar
>> mkdir -p a/b/c/d/e
>> cd **/e
>> pwd
>>
>>
>> It's no DWIM, but sometimes it's a good shortcut. I'm lazy and a bad
>> typist, so my .bashrc includes this:
>>
>> shopt -s cdspell dirspell globstar
>>
>> Also this:
>>
>> alias mroe=more  # for Evi
>>
>> Perhaps, however, tonight's emacs talk will include ways to use DWIM
>> commands to turn emacs into the shell-of-your-dreams. :-)
>>
>> On Wed, Apr 13, 2016 at 6:38 PM, Jed S. Baer <blug at jbaer.cotse.net> wrote:
>>
>> > On Wed, 13 Apr 2016 17:59:47 -0600
>> > Zan Lynx wrote:
>> >
>> > > Notice that "warez" is not in your CDPATH. To make "cd warez" work you'd
>> > > need "CDPATH=/home/jbaer/incoming"
>> >
>> > Aha. Somehow, I had it in my head it'd be recursive. Funny, how the brain
>> > works sometimes. Thanks.
>> > _______________________________________________
>> > Web Page:  http://lug.boulder.co.us
>> > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> > Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety
>> >
>>
>>
>>
>> --
>> Jeffrey Haemer <jeffrey.haemer at gmail.com>
>> 720-837-8908 [cell], http://seejeffrun.blogspot.com [blog],
>> http://www.youtube.com/user/goyishekop [vlog]
>> *פרייהייט? דאס איז יאַנג דינען וואָרט.*
>
>> _______________________________________________
>> Web Page:  http://lug.boulder.co.us
>> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety
>
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety



-- 
Chris Riddoch
http://www.syntacticsugar.org/


More information about the LUG mailing list