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!
















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
@maninalift: I’ll post them (modules) soon, though I don’t have that many.
Comment by Ivan Čukić — 30 January 2010 @ 18:40
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
@Piotr Budny: Cool, but is there a way to set the completion for that approach?
Comment by Ivan Čukić — 30 January 2010 @ 22:43
Ivan: completion works OOTB
Comment by Piotr Budny — 3 February 2010 @ 12:58
I meant, mp, or simple mp3
Comment by Piotr Budny — 3 February 2010 @ 13:01
@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
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
@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