From 1b1292947f54bea3d581144e4bf13ec8caf75609 Mon Sep 17 00:00:00 2001 From: Dmitriy B Date: Tue, 23 May 2023 02:08:40 +0500 Subject: [PATCH 1/4] Make bashmark compatible with files and add default dir for g --- bashmarks.sh | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/bashmarks.sh b/bashmarks.sh index b77a8c1..8878890 100644 --- a/bashmarks.sh +++ b/bashmarks.sh @@ -22,16 +22,6 @@ # POSSIBILITY OF SUCH DAMAGE. -# USAGE: -# s bookmarkname - saves the curr dir as bookmarkname -# g bookmarkname - jumps to the that bookmark -# g b[TAB] - tab completion is available -# p bookmarkname - prints the bookmark -# p b[TAB] - tab completion is available -# d bookmarkname - deletes the bookmark -# d [TAB] - tab completion is available -# l - list all bookmarks - # setup file to store bookmarks if [ ! -n "$SDIRS" ]; then SDIRS=~/.sdirs @@ -48,7 +38,16 @@ function s { if [ -z "$exit_message" ]; then _purge_line "$SDIRS" "export DIR_$1=" CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") - echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS + if [ -z "$2" ]; then + echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS + else + if [ -f "$2" ]; then + CURDIR="${CURDIR}/$2" + echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS + else + echo "File doesn't exist" + fi + fi fi } @@ -57,8 +56,12 @@ function g { check_help $1 source $SDIRS target="$(eval $(echo echo $(echo \$DIR_$1)))" - if [ -d "$target" ]; then + if [ -z "$1" ] && [ ! -z "$BASHMARKS_DEF_DIR" ]; then + cd "$BASHMARKS_DEF_DIR" + elif [ -d "$target" ]; then cd "$target" + elif [ -f "$target" ]; then + "$EDITOR" "$target" elif [ ! -n "$target" ]; then echo -e "\033[${RED}WARNING: '${1}' bashmark does not exist\033[00m" else @@ -86,18 +89,20 @@ function d { # print out help for the forgetful function check_help { if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then + echo 'Remember to add "source ~/.local/bin/bashmarks.sh" to your .bashrc file' + echo 'Also you could assign BASHMARKS_DEF_DIR to dir that you want to cd in when you provide no args for "g"' echo '' - echo 's - Saves the current directory as "bookmark_name"' - echo 'g - Goes (cd) to the directory associated with "bookmark_name"' - echo 'p - Prints the directory associated with "bookmark_name"' + echo 's - Saves the current directory or file as "bookmark_name"' + echo 'g - Goes (cd) to the directory or open the file associated with "bookmark_name"' + echo 'p - Prints the directory or file associated with "bookmark_name"' echo 'd - Deletes the bookmark' - echo 'l - Lists all available bookmarks' + echo 'v - Lists all available bookmarks (view dirs)' kill -SIGINT $$ fi } -# list bookmarks with dirnam -function l { +# list bookmarks with dirname +function v { check_help $1 source $SDIRS From 1e0f9524e1b30b3adff605b6fb88176b52929934 Mon Sep 17 00:00:00 2001 From: ybda Date: Sat, 26 Aug 2023 10:44:35 +0500 Subject: [PATCH 2/4] Fixes & Cleanup & Change default names --- .gitignore | 1 + bashmarks.sh | 242 +++++++++++++++++++++++++-------------------------- 2 files changed, 121 insertions(+), 122 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/bashmarks.sh b/bashmarks.sh index 8878890..000d084 100644 --- a/bashmarks.sh +++ b/bashmarks.sh @@ -1,174 +1,172 @@ +#!/bin/bash # Copyright (c) 2010, Huy Nguyen, http://www.huyng.com # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, are permitted provided # that the following conditions are met: # -# * Redistributions of source code must retain the above copyright notice, this list of conditions +# * Redistributions of source code must retain the above copyright notice, this list of conditions # and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the # following disclaimer in the documentation and/or other materials provided with the distribution. # * Neither the name of Huy Nguyen nor the names of contributors -# may be used to endorse or promote products derived from this software without +# may be used to endorse or promote products derived from this software without # specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # setup file to store bookmarks -if [ ! -n "$SDIRS" ]; then - SDIRS=~/.sdirs +if [ -z "$BASHMARKS_LIST_FILE" ]; then + BASHMARKS_LIST_FILE=~/.bashmarkslist fi -touch $SDIRS +touch "$BASHMARKS_LIST_FILE" RED="0;31m" GREEN="0;33m" # save current directory to bookmarks -function s { - check_help $1 - _bookmark_name_valid "$@" - if [ -z "$exit_message" ]; then - _purge_line "$SDIRS" "export DIR_$1=" - CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") - if [ -z "$2" ]; then - echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS - else - if [ -f "$2" ]; then - CURDIR="${CURDIR}/$2" - echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS - else - echo "File doesn't exist" - fi - fi - fi +bashmarks_save() { + _bashmarks_check_help "$1" + _bashmarks_bookmark_name_valid "$@" + if [ -z "$exit_message" ]; then + _bashmarks_purge_line "$BASHMARKS_LIST_FILE" "export DIR_$1=" + local CURDIR + CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") + if [ -z "$2" ]; then + echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_LIST_FILE" + else + if [ -f "$2" ]; then + CURDIR="${CURDIR}/$2" + echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_LIST_FILE" + else + echo "File doesn't exist" + fi + fi + fi } # jump to bookmark -function g { - check_help $1 - source $SDIRS - target="$(eval $(echo echo $(echo \$DIR_$1)))" - if [ -z "$1" ] && [ ! -z "$BASHMARKS_DEF_DIR" ]; then - cd "$BASHMARKS_DEF_DIR" - elif [ -d "$target" ]; then - cd "$target" - elif [ -f "$target" ]; then - "$EDITOR" "$target" - elif [ ! -n "$target" ]; then - echo -e "\033[${RED}WARNING: '${1}' bashmark does not exist\033[00m" - else - echo -e "\033[${RED}WARNING: '${target}' does not exist\033[00m" - fi +bashmarks_jump() { + _bashmarks_check_help "$1" + . "$BASHMARKS_LIST_FILE" + local target + target="$(eval "$(echo echo "$(echo \$DIR_$1)")")" + if [ -z "$1" ] && [ -n "$BASHMARKS_DEFAULT_DIR" ]; then + cd "$BASHMARKS_DEFAULT_DIR" + elif [ -d "$target" ]; then + cd "$target" + elif [ -f "$target" ]; then + "$EDITOR" "$target" + elif [ -z "$target" ]; then + echo -e "\033[${RED}WARNING: '${1}' bashmark does not exist\033[00m" + else + echo -e "\033[${RED}WARNING: '${target}' does not exist\033[00m" + fi } # print bookmark -function p { - check_help $1 - source $SDIRS - echo "$(eval $(echo echo $(echo \$DIR_$1)))" +bashmarks_print() { + _bashmarks_check_help "$1" + . "$BASHMARKS_LIST_FILE" + if [ -z "$1" ]; then + # if color output is not working for you, comment out the line below '\033[1;32m' == "red" + env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}' + + # uncomment this line if color output is not working with the line above + # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" + else + echo "$(eval "$(echo echo "$(echo \$DIR_$1)")")" + fi } # delete bookmark -function d { - check_help $1 - _bookmark_name_valid "$@" - if [ -z "$exit_message" ]; then - _purge_line "$SDIRS" "export DIR_$1=" - unset "DIR_$1" - fi +bashmarks_delete() { + _bashmarks_check_help "$1" + _bashmarks_bookmark_name_valid "$@" + if [ -z "$exit_message" ]; then + _bashmarks_purge_line "$BASHMARKS_LIST_FILE" "export DIR_$1=" + unset "DIR_$1" + fi } # print out help for the forgetful -function check_help { - if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then - echo 'Remember to add "source ~/.local/bin/bashmarks.sh" to your .bashrc file' - echo 'Also you could assign BASHMARKS_DEF_DIR to dir that you want to cd in when you provide no args for "g"' - echo '' - echo 's - Saves the current directory or file as "bookmark_name"' - echo 'g - Goes (cd) to the directory or open the file associated with "bookmark_name"' - echo 'p - Prints the directory or file associated with "bookmark_name"' - echo 'd - Deletes the bookmark' - echo 'v - Lists all available bookmarks (view dirs)' - kill -SIGINT $$ - fi +_bashmarks_check_help() { + if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then + echo 'bashmarks_save - Saves the current directory or file as "bookmark_name"' + echo 'bashmarks_jump - Goes (cd) to the directory or open the file associated with "bookmark_name"' + echo 'bashmarks_print - Prints the directory or file associated with "bookmark_name"' + echo 'bashmarks_print - Lists all available bookmarks (view dirs)' + echo 'bashmarks_delete - Deletes the bookmark' + kill -SIGINT $$ + fi } -# list bookmarks with dirname -function v { - check_help $1 - source $SDIRS - - # if color output is not working for you, comment out the line below '\033[1;32m' == "red" - env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}' - - # uncomment this line if color output is not working with the line above - # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" -} # list bookmarks without dirname -function _l { - source $SDIRS - env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" +_bashmarks_list_without_dirname() { + . "$BASHMARKS_LIST_FILE" + env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" } # validate bookmark name -function _bookmark_name_valid { - exit_message="" - if [ -z $1 ]; then - exit_message="bookmark name required" - echo $exit_message - elif [ "$1" != "$(echo $1 | sed 's/[^A-Za-z0-9_]//g')" ]; then - exit_message="bookmark name is not valid" - echo $exit_message - fi +_bashmarks_bookmark_name_valid() { + exit_message="" + if [ -z "$1" ]; then + exit_message="Bookmark name required" + echo "$exit_message" + elif [ "$1" != "$(echo $1 | sed 's/[^A-Za-z0-9_]//g')" ]; then + exit_message="Bookmark name is not valid" + echo "$exit_message" + fi } # completion command -function _comp { - local curw - COMPREPLY=() - curw=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=($(compgen -W '`_l`' -- $curw)) - return 0 +_bashmarks_comp() { + local curw + COMPREPLY=() + curw=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=($(compgen -W '`_bashmarks_list_without_dirname`' -- $curw)) + return 0 } # ZSH completion command -function _compzsh { - reply=($(_l)) +_bashmarks_compzsh() { + reply=($(_bashmarks_list_without_dirname)) } -# safe delete line from sdirs -function _purge_line { - if [ -s "$1" ]; then - # safely create a temp file - t=$(mktemp -t bashmarks.XXXXXX) || exit 1 - trap "/bin/rm -f -- '$t'" EXIT - - # purge line - sed "/$2/d" "$1" > "$t" - /bin/mv "$t" "$1" - - # cleanup temp file - /bin/rm -f -- "$t" - trap - EXIT - fi +# safe delete line from BASHMARKS_LIST_FILE +_bashmarks_purge_line() { + if [ -s "$1" ]; then + # safely create a temp file + local t + t=$(mktemp -t bashmarks.XXXXXX) || exit 1 + trap "/bin/rm -f -- '$t'" EXIT + + # purge line + sed "/$2/d" "$1" >"$t" + /bin/mv "$t" "$1" + + # cleanup temp file + /bin/rm -f -- "$t" + trap - EXIT + fi } -# bind completion command for g,p,d to _comp -if [ $ZSH_VERSION ]; then - compctl -K _compzsh g - compctl -K _compzsh p - compctl -K _compzsh d -else - shopt -s progcomp - complete -F _comp g - complete -F _comp p - complete -F _comp d +# bind completion command +if [ "$ZSH_VERSION" ]; then + compctl -K _bashmarks_compzsh bashmarks_jump + compctl -K _bashmarks_compzsh bashmarks_print + compctl -K _bashmarks_compzsh bashmarks_delete +elif [ "$BASH_VERSION" ]; then + shopt -s progcomp + complete -F _bashmarks_comp bashmarks_jump + complete -F _bashmarks_comp bashmarks_print + complete -F _bashmarks_comp bashmarks_delete fi From 78f45847dc9e3057b23a742cf022e61545d0377e Mon Sep 17 00:00:00 2001 From: Dmitriy B <31192670+ybda@users.noreply.github.com> Date: Mon, 1 Jan 2024 01:37:08 +0500 Subject: [PATCH 3/4] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index c13612f..93ab357 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ ### Bashmarks is a shell script that allows you to save and jump to commonly used directories. Now supports tab completion. + +|**Note: Maintainership of this project has been ceased. You may want to use [shmarks](https://github.com/ybda/shmarks) instead.**| +|----| + + ## Install 1. git clone git://github.com/huyng/bashmarks.git From 2226676a3b790c33646bade64aba5498f86502ec Mon Sep 17 00:00:00 2001 From: Dmitriy B <31192670+ybda@users.noreply.github.com> Date: Mon, 1 Jan 2024 01:37:39 +0500 Subject: [PATCH 4/4] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 93ab357..ba3b252 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ -### Bashmarks is a shell script that allows you to save and jump to commonly used directories. Now supports tab completion. - - |**Note: Maintainership of this project has been ceased. You may want to use [shmarks](https://github.com/ybda/shmarks) instead.**| |----| +### Bashmarks is a shell script that allows you to save and jump to commonly used directories. Now supports tab completion. + ## Install 1. git clone git://github.com/huyng/bashmarks.git