├── README └── bashmarks.sh /README: -------------------------------------------------------------------------------- 1 | Provides bookmarking ability for folders/directories in bash. 2 | 3 | To install, put bashmarks.sh somewhere such as ~/bin, then source it 4 | in your .bashrc file (or other bash startup file): 5 | source ~/bin/bashmarks.sh 6 | 7 | To bookmark a folder, simply go to that folder, then bookmark it like so: 8 | bookmark foo 9 | 10 | The bookmark will be named "foo" 11 | 12 | When you want to get back to that folder use: 13 | go foo 14 | 15 | To see a list of the bookmarks: 16 | bookmarksshow 17 | 18 | Tab completion works, to go to the shoobie folder: 19 | go sho[tab] 20 | -------------------------------------------------------------------------------- /bashmarks.sh: -------------------------------------------------------------------------------- 1 | # Bashmarks is a simple set of bash functions that allows you to bookmark 2 | # folders in the command-line. 3 | # 4 | # To install, put bashmarks.sh somewhere such as ~/bin, then source it 5 | # in your .bashrc file (or other bash startup file): 6 | # source ~/bin/bashmarks.sh 7 | # 8 | # To bookmark a folder, simply go to that folder, then bookmark it like so: 9 | # bookmark foo 10 | # 11 | # The bookmark will be named "foo" 12 | # 13 | # When you want to get back to that folder use: 14 | # go foo 15 | # 16 | # To see a list of bookmarks: 17 | # bookmarksshow 18 | # 19 | # Tab completion works, to go to the shoobie bookmark: 20 | # go sho[tab] 21 | # 22 | # Your bookmarks are stored in the ~/.bookmarks file 23 | 24 | bookmarks_file=~/.bookmarks 25 | 26 | # Create bookmarks_file it if it doesn't exist 27 | if [[ ! -f $bookmarks_file ]]; then 28 | touch $bookmarks_file 29 | fi 30 | 31 | bookmark (){ 32 | bookmark_name=$1 33 | 34 | if [[ -z $bookmark_name ]]; then 35 | echo 'Invalid name, please provide a name for your bookmark. For example:' 36 | echo ' bookmark foo' 37 | else 38 | bookmark="`pwd`|$bookmark_name" # Store the bookmark as folder|name 39 | 40 | if [[ -z `grep "|$bookmark_name" $bookmarks_file` ]]; then 41 | echo $bookmark >> $bookmarks_file 42 | echo "Bookmark '$bookmark_name' saved" 43 | else 44 | echo "Bookmark '$bookmark_name' already exists. Replace it? (y or n)" 45 | while read replace 46 | do 47 | if [[ $replace = "y" ]]; then 48 | # Delete existing bookmark 49 | sed "/.*|$bookmark_name/d" $bookmarks_file > ~/.tmp && mv ~/.tmp $bookmarks_file 50 | # Save new bookmark 51 | echo $bookmark >> $bookmarks_file 52 | echo "Bookmark '$bookmark_name' saved" 53 | break 54 | elif [[ $replace = "n" ]]; then 55 | break 56 | else 57 | echo "Please type 'y' or 'n'" 58 | fi 59 | done 60 | fi 61 | fi 62 | } 63 | 64 | # Delete the named bookmark from the list 65 | bookmarkdelete (){ 66 | bookmark_name=$1 67 | 68 | if [[ -z $bookmark_name ]]; then 69 | echo 'Invalid name, please provide the name of the bookmark to delete.' 70 | else 71 | bookmark=`grep "|$bookmark_name$" "$bookmarks_file"` 72 | 73 | if [[ -z $bookmark ]]; then 74 | echo 'Invalid name, please provide a valid bookmark name.' 75 | else 76 | cat $bookmarks_file | grep -v "|$bookmark_name$" $bookmarks_file > bookmarks_temp && mv bookmarks_temp $bookmarks_file 77 | echo "Bookmark '$bookmark_name' deleted" 78 | fi 79 | fi 80 | } 81 | 82 | # Show a list of the bookmarks 83 | bookmarksshow (){ 84 | cat $bookmarks_file | awk '{ printf "%-40s%-40s%s\n",$1,$2,$3}' FS=\| 85 | } 86 | 87 | go(){ 88 | bookmark_name=$1 89 | 90 | bookmark=`grep "|$bookmark_name$" "$bookmarks_file"` 91 | 92 | if [[ -z $bookmark ]]; then 93 | echo 'Invalid name, please provide a valid bookmark name. For example:' 94 | echo ' go foo' 95 | echo 96 | echo 'To bookmark a folder, go to the folder then do this (naming the bookmark 'foo'):' 97 | echo ' bookmark foo' 98 | else 99 | dir=`echo "$bookmark" | cut -d\| -f1` 100 | cd "$dir" 101 | fi 102 | } 103 | 104 | _go_complete(){ 105 | # Get a list of bookmark names, then grep for what was entered to narrow the list 106 | cat $bookmarks_file | cut -d\| -f2 | grep "$2.*" 107 | } 108 | 109 | complete -C _go_complete -o default go 110 | --------------------------------------------------------------------------------