From a6070c8348d39118aeb98e5e29c81094eacc14f4 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Sun, 17 Oct 2021 01:20:04 -0700 Subject: [PATCH] zsh: better save/load working directory behavior --- .zshrc | 64 +++++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/.zshrc b/.zshrc index 0ab7017..71e8723 100644 --- a/.zshrc +++ b/.zshrc @@ -24,9 +24,9 @@ export ZINIT_HOME_DIR="" - AUTO_SAVE_WORKING_DIR=true - AUTO_LOAD_LAST_WORKING_DIR=false - WORKING_DIR_SAVE_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/shell/last-dir.save" + WORKING_DIR_SAVE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/last-working-dir" + # make the dir for the file if needed + ! test -d "$(dirname "$WORKING_DIR_SAVE_FILE")" && mkdir -p "$(dirname "$WORKING_DIR_SAVE_FILE")" # (I know this shouldn't go here, but I needed it to happen early) # change cursor to beam by default @@ -79,6 +79,7 @@ # <<< ## Program improvements (ex: ls = ls -h) >>> + alias sudo="sudo " # allow using aliases in sudo commands alias df='df -h' # Human-readable sizes alias free='free -m' # Show sizes in MB alias bc="bc -ql" # Make bc usable for fast math @@ -88,7 +89,6 @@ ls="ls -hN --color=auto --group-directories-first" \ grep="grep --color=auto" \ diff="diff --color=auto" \ - ccat="highlight --out-format=ansi" \ pacman="pacman --color=auto" \ paru="paru --color=auto --sudoloop --newsonupgrade --pgpfetch --upgrademenu --bottomup --skipreview" \ @@ -228,36 +228,6 @@ fi } - # Loads the last working directory (or the one you're in) - alias lwd=load_working_dir - load_working_dir() { - # load the directory (and do validation to make sure it's actually a directory) - if test -f "$WORKING_DIR_SAVE_FILE"; then - local PREVIOUS_WORKING_DIR="$(cat "$WORKING_DIR_SAVE_FILE")" - if test -d "$(cat "$WORKING_DIR_SAVE_FILE")"; then - cd "$PREVIOUS_WORKING_DIR" || error $LINENO - else - echo "Invalid saved working directory ($PREVIOUS_WORKING_DIR)" - fi - else - echo "Invalid working directory save file ($WORKING_DIR_SAVE_FILE)" - fi - } - - # Saves the CWD to a file - alias swd=save_working_dir - save_working_dir() { - $SAVE_WORKING_DIR && pwd > "$WORKING_DIR_SAVE_FILE" - } - - # automatically save and resume working directory >>> - $AUTO_LOAD_LAST_WORKING_DIR && load_working_dir - if $AUTO_SAVE_WORKING_DIR; then - autoload -U add-zsh-hook - add-zsh-hook chpwd save_working_dir - fi - # <<< - # Simple extraction script. Taken from manjaro's .bashrc ex() { if [ -f $1 ] ; then @@ -503,7 +473,7 @@ [ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history" -## ZSH Options (opening this fold causes lag in vim for some reason) >>> +## ZSH Options >>> { # setopt correct # Auto correct mistakes setopt extendedglob # Extended globbing. Allows using regular expressions with * @@ -704,7 +674,29 @@ autoload -U add-zsh-hook add-zsh-hook precmd mzc_termsupport_precmd add-zsh-hook preexec mzc_termsupport_preexec # <<< -# allow `cd -` on loading zsh >>> +# auto save and load working dir (allow `cd -` on start) >>> +# Load the last working directory +load_working_dir() { + # load the directory (and do validation to make sure it's actually a directory) + if test -f "$WORKING_DIR_SAVE_FILE"; then + local PREVIOUS_WORKING_DIR="$(cat "$WORKING_DIR_SAVE_FILE")" + if test -d "$(cat "$WORKING_DIR_SAVE_FILE")"; then + cd "$PREVIOUS_WORKING_DIR" || error $LINENO + else + echo "Invalid saved working directory ($PREVIOUS_WORKING_DIR)" + fi + else + echo "Invalid working directory save file ($WORKING_DIR_SAVE_FILE)" + fi +} + +save_working_dir() { + pwd > "$WORKING_DIR_SAVE_FILE" +} + +autoload -U add-zsh-hook +add-zsh-hook chpwd save_working_dir + load_working_dir cd - > /dev/null # <<<