zsh: modified auto cd functionality (now, off by default, added aliases
lwd and swd, too)
This commit is contained in:
parent
b0838e8322
commit
1b5f8d6a80
1 changed files with 37 additions and 25 deletions
58
.zshrc
58
.zshrc
|
@ -24,7 +24,8 @@
|
||||||
|
|
||||||
export ZINIT_HOME_DIR=""
|
export ZINIT_HOME_DIR=""
|
||||||
|
|
||||||
SAVE_WORKING_DIR=true
|
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_CONFIG_HOME:-$HOME/.config}/shell/last-dir.save"
|
||||||
|
|
||||||
# (I know this shouldn't go here, but I needed it to happen early)
|
# (I know this shouldn't go here, but I needed it to happen early)
|
||||||
|
@ -215,6 +216,36 @@
|
||||||
fi
|
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
|
# Simple extraction script. Taken from manjaro's .bashrc
|
||||||
ex() {
|
ex() {
|
||||||
if [ -f $1 ] ; then
|
if [ -f $1 ] ; then
|
||||||
|
@ -250,10 +281,11 @@
|
||||||
echo "$FILEPATH" | grep -o 'site.*' | sed 's+^site+https://blakenorth.net+g'
|
echo "$FILEPATH" | grep -o 'site.*' | sed 's+^site+https://blakenorth.net+g'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# for sending error messages from functions and whatnot
|
||||||
error() {
|
error() {
|
||||||
ERROR_CODE="$?"
|
ERROR_CODE="$?"
|
||||||
>&2 echo "An error occurred within a function in the .zshrc on line number ${1}"
|
>&2 echo "An error occurred within a function in the .zshrc on line number ${1}"
|
||||||
exit $ERROR_CODE
|
return $ERROR_CODE
|
||||||
}
|
}
|
||||||
|
|
||||||
# prcolors(): Display all colors in a few different ways >>>
|
# prcolors(): Display all colors in a few different ways >>>
|
||||||
|
@ -374,6 +406,7 @@
|
||||||
ls --color=auto
|
ls --color=auto
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# believe it or not, zsh makes for a fine calculator
|
||||||
calc() {
|
calc() {
|
||||||
echo $(($*))
|
echo $(($*))
|
||||||
}
|
}
|
||||||
|
@ -543,27 +576,6 @@ bindkey -M vicmd 'k' history-substring-search-up
|
||||||
bindkey -M vicmd 'j' history-substring-search-down
|
bindkey -M vicmd 'j' history-substring-search-down
|
||||||
# <<<
|
# <<<
|
||||||
|
|
||||||
# automatically save and resume working directory >>>
|
|
||||||
if $SAVE_WORKING_DIR; then
|
|
||||||
# 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() {
|
|
||||||
$SAVE_WORKING_DIR && pwd > "$WORKING_DIR_SAVE_FILE"
|
|
||||||
}
|
|
||||||
autoload -U add-zsh-hook
|
|
||||||
add-zsh-hook chpwd save_working_dir
|
|
||||||
fi
|
|
||||||
# <<<
|
|
||||||
# Colors and compinit >>>
|
# Colors and compinit >>>
|
||||||
export LESS_TERMCAP_mb=$'\E[01;32m'
|
export LESS_TERMCAP_mb=$'\E[01;32m'
|
||||||
export LESS_TERMCAP_md=$'\E[01;32m'
|
export LESS_TERMCAP_md=$'\E[01;32m'
|
||||||
|
|
Loading…
Add table
Reference in a new issue