From 90a0de4698d8ec4300bf6aa3d6847e299a05b1f5 Mon Sep 17 00:00:00 2001 From: SomeDer <48731521+SomeDer@users.noreply.github.com> Date: Mon, 19 Aug 2019 16:53:13 +0100 Subject: Add alias-finder plugin (#7768) --- plugins/alias-finder/README.md | 46 ++++++++++++++++++++++++++++ plugins/alias-finder/alias-finder.plugin.zsh | 46 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 plugins/alias-finder/README.md create mode 100644 plugins/alias-finder/alias-finder.plugin.zsh (limited to 'plugins/alias-finder') diff --git a/plugins/alias-finder/README.md b/plugins/alias-finder/README.md new file mode 100644 index 000000000..409f4b653 --- /dev/null +++ b/plugins/alias-finder/README.md @@ -0,0 +1,46 @@ +# alias-finder plugin + +This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier. + +To use it, add `alias-finder` to the `plugins` array of your zshrc file: +``` +plugins=(... alias-finder) +``` + +## Usage +To see if there is an alias defined for the command, pass it as an argument to `alias-finder`. This can also run automatically before each command you input - add `ZSH_ALIAS_FINDER_AUTOMATIC=true` to your zshrc if you want this. + +## Options + +- Use `--longer` or `-l` to allow the aliases to be longer than the input (match aliases if they contain the input). +- Use `--exact` or `-e` to avoid matching aliases that are shorter than the input. + +## Examples +``` +$ alias-finder "git pull" +gl='git pull' +g=git +``` +``` +$ alias-finder "web_search google oh my zsh" +google='web_search google' +``` +``` +$ alias-finder "git commit -v" +gc="git commit -v" +g=git +``` +``` +$ alias-finder -e "git commit -v" +gc='git commit -v' +``` +``` +$ alias-finder -l "git commit -v" +gc='git commit -v' +'gc!'='git commit -v --amend' +gca='git commit -v -a' +'gca!'='git commit -v -a --amend' +'gcan!'='git commit -v -a --no-edit --amend' +'gcans!'='git commit -v -a -s --no-edit --amend' +'gcn!'='git commit -v --no-edit --amend' +``` diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh new file mode 100644 index 000000000..2bfaad597 --- /dev/null +++ b/plugins/alias-finder/alias-finder.plugin.zsh @@ -0,0 +1,46 @@ +alias-finder() { + local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd="" + for i in $@; do + case $i in + -e|--exact) exact=true;; + -l|--longer) longer=true;; + *) + if [[ -z $cmd ]]; then + cmd=$i + else + cmd="$cmd $i" + fi + ;; + esac + done + cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep + if [[ $(wc -l <<< $cmd) == 1 ]]; then + while [[ $cmd != "" ]]; do + if [[ $longer = true ]]; then + wordStart="'{0,1}" + else + wordEnd="$" + multiWordEnd="'$" + fi + if [[ $cmd == *" "* ]]; then + local finder="'$cmd$multiWordEnd" + else + local finder=$wordStart$cmd$wordEnd + fi + alias | grep -E "=$finder" + if [[ $exact = true || $longer = true ]]; then + break + else + cmd=$(sed -E 's/ {0,1}[^ ]*$//' <<< $cmd) # removes last word + fi + done + fi +} + +preexec_alias-finder() { + if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then + alias-finder $1 + fi +} + +preexec_functions+=(preexec_alias-finder) -- cgit v1.2.3-70-g09d2 From e363109a6d3367d8be1dd66f05a38eb38b4257d7 Mon Sep 17 00:00:00 2001 From: Vice Versa <0x7c48@gmail.com> Date: Sun, 27 Oct 2019 18:36:00 +0200 Subject: alias-finder: fix wc numeric conditional (#8251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Never use `[[` for numeric comparisons, for that, we’ll use `((`. --- plugins/alias-finder/alias-finder.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/alias-finder') diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh index 2bfaad597..6b8fa66ce 100644 --- a/plugins/alias-finder/alias-finder.plugin.zsh +++ b/plugins/alias-finder/alias-finder.plugin.zsh @@ -14,7 +14,7 @@ alias-finder() { esac done cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep - if [[ $(wc -l <<< $cmd) == 1 ]]; then + if (( $(wc -l <<< $cmd) == 1 )); then while [[ $cmd != "" ]]; do if [[ $longer = true ]]; then wordStart="'{0,1}" -- cgit v1.2.3-70-g09d2 From 1ba0af650ac575a7d35b10146d2e7a7b3b2e2ae6 Mon Sep 17 00:00:00 2001 From: Jacob Tomaw Date: Tue, 19 Nov 2019 12:47:12 -0500 Subject: Use safer append to hook function arrays (#8406) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use add-zsh-hook to add functions to hooks. That way they won't be added again when doing `source ~/.zshrc` multiple times. Co-authored-by: Marc Cornellà --- .gitignore | 1 + lib/termsupport.zsh | 7 ++++--- plugins/alias-finder/alias-finder.plugin.zsh | 5 +++-- plugins/dirhistory/dirhistory.plugin.zsh | 3 ++- plugins/dirpersist/dirpersist.plugin.zsh | 3 ++- plugins/git-prompt/git-prompt.plugin.zsh | 7 ++++--- plugins/last-working-dir/last-working-dir.plugin.zsh | 3 ++- plugins/pipenv/pipenv.plugin.zsh | 3 ++- plugins/timer/timer.plugin.zsh | 5 +++-- plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh | 5 ++--- themes/pygmalion-virtualenv.zsh-theme | 5 ++--- themes/pygmalion.zsh-theme | 5 ++--- 12 files changed, 29 insertions(+), 23 deletions(-) (limited to 'plugins/alias-finder') diff --git a/.gitignore b/.gitignore index 87a79cdae..251c9dc9f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ custom/ # temp files directories cache/ log/ +*.swp diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index aa14f3f07..f5e367fcb 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -75,8 +75,9 @@ function omz_termsupport_preexec { title '$CMD' '%100>...>$LINE%<<' } -precmd_functions+=(omz_termsupport_precmd) -preexec_functions+=(omz_termsupport_preexec) +autoload -U add-zsh-hook +add-zsh-hook precmd omz_termsupport_precmd +add-zsh-hook preexec omz_termsupport_preexec # Keep Apple Terminal.app's current working directory updated @@ -99,7 +100,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then } # Use a precmd hook instead of a chpwd hook to avoid contaminating output - precmd_functions+=(update_terminalapp_cwd) + add-zsh-hook precmd update_terminalapp_cwd # Run once to get initial cwd set update_terminalapp_cwd fi diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh index 6b8fa66ce..caee9b5a3 100644 --- a/plugins/alias-finder/alias-finder.plugin.zsh +++ b/plugins/alias-finder/alias-finder.plugin.zsh @@ -4,7 +4,7 @@ alias-finder() { case $i in -e|--exact) exact=true;; -l|--longer) longer=true;; - *) + *) if [[ -z $cmd ]]; then cmd=$i else @@ -43,4 +43,5 @@ preexec_alias-finder() { fi } -preexec_functions+=(preexec_alias-finder) +autoload -U add-zsh-hook +add-zsh-hook preexec preexec_alias-finder diff --git a/plugins/dirhistory/dirhistory.plugin.zsh b/plugins/dirhistory/dirhistory.plugin.zsh index 239915e48..35c43d76a 100644 --- a/plugins/dirhistory/dirhistory.plugin.zsh +++ b/plugins/dirhistory/dirhistory.plugin.zsh @@ -53,7 +53,8 @@ function push_future() { } # Called by zsh when directory changes -chpwd_functions+=(chpwd_dirhistory) +autoload -U add-zsh-hook +add-zsh-hook chpwd chpwd_dirhistory function chpwd_dirhistory() { push_past $PWD # If DIRHISTORY_CD is not set... diff --git a/plugins/dirpersist/dirpersist.plugin.zsh b/plugins/dirpersist/dirpersist.plugin.zsh index 616e2c3c6..daadc3850 100644 --- a/plugins/dirpersist/dirpersist.plugin.zsh +++ b/plugins/dirpersist/dirpersist.plugin.zsh @@ -11,7 +11,8 @@ if [[ -f ${dirstack_file} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then [[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD fi -chpwd_functions+=(chpwd_dirpersist) +autoload -U add-zsh-hook +add-zsh-hook chpwd chpwd_dirpersist chpwd_dirpersist() { if (( $DIRSTACKSIZE <= 0 )) || [[ -z $dirstack_file ]]; then return; fi local -ax my_stack diff --git a/plugins/git-prompt/git-prompt.plugin.zsh b/plugins/git-prompt/git-prompt.plugin.zsh index 76ac2e62b..da674af98 100644 --- a/plugins/git-prompt/git-prompt.plugin.zsh +++ b/plugins/git-prompt/git-prompt.plugin.zsh @@ -20,9 +20,10 @@ function precmd_update_git_vars() { fi } -chpwd_functions+=(chpwd_update_git_vars) -precmd_functions+=(precmd_update_git_vars) -preexec_functions+=(preexec_update_git_vars) +autoload -U add-zsh-hook +add-zsh-hook chpwd chpwd_update_git_vars +add-zsh-hook precmd precmd_update_git_vars +add-zsh-hook preexec preexec_update_git_vars ## Function definitions diff --git a/plugins/last-working-dir/last-working-dir.plugin.zsh b/plugins/last-working-dir/last-working-dir.plugin.zsh index 53bb19e46..fd21705ae 100644 --- a/plugins/last-working-dir/last-working-dir.plugin.zsh +++ b/plugins/last-working-dir/last-working-dir.plugin.zsh @@ -2,7 +2,8 @@ typeset -g ZSH_LAST_WORKING_DIRECTORY # Updates the last directory once directory is changed -chpwd_functions+=(chpwd_last_working_dir) +autoload -U add-zsh-hook +add-zsh-hook chpwd chpwd_last_working_dir chpwd_last_working_dir() { if [ "$ZSH_SUBSHELL" = 0 ]; then local cache_file="$ZSH_CACHE_DIR/last-working-dir" diff --git a/plugins/pipenv/pipenv.plugin.zsh b/plugins/pipenv/pipenv.plugin.zsh index 0a5dc56a4..ec41c3e02 100644 --- a/plugins/pipenv/pipenv.plugin.zsh +++ b/plugins/pipenv/pipenv.plugin.zsh @@ -23,7 +23,8 @@ _togglePipenvShell() { fi fi } -chpwd_functions+=(_togglePipenvShell) +autoload -U add-zsh-hook +add-zsh-hook chpwd _togglePipenvShell # Aliases alias pch="pipenv check" diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh index 231134e7d..728377c5c 100644 --- a/plugins/timer/timer.plugin.zsh +++ b/plugins/timer/timer.plugin.zsh @@ -25,5 +25,6 @@ __timer_display_timer_precmd() { fi } -preexec_functions+=(__timer_save_time_preexec) -precmd_functions+=(__timer_display_timer_precmd) +autoload -U add-zsh-hook +add-zsh-hook preexec __timer_save_time_preexec +add-zsh-hook precmd __timer_display_timer_precmd diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index e27c6bb76..2a4b43189 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -96,7 +96,6 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then # Append workon_cwd to the chpwd_functions array, so it will be called on cd # http://zsh.sourceforge.net/Doc/Release/Functions.html - if ! (( $chpwd_functions[(I)workon_cwd] )); then - chpwd_functions+=(workon_cwd) - fi + autoload -U add-zsh-hook + add-zsh-hook chpwd workon_cwd fi diff --git a/themes/pygmalion-virtualenv.zsh-theme b/themes/pygmalion-virtualenv.zsh-theme index ea28e125a..605e3d10c 100644 --- a/themes/pygmalion-virtualenv.zsh-theme +++ b/themes/pygmalion-virtualenv.zsh-theme @@ -28,7 +28,8 @@ prompt_setup_pygmalion(){ base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g") post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g") - precmd_functions+=(prompt_pygmalion_precmd) + autoload -U add-zsh-hook + add-zsh-hook precmd prompt_pygmalion_precmd } prompt_pygmalion_precmd(){ @@ -46,5 +47,3 @@ prompt_pygmalion_precmd(){ } prompt_setup_pygmalion - - diff --git a/themes/pygmalion.zsh-theme b/themes/pygmalion.zsh-theme index 5f5fe7f9a..cd773e4a4 100644 --- a/themes/pygmalion.zsh-theme +++ b/themes/pygmalion.zsh-theme @@ -12,7 +12,8 @@ prompt_setup_pygmalion(){ base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g") post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g") - precmd_functions+=(prompt_pygmalion_precmd) + autoload -U add-zsh-hook + add-zsh-hook precmd prompt_pygmalion_precmd } prompt_pygmalion_precmd(){ @@ -30,5 +31,3 @@ prompt_pygmalion_precmd(){ } prompt_setup_pygmalion - - -- cgit v1.2.3-70-g09d2