From 07d600c9ccf8a6491fb113a7d0d5f7ec160e62da Mon Sep 17 00:00:00 2001 From: chenyc Date: Mon, 7 Nov 2022 11:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=98=AF=E5=90=A6=E4=BD=BF=E7=94=A8hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 +++++++++++++++++++ config/hook.zsh | 19 +++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 02277b1..75fd4c0 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,25 @@ fork自 oh my zsh,更纯净 更快速 ~/.fzf/install ``` +## CONFIGURATION + +```plaintext +# 以下env如果需要请在source omz.zsh 前定义 + +_OMZ_APPLY_PREEXEC_HOOK + 是否允许OMZ开启"命令发生前"相关hook + 默认true + 行为: + 1. 执行命令前 将修改当前终端会话的title为: $cmd + +_OMZ_APPLY_CHPWD_HOOK + 是否允许OMZ开启"pwd变更后"相关hook + 默认true + 行为: + 1. 载入时,会自动切换到上一次cd过的目录 + 2. 发生cd命令时,自动记录当前cd的目录(在缓存文件中) +``` + ## PLUGINS 1. z.lua diff --git a/config/hook.zsh b/config/hook.zsh index 3c24d64..3b4a46c 100644 --- a/config/hook.zsh +++ b/config/hook.zsh @@ -1,9 +1,16 @@ # auto update the terminal name -preexec_hook() { _cmd=($(echo $2)); print -n "\e]2;${(q)_cmd[1]}\a"; } -add-zsh-hook -Uz preexec preexec_hook +_apply_preexec_hook() { + preexec_hook() { _cmd=($(echo $2)); print -n "\e]2;${(q)_cmd[1]}\a"; } + add-zsh-hook -Uz preexec preexec_hook +} # auto to last pwd -chpwd_hook() { echo $PWD > $OMZ/cache/currentdir } -add-zsh-hook -Uz chpwd chpwd_hook -currentdir=$(cat $OMZ/cache/currentdir 2>/dev/null) -[ -d "$currentdir" ] && cd $currentdir +_apply_chpwd_hook() { + chpwd_hook() { echo $PWD > $OMZ/cache/currentdir } + add-zsh-hook -Uz chpwd chpwd_hook + currentdir=$(cat $OMZ/cache/currentdir 2>/dev/null) + [ -d "$currentdir" ] && cd $currentdir +} + +[ "$_OMZ_APPLY_PREEXEC_HOOK" != "false" ] && _apply_preexec_hook +[ "$_OMZ_APPLY_CHPWD_HOOK" != "false" ] && _apply_chpwd_hook