You are using an obsolete web browser, or you have the page styles (CSS) disabled. You'll be able to access this site, but only a visually simplified version.

ZSH: bookmarks for cd (change directory) with completion

Another zsh-related post. This one is about defining a function that will allow you to define aliases for directories so that you don’t need to type something like ‘cd /this/is/a/very/long/path/in/the/filesystem’, but only ‘cdb some-alias’ (cdb – Change Directory with Bookmarks)

You can copy the script to your .zshrc, or create a zsh module as described in my previous post

# Module: Change directory with bookmarks
# path: bin/zsh-modules-available/cdbookmarks

function cdb_edit() {
  vim ~/.cdbookmarks
}

function cdb() {
  NewDir=`egrep "^$1<TAB_KEY>" ~/.cdbookmarks \
     | sed 's/^.*<TAB_KEY>//'`;
  echo cd $NewDir
  cd $NewDir
}

function _cdb() {
  reply=(`cat ~/.cdbookmarks | sed 's/<TAB_KEY>.*$//'`);
}

compctl -K _cdb cdb

That’s it. To define the bookmarks, create a file named ~/.cdbookmarks and the bookmarks in the following format:

<alias><TAB_KEY><path>

(no spaces)

Cheerio!

Share and Enjoy:
  • FSDaily
  • Slashdot
  • Digg
  • del.icio.us
  • Identi.ca
  • Twitter
  • Technorati
  • Reddit
  • LinkedIn
  • Netvibes
  • StumbleUpon
  • Facebook
  • Add to favorites
  • email

9 Comments »

  1. More ZSH wisdom.

    I use a pretty nearly unconfigured zsh, I’m sure I could learn a lot if you posted your whole config.

    Comment by maninalift — 30 January 2010 @ 10:24 Reply to this comment

  2. @maninalift: I’ll post them (modules) soon, though I don’t have that many.

    Comment by Ivan Čukić — 30 January 2010 @ 18:40 Reply to this comment

  3. Nice one, I’m using something similar, e.g.:
    mp3=/media/storage/mp3 in my .zshrc.

    Simple `cd mp3` does the job.

    Hope to see more!
    Wishes

    Comment by Piotr Budny — 30 January 2010 @ 22:30 Reply to this comment

  4. @Piotr Budny: Cool, but is there a way to set the completion for that approach?

    Comment by Ivan Čukić — 30 January 2010 @ 22:43 Reply to this comment

  5. Ivan: completion works OOTB

    Comment by Piotr Budny — 3 February 2010 @ 12:58 Reply to this comment

  6. I meant, mp, or simple mp3

    Comment by Piotr Budny — 3 February 2010 @ 13:01 Reply to this comment

  7. @Piotr Budny: It doesn’t for me – if I do a
    $ mp3=/asadsda/sdasda/sdasd

    and do
    $ cd mp[TAB]
    nothing happens. It only completes if I start with the tilde character

    $ cd ~mp[TAB]
    completes to
    $ cd ~mp3
    and CDs to /asadsda/sdasda/sdasd

    Comment by Ivan Čukić — 3 February 2010 @ 13:04 Reply to this comment

  8. eghr, it ate tags; should be mp3-tab-tab-enter, or simple mp3-enter. You can join my comments into one.

    Comment by Piotr Budny — 3 February 2010 @ 13:05 Reply to this comment

  9. @Piotr Budny: Heh, it ate mine as well.

    The problem is that I want to have a full name for the bookmark – for example, I have lancelot, liblancelot, liblancelot-dataengines.

    I used to shorten them before to lnc, llnc… but it became tiresome to remember all the shortcuts. This way, the autocompletion liblancelot does the work.

    Comment by Ivan Čukić — 3 February 2010 @ 13:21 Reply to this comment

RSS feed for comments on this post. TrackBack URL

Leave a comment