bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: to - Bookmark file system locations in bash on POSIX-like systems


From: Greg Wooledge
Subject: Re: to - Bookmark file system locations in bash on POSIX-like systems
Date: Mon, 8 Apr 2013 08:50:48 -0400
User-agent: Mutt/1.4.2.3i

On Thu, Apr 04, 2013 at 08:51:19PM -0500, Mara Kim wrote:
> The biggest benefit is that it is just plain easier than managing a
> directory of symbolic links on your own. I am extremely lazy.
> 
> Here is an example use case. At the end of a work day, I like to bookmark
> the folder I am working in on my cluster with
> 
> to -b work
> 
> The next day, I can come back in and use
> 
> to work

In .bashrc:

declare -A bookmarks
while IFS= read -rd '' mark; do
    IFS= read -rd '' dir || break
    bookmarks["$mark"]=$dir
done < ~/.bash-bookmarks

mark() {
    if [[ ${bookmarks["$1"]} ]]; then
        echo "Bookmark already set: $1 -> ${bookmarks["$1"]}" >&2
    else
        bookmarks["$1"]=$PWD
        printf '%s\0' "$1" "$PWD" >> ~/.bash-bookmarks
    fi
}

to() {
    if [[ ${bookmarks["$1"]} ]]; then
        cd "${bookmarks["$1"]}"
    else
        echo "No such bookmark: $1" >&2
    fi
}

If you really insist on using "to -b" instead of "mark" then it'll be a
bit lengthier, as you'll need to add option checking to "to".

Multiple concurrent shells could conceivably write contradictory (or
duplicate) bookmark definitions to the ~/.bash-bookmarks file.  In this
implementation, whichever one is written last will be the one that counts
when a new shell reads the file.  You might want to clean up duplicates
and contradictions eventually.  You might also want to write a function to
remove a bookmark from the file (hint: it'll involve writing the whole new
set of bookmarks to a temp file and then moving it back to the original).

I've taken the liberty of copying address@hidden as I believe that
is a more appropriate list for this discussion.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]