├── README.md └── lcd.sh /README.md: -------------------------------------------------------------------------------- 1 | # Lazy Change Directory 2 | 3 | Simple bash commands for bookmarked navigation of the file system, complete with bash-completion. Usage: 4 | 5 | * ```acd ``` add a mark. 6 | * ```pcd``` print list of marks. 7 | * ```gcd ``` grep marks for 'needle'. 8 | * ```lcd ``` change directories to mark target, supports tab completion. 9 | * ```dcd ``` delete a mark, supports tab completion. 10 | 11 | Easy as that. 12 | -------------------------------------------------------------------------------- /lcd.sh: -------------------------------------------------------------------------------- 1 | # Lazy Change Directory 2 | # 3 | # derived from some old code from spoonm, https://github.com/twerth/bashmarks and my own modifications. 4 | 5 | bookmarks_file=~/.bookmarks 6 | 7 | # create bookmark storage file. 8 | if [[ ! -f $bookmarks_file ]]; then 9 | touch $bookmarks_file 10 | fi 11 | 12 | 13 | # add a bookmark. ###################################################################################################### 14 | acd () 15 | { 16 | bookmark_name=$1 17 | 18 | # no bookmark name specified. 19 | if [[ -z $bookmark_name ]]; then 20 | echo "you didn't specify a bookmark name sir." 21 | 22 | else 23 | # store the bookmark as name|path. 24 | bookmark="$bookmark_name|`pwd`" 25 | 26 | # ensure bookmark doesn't already exist. 27 | if [[ -z `grep "^$bookmark_name|" $bookmarks_file` ]]; then 28 | echo $bookmark >> $bookmarks_file 29 | echo "bookmark saved: $bookmark_name" 30 | 31 | else 32 | echo "bookmark already exists by that name." 33 | fi 34 | fi 35 | } 36 | 37 | 38 | # delete a bookmark. ################################################################################################### 39 | dcd () 40 | { 41 | bookmark_name=$1 42 | 43 | # no bookmark name specified. 44 | if [[ -z $bookmark_name ]]; then 45 | echo "you didn't specify a bookmark name sir." 46 | 47 | else 48 | bookmark=`grep "^$bookmark_name|" "$bookmarks_file"` 49 | 50 | # no match. 51 | if [[ -z $bookmark ]]; then 52 | echo "no bookmark by that name sir." 53 | 54 | else 55 | cat $bookmarks_file | grep -v "^$bookmark_name|" > $bookmarks_file.tmp 56 | mv $bookmarks_file.tmp $bookmarks_file 57 | echo "bookmark deleted: $bookmark_name" 58 | fi 59 | fi 60 | } 61 | 62 | 63 | # grep bookmarks. ##################################################################################################### 64 | gcd () 65 | { 66 | needle=$1 67 | cat $bookmarks_file | sort | awk '{ printf "%-30s%-40s%s\n",$1,$2,$3}' FS=\| | grep $needle 68 | } 69 | 70 | 71 | # print bookmarks. ##################################################################################################### 72 | pcd () 73 | { 74 | cat $bookmarks_file | sort | awk '{ printf "%-20s%-40s%s\n",$1,$2,$3}' FS=\| 75 | } 76 | 77 | 78 | # lazy change directory. ############################################################################################### 79 | lcd () 80 | { 81 | bookmark_name=$1 82 | 83 | bookmark=`grep "^$bookmark_name|" "$bookmarks_file"` 84 | 85 | if [[ -z $bookmark ]]; then 86 | echo "no bookmark by that name sir." 87 | else 88 | dir=`echo "$bookmark|" | cut -d\| -f2` 89 | cd "$dir" 90 | fi 91 | } 92 | 93 | # bash completion. ##################################################################################################### 94 | _go_complete() 95 | { 96 | cat $bookmarks_file | cut -d\| -f1 | grep "$2.*" 97 | } 98 | 99 | complete -C _go_complete -o default lcd 100 | complete -C _go_complete -o default dcd 101 | --------------------------------------------------------------------------------