Skip to content

Instantly share code, notes, and snippets.

@copitux
Created January 23, 2012 13:17
Show Gist options
  • Save copitux/1663070 to your computer and use it in GitHub Desktop.
Save copitux/1663070 to your computer and use it in GitHub Desktop.
Bookmarks in bash shell
# Source: http://hints.macworld.com/article.php?story=20020716005123797
if [ ! -f ~/.dirs ]; then # if doesn't exist, create it
touch ~/.dirs
fi
alias show='cat ~/.dirs'
save (){
command sed "/!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "$@"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ;
}
source ~/.dirs # Initialization for the above 'save' facility: source the .sdirs file
shopt -s cdable_vars # set the bash option so that no '$' is required when using the above facility

Usage

[/home/user]$ cd some/path
[/home/user/project/omg]$ save omg
[/home/user/project/omg]$ cd
[/home/user]$ cd omg
[/home/user]$ cp file $omg

[/home/user]$ show
omg="/home/user/project/omg"
[/home/user]$ $EDITOR ~/.dirs

Sample install

$ curl https://raw.github.com/gist/97b1806058ed33cc2612/3b98af2ba3ceaa596139311337a5750686c8a6cc/bookmarks.bash > ~/.bookmarks.bash
$ echo '. ~/.bookmarks.bash' >> ~/.bashrc
@agonzalezro
Copy link

If you are zsh users you can use too pushd, popd and the variable $OLDPWD.

Example:

alex turtle:~ [1005]% pushd
~ ~
alex turtle:~ [1006]% cd /tmp 
alex turtle:/tmp [1007]% mkdir $OLDPWD/gist
alex turtle:/tmp [1008]% ls -d $HOME/gist
/home/alex/gist

@ernesto-jimenez
Copy link

Both comments are valid for zsh and bash :)

@copitux
Copy link
Author

copitux commented Jan 24, 2012

I knew CDPATH, in fact I actually use it with 'projects' folder to put cd project_name in Vim (I haven't searched about project management in Vim as I love the simplicity)

I couldn't use bookmarks functionality into Vim, for example :cd omg. I thought about map each enviroment var into Vim's vars and then aliasing 'cd' command to "exe 'cd' . fnameescape($shell_var)" but... it's so complicated to me.
So I've dynamics bookmarks every session into shell and project alias into Vim. It's useful enough ;)

PD: If any arrives here and has a solution to mix both functionality, please comment it

@agonzalezro
Copy link

You can use the env variable $OLDPWD of my "solution" on vim.

Furhtermore, I think that you can use $omg variable on vim too, am I right?

Example: try to write on vim :!echo "testing" > $omg/testing and then :vi $omg/testing

@copitux
Copy link
Author

copitux commented Jan 24, 2012

I saw your pushd / popd+ $OLDPWD solution but I think that it isn't the same functionality like bookmarks for two reasons:

  1. It's a LIFO structure and I couldn't choose a 'bookmark' (but I maybe don't understand very well with $OLDPWD var)
  2. The bookmark's solution persists

And about Vim, I was talking about use the :cd command with environment variables inside Vim (and without the !)

 $ vim
 > ~
 > ~
 ---------
 :cd bookmark_dir

 or ... `:cd $bookmark_dir`

@agonzalezro
Copy link

Indeed, this was the reason why I wrote "solution" between quotes.

About the use of variables inside vim, you can always use env variable, like :cd $HOME without the need of execute it in a shel with the !.

In my shell, I use old-school solution: https://github.com/agonzalezro/.dot/blob/master/zsh#L31 :)

@copitux
Copy link
Author

copitux commented Jan 24, 2012

Awesome!. You've just discovered implicitly the 'error' ;)

We can modify the script like that and each bookmark_dir will be an environment variable so we can use it in Vim

command sed "/!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "export $@"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ;

I'm thinking to append some string to bookmark name because I could override some important variable like PATH... or always remember that I have to save bookmarks in lowercase :P

Thanks for your help ;)

PD: Yes, old-school solution is so great too ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment