Skip to content

Instantly share code, notes, and snippets.

@166MMX
Last active November 20, 2017 12:21
Show Gist options
  • Save 166MMX/5064acdd8f8502f28980 to your computer and use it in GitHub Desktop.
Save 166MMX/5064acdd8f8502f28980 to your computer and use it in GitHub Desktop.
#!/bin/sh
in_place() {
content="$1"
file="$2"
id="$3"
range="/^#${id} START\$/,/^#${id} END\$/"
[ -f "$file" ] && [ -n "$(tail -c 1 "$file")" ] && echo >>"$file"
{ rm -- "$file" && sed "${range}d" >"$file"; } <"$file" && sed -n "${range}p" >>"$file" <"$content"
}
in_place "$0" "$HOME/.profile" 'Jump marks .profile'
in_place "$0" "$HOME/.bash_completion" 'Jump marks .bash_completion'
exit 0
#Jump marks .profile START
jump() {
#new
store="$HOME/.jump_marks"
case "$1" in
mark)
name="${2-$(basename -- "$PWD")}"
mkdir -p "$store" && ln -s "$PWD" "$store/$name"
unset name
;;
marks|list|ls)
FS="$(printf '\x1C')"
find "$store" -type l -prune -execdir stat -f "%N$FS->$FS%Y" '{}' ';' | column -s "$FS" -t
unset FS
;;
unmark)
name="${2-$(basename -- "$PWD")}"
rm -i -- "$store/$name" || echo "No such mark: $name"
unset name
;;
*)
name="$1"
cd -P -- "$store/$name" 2>/dev/null || echo "No such mark: $name"
unset name
;;
esac
unset store
}
#Jump marks .profile END
#Jump marks .bash_completion START
_complete_marks() {
#new
declare cur="${COMP_WORDS[COMP_CWORD]}"
declare -a dirlist
IFS=$'\n'; dirlist=( $(find "$HOME/.jump_marks" -type l -prune -execdir stat -f '%N' '{}' ';') ); unset IFS
COMPREPLY=( $(compgen -W "${dirlist[*]} mark list unmark" -- "$cur") )
return 0
}
complete -F _complete_marks jump unmark
#Jump marks .bash_completion END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment