-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchpwd.sh
More file actions
38 lines (29 loc) · 816 Bytes
/
chpwd.sh
File metadata and controls
38 lines (29 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# create a PROPMT_COMMAND equivalent to store chpwd functions
typeset -g CHPWD_COMMAND=""
_chpwd_hook() {
shopt -s nullglob
local f
# run commands in CHPWD_COMMAND variable on dir change
if [[ "$PREVPWD" != "$PWD" ]]; then
local IFS=$';'
for f in $CHPWD_COMMAND; do
"$f"
done
unset IFS
fi
# refresh last working dir record
export PREVPWD="$PWD"
}
# add `;` after _chpwd_hook if PROMPT_COMMAND is not empty
PROMPT_COMMAND="_chpwd_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
### Usage
#```bash
## example 1: `ls` list directory once dir is changed
#_ls_on_cwd_change() {
# ls
#}
## append the command into CHPWD_COMMAND
#CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}_ls_on_cwd_change"
## or just use `ls` directly
#CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}ls"
#```