diff --git a/zsh/.zshrc b/zsh/.zshrc index 59dc6d8..c4a1f5c 100755 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -198,32 +198,46 @@ function extract() } forget () { - print "Forgetting last line with '$@'. Please reload shell after completion." + if [ -z "$@" ]; then + echo "ERROR: No keyword given" + return + fi - temp=$(mktemp) + echo "Forgetting last command with '$@'." - sed -e '$ d' ~/.zsh_history | tac | awk "!/$@/ || f++" | tac > $temp + new_zsh_history=$(tac ~/.zsh_history | awk "/$@/ && p<2 {p++; next}1" | tac) print "Difference between .zsh_history and new .zsh_history" - diff ~/.zsh_history $temp + echo $new_zsh_history | diff ~/.zsh_history - - mv $temp ~/.zsh_history - - if [ -f $temp ]; then - rm -f $temp + read answer + if [ "$answer" = "y" ]; then + echo $new_zsh_history > ~/.zsh_history + echo "Last line with '$@' forgotten" + echo "You will need to reload your shell for the changes to take effect." + else + echo "Changes not applied." fi } forgetall () { - print "Forgetting all lines with '$@'. Please reload shell after completion." + if [ -z "$@" ]; then + echo "ERROR: No keyword given" + return + fi - temp=$(mktemp) - sed "/$@/d" ~/.zsh_history > $temp + echo "Forgetting all command with '$@'." + + new_zsh_history=$(grep --inverse-match "$@" ~/.zsh_history) print "Difference between .zsh_history and new .zsh_history" - diff ~/.zsh_history $temp - mv $temp ~/.zsh_history + echo $new_zsh_history | diff ~/.zsh_history - - if [ -f $temp ]; then - rm -f $temp + read -p 'Apply changes? (y/n): ' answer + if [ "$answer" = "y" ]; then + echo $new_zsh_history > ~/.zsh_history + echo "Last line with '$@' forgotten" + echo "You will need to reload your shell for the changes to take effect." + else + echo "Changes not applied." fi } @@ -237,17 +251,3 @@ fcd() { fi } -tcd() { - DIR=$(mktemp -d 2>/dev/null) - if [[ -e "$DIR" ]]; then - cd $DIR - print "Moved to \"$DIR\"" - else - print "\"$DIR\" failed to be created" - fi -} - -ttcd() { - cd /tmp - tcd -}