LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-07-2009, 01:08 PM   #1
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quick directory changing, with autocompletion: howto


I've been fooling around with this for a while, and I've just gotten it to work the way I always wanted it. I've managed to create a set of functions that will let you cd directly into preset directories, and use bash_completion to quickly navigate subdirectories as well. Hopefully others will find this useful as well.

First, create a set of variables and simple functions like this in your bashrc:
Code:
export MUSICDIR="/mount/bigdrive/media/music"
cdmusic ()
{
    cd "$MUSICDIR/$1" || cd "$MUSICDIR"
}
Now you can quickly cd into your music directory with "cdmusic", and further into subdirectories with e.g. "cdmusic rock/pink_floyd". But it won't autocomplete the directory listings for you...yet.

To enable bash_completion for the command, create a new file in /etc/bash_completion.d/ and name it something like 10_cd2dirs. For each function you've defined above, include the following code:

Code:
_cdmusic()
{

        local CD_PATH="$MUSICDIR"
        local IFS=$'\t\n' cur=`_get_cword` i j k

        # Enable -o filenames option, see Debian bug #272660
        compgen -f /non-existing-dir/ >/dev/null

        local -r mark_dirs=$(_rl_enabled mark-directories && echo y)
        local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y)

        # loop through CD_PATH contents
        for i in ${CD_PATH}; do
                # create an array of matched subdirs
                k="${#COMPREPLY[@]}"
                for j in $( compgen -d $i/$cur ); do
                        if [[ ( $mark_symdirs && -h $j || $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
                                j="${j}/"
                        fi
                        COMPREPLY[k++]=${j#$i/}
                done
        done

        if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
            i=${COMPREPLY[0]}
            if [ "$i" == "$cur" ] && [[ $i != "*/" ]]; then
                COMPREPLY[0]="${i}"
            fi
        fi

        return 0
}

if shopt -q cdable_vars; then
    complete -o nospace -v -F _cdmusic $nospace cdmusic
else
    complete -o nospace -F _cdmusic $nospace cdmusic
fi

# Above code is a modified version of the
# bash_completion function for "cd", but this
# only lists entries in the directory defined
# in CD_PATH.  I'm not 100% sure what all the
# commands do, but it seems to work for me.
Just be sure to modify the names on the first and final lines to match each function name, and make CD_PATH the main target directory of that function.

Now just reload your bashrc (or open a new console) and you should be able to cruise your directories in speed and comfort.
 
Old 09-08-2009, 05:41 AM   #2
kofucii
Member
 
Registered: May 2007
Location: Bulgaria
Distribution: Slackware, SCO Unix
Posts: 62

Rep: Reputation: 20
Can't you just use CDPATH function in bash? Something like this:

export CDPATH=.:/mount/bigdrive/media/music
 
Old 09-08-2009, 08:01 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Original Poster
Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Oh sure, just ruin take all of the steam out of my hard work, why don't you?

Actually, I simply wasn't aware that you could do that with CDPATH. Reading the description of it more carefully, I now realize that. You can even specify multiple paths.

Still, I think I prefer having specific cd commands for different locations, as opposed to one big path completion pool. It's easier for me psychologically.

Oh, well. At least I have my working functions, and I also learned a bit about how bash_completion works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SCP and Autocompletion PlatinumX Linux - Networking 6 09-25-2010 03:25 PM
[SOLVED] GRUB recover. Quick HOWTO. mufy Red Hat 1 07-20-2007 08:57 AM
Quick Q: convert ps to pdf-HOWTO smiley_lauf Linux - Newbie 3 02-14-2006 09:38 PM
Quick HOWTO to share Jzarecta Linux - Networking 2 01-02-2006 09:59 AM
HOWTO Guides - A Quick Question Sadie Newlinux Linux - Newbie 5 04-24-2003 08:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration