Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

quick change dir by menu

by tos (Deacon)
on May 30, 2003 at 14:40 UTC ( [id://261866]=CUFP: print w/replies, xml ) Need Help??

Sometimes navigation within directory-trees is exhausting especially if the pathnames are long and typing becomes uncomfortable. qcd makes this directory changes easier through selection by menu.
#!/usr/bin/perl use warnings; use strict; use Fcntl; use SDBM_File; my $pathDats = ($ENV{QCD_DAT} || $ENV{HOME}) . "/.qcd"; #print "\$pathDats: $pathDats\n"; tie (my %DIRS, 'SDBM_File', $pathDats, O_RDWR|O_CREAT, 0666) || die $!; if ( @ARGV ) { { ($ARGV[0] eq "-i") && do { # insert a path-entry if ($ARGV[1]) { $ARGV[1] = $ENV{PWD} if ($ARGV[1] eq " +."); #$ARGV[1] =~ s/(\s)/\\$1/g; $DIRS{firstFreeIndex()} = $ARGV[1]; } print ".\n"; exit}; ($ARGV[0] eq "-d") && do { # delete a path-entry delete $DIRS{$ARGV[1]}; print ".\n"; exit}; ($ARGV[0] eq "-c") && do { # compress lists of path-en +tries compress(); print ".\n"; exit}; ($ARGV[0] =~ /[1-9]+/ ) && do { printPath($ARGV[0]); exit}; } } my $oldfh = select STDERR; $| = 1; select $oldfh; #neccessary for usage on cygwin #Holy Scripture, 3rd Edition, P.803 (my @liste = sort keys %DIRS) || do {print ".\n"; exit}; my ($pat, $found, $wahlPfad) = ($ARGV[0] || "", 0); foreach (@liste) { ($DIRS{$_} =~ /$pat/o) && do { $wahlPfad = $DIRS{$_}; print STDERR "[$_] $wahlPfad\n"; $found++; } } { ($found == 0) && do { print ".\n"; exit }; $pat && ($found == 1) && do { print "$wahlPfad\n"; exit }; } print STDERR "[#]> "; my $input = <STDIN>; chop($input); printPath($input); untie %DIRS; sub printPath { #--------------------------------------------------- my $sel = shift; if (exists $DIRS{$sel}) { print "$DIRS{$sel}\n"; } else { print ".\n"; } } sub firstFreeIndex { #---------------------------------------------- my $i = 1; foreach (sort keys %DIRS) { last if ($i != $_); $i++; } $i; } sub compress { #---------------------------------------------------- (my @liste = values %DIRS) || exit; %DIRS = (); my $i = 0; foreach (@liste) { $DIRS{++$i} = $_; } } __END__ =head1 NAME qcd (quick change directory, a variation of M.Schillis cdbm) =head1 SYNOPSIS c path-item-index | path-item-pattern c -i pathname | -d path-item-index | -c =head1 DESCRIPTION qcd is an interactive tool for cd-ing driven by menu. The desired path + can be selected by indexnumber directly or by choice. Also a pattern can be the parameter for qcd. The matched path-items wi +ll be displayed in the menu. If there remains only one a cd to this w +ill be executed immediatly. The B<implementation> is realized via perl-script qcd which is embedde +d in the shell(bash)-function c or how you will ever name this functi +on. The path-items are stored in ~/.qcd.pag if no explicit path is given v +ia Env-Var QCD_DAT. =head1 OPTIONS =over 4 =item B<-c> compress the menu by eliminating gaps which were created by former pat +h-item deletes =item B<-d> delete a path-item =item B<-i> insert/append a path-item =back =head1 PREREQUISITE C<function c () { cd `qcd $1 $2`; pwd; }> which should be done in .bas +hrc or something similar. =head1 KNOWN BUGS Pathnames which contains Blanks (a nasty Windows-property) can't be ha +ndled with qcd. I tested with singlequotes and backslashes, nothing d +id it. Perhaps someone has an helpful idea. =head1 AUTHOR tos <_tosch_@yahoo.com> =head1 EXAMPLE tos@sys-5713 /cygdrive/c/WINDOWS/SYSTEM32/DRIVERS function c () { cd `perl "d:\tsp\lab\dvlp\tos\too\qcd" $1 $2`; } tos@sys-5713 /cygdrive/c/WINDOWS/SYSTEM32/DRIVERS # c [1] /home/tos/lab/dvlp/tos/too/picSort [2] /cygdrive/c/SapWorkDir [3] /cygdrive/c/WINDOWS/SYSTEM32/DRIVERS [#]> 1 tos@sys-5713 ~/lab/dvlp/tos/too/picSort # c 3 tos@sys-5713 /cygdrive/c/WINDOWS/SYSTEM32/DRIVERS # c Sap [2] /cygdrive/c/SapWorkDir tos@sys-5713 /cygdrive/c/SapWorkDir # c -i /cygdrive/d/perl/site/lib/HTML/Element tos@sys-5713 /cygdrive/c/SapWorkDir # c [1] /home/tos/lab/dvlp/tos/too/picSort [2] /cygdrive/c/SapWorkDir [3] /cygdrive/c/WINDOWS/SYSTEM32/DRIVERS [4] /cygdrive/d/perl/site/lib/HTML/Element [#]> 4 tos@sys-5713 /cygdrive/d/perl/site/lib/HTML/Element =head1 CREDITS Michael Schilli http://perlmeister.com/scripts/cdbm-README.html http://www.heise.de/ix/artikel/2001/01/179/ =cut

Replies are listed 'Best First'.
Re: quick change dir by menu
by Aristotle (Chancellor) on May 31, 2003 at 03:46 UTC

    You can get away without any coding if you need bookmarked directories in bash (and possibly other shells): create a directory somewhere to hold your bookmarks (maybe ~/.dirbookmarks) and add it to your $CDPATH. Now fill the bookmark directory with symlinks to the bookmarked locations and you can cd <somebookmark> directly regardless of where you are. Filename completion on the bookmarks works too. Additionally after a complete -d cd for convenience, bash will only complete directory names when you type cd.

    Of course, all this presumes you're on a system that supports symlinks.

    Makeshifts last the longest.

Re: quick change dir by menu
by zentara (Archbishop) on May 31, 2003 at 14:38 UTC
    midnight commander does quick cd with <control \>. It's one of mc's best features.

      I use subst x: d:\path then

      > x:

      takes me straight there and its quicker than cd ...

      There are only 26 drives, but beyond about 5 or 6 and I can't remember which is which anyway.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://261866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (None)
    As of 2024-04-25 03:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found