Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a directory path for which I want to create a shortcut, like ~ for a users home directory.

I've tried the ENV variables, and I could match $<something> for a path, but I dont want that $ letter.

I'm guessing maybe with some kind of .bashrc shenanigans?!

share|improve this question

2 Answers 2

I have a handy little function that I use:

# A function to help with creating directory aliases and providing
# completion for them.
# Taken from here:
#   http://blog.caioromao.com/2010/10/10/Custom-directory-completion.html
# Tweaked to work with more than just 'cd'
_make_dir_complete() {
    local aliasname=$1
    local prgname="__s_${aliasname}__"

    cd "$3" >/dev/null 2>&1
    local dirname=$(pwd -L)
    local realpath=$(pwd -P)
    cd - >/dev/null 2>&1

    FUNC="function $prgname() {
        local cur len wrkdir;
        local IFS=\$'\\n'
        wrkdir=\"$realpath\"
        cur=\${COMP_WORDS[COMP_CWORD]};
        len=\$((\${#wrkdir} + 2));
        COMPREPLY=( \$(compgen -S/ -d \$wrkdir/\$cur| cut -b \$len-) );
    }"
    ALIAS="$aliasname () { $2 \"$dirname/\$*\"; }"

    eval $FUNC
    eval $ALIAS
    complete -o nospace -F $prgname $aliasname
}

And then I have some code like this to setup my shortcuts:

test -e ~/projects &&
    _make_dir_complete cdp cd ~/projects &&
    _make_dir_complete pdp pushd ~/projects

This will set up two bash functions, cdp and pdp. cdp I use to change directories, so I do something like cdp foo, which will translate into cd ~/projects/foo. pdp works similarly, but uses pushd instead. The nice part is that it does completion as well, so I can type cdp f, then press TAB, and it will complete with cdp foo.

share|improve this answer

I have this set up in my .bashrc:

alias re='cd /home/terdon/research/'

So, if I type re at the BASH prompt, I directly cd into my research directory.

This works fine if you only want to change into the directory in question. Another way would be to set an alias that echoes the directory path:

alias foo='echo /path/to/bar'

You can then do, for example:

$ ls `foo`

You will need the inverted commas though. As far as I know there is no way to set up a simple character, with no quotes or $ to do what you want in BASH. It is possible in zsh with "named directories".

Finally, you can find some interesting tips here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.