mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-02-13 05:59:46 +08:00
Compare commits
No commits in common. "03a0d5bbaedc732436b5c67b166cde954817cc2f" and "33c0de7add12b050b94fd6f2f988e9b5547d172c" have entirely different histories.
03a0d5bbae
...
33c0de7add
@ -100,8 +100,8 @@ function detect-clipboard() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function clipcopy clippaste {
|
# Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
|
||||||
unfunction clipcopy clippaste
|
# which is not really an error. If the user calls them, they will attempt to redetect
|
||||||
detect-clipboard || true # let one retry
|
# (for example, perhaps the user has now installed xclip) and then either print an error
|
||||||
"$0" "$@"
|
# or proceed successfully.
|
||||||
}
|
detect-clipboard || true
|
||||||
|
|||||||
@ -1,9 +0,0 @@
|
|||||||
tap: false
|
|
||||||
directories:
|
|
||||||
tests: tests
|
|
||||||
output: tests/_output
|
|
||||||
support: tests/_support
|
|
||||||
time_limit: 0
|
|
||||||
fail_fast: false
|
|
||||||
allow_risky: false
|
|
||||||
verbose: true
|
|
||||||
@ -2,32 +2,45 @@
|
|||||||
|
|
||||||
This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
|
This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
To use it, add `alias-finder` to the `plugins` array of your zshrc file:
|
To use it, add `alias-finder` to the `plugins` array of your zshrc file:
|
||||||
```
|
```
|
||||||
plugins=(... alias-finder)
|
plugins=(... alias-finder)
|
||||||
```
|
```
|
||||||
|
|
||||||
To enable it for every single command, set zstyle in your `~/.zshrc`.
|
## 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.
|
||||||
|
|
||||||
```zsh
|
## Options
|
||||||
# ~/.zshrc
|
|
||||||
|
|
||||||
zstyle ':omz:plugins:alias-finder' autoload yes # disabled by default
|
- Use `--longer` or `-l` to allow the aliases to be longer than the input (match aliases if they contain the input).
|
||||||
zstyle ':omz:plugins:alias-finder' longer yes # disabled by default
|
- Use `--exact` or `-e` to avoid matching aliases that are shorter than the input.
|
||||||
zstyle ':omz:plugins:alias-finder' exact yes # disabled by default
|
|
||||||
zstyle ':omz:plugins:alias-finder' cheaper yes # disabled by default
|
## 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'
|
||||||
```
|
```
|
||||||
|
|
||||||
As you can see, options are also available with zstyle.
|
|
||||||
|
|
||||||
### Options
|
|
||||||
|
|
||||||
> In order to clarify, let's say `alias a=abc` has source 'abc' and destination 'a'.
|
|
||||||
|
|
||||||
- Use `--longer` or `-l` to include aliases where the source is longer than the input (in other words, the source could contain the whole input).
|
|
||||||
- Use `--exact` or `-e` to avoid aliases where the source is shorter than the input (in other words, the source must be the same with the input).
|
|
||||||
- Use `--cheaper` or `-c` to avoid aliases where the destination is longer than the input (in other words, the destination must be the shorter than the input).
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,59 +1,44 @@
|
|||||||
alias-finder() {
|
alias-finder() {
|
||||||
local cmd=" " exact="" longer="" cheaper="" wordEnd="'{0,1}$" finder="" filter=""
|
local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd=""
|
||||||
|
for i in $@; do
|
||||||
# build command and options
|
case $i in
|
||||||
for c in "$@"; do
|
|
||||||
case $c in
|
|
||||||
# TODO: Remove backward compatibility (other than zstyle form)
|
|
||||||
# set options if exist
|
|
||||||
-e|--exact) exact=true;;
|
-e|--exact) exact=true;;
|
||||||
-l|--longer) longer=true;;
|
-l|--longer) longer=true;;
|
||||||
-c|--cheaper) cheaper=true;;
|
*)
|
||||||
# concatenate cmd
|
if [[ -z $cmd ]]; then
|
||||||
*) cmd="$cmd$c " ;;
|
cmd=$i
|
||||||
|
else
|
||||||
|
cmd="$cmd $i"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep
|
||||||
zstyle -t ':omz:plugins:alias-finder' longer && longer=true
|
if (( $(wc -l <<< $cmd) == 1 )); then
|
||||||
zstyle -t ':omz:plugins:alias-finder' exact && exact=true
|
|
||||||
zstyle -t ':omz:plugins:alias-finder' cheaper && cheaper=true
|
|
||||||
|
|
||||||
# format cmd for grep
|
|
||||||
## - replace newlines with spaces
|
|
||||||
## - trim both ends
|
|
||||||
## - replace multiple spaces with one space
|
|
||||||
## - add escaping character to special characters
|
|
||||||
cmd=$(echo -n "$cmd" | tr '\n' ' ' | xargs | tr -s '[:space:]' | sed 's/[].\|$(){}?+*^[]/\\&/g')
|
|
||||||
|
|
||||||
if [[ $longer == true ]]; then
|
|
||||||
wordEnd="" # remove wordEnd to find longer aliases
|
|
||||||
fi
|
|
||||||
|
|
||||||
# find with alias and grep, removing last word each time until no more words
|
|
||||||
while [[ $cmd != "" ]]; do
|
while [[ $cmd != "" ]]; do
|
||||||
finder="'{0,1}$cmd$wordEnd"
|
if [[ $longer = true ]]; then
|
||||||
|
wordStart="'{0,1}"
|
||||||
# make filter to find only shorter results than current cmd
|
else
|
||||||
if [[ $cheaper == true ]]; then
|
wordEnd="$"
|
||||||
cmdLen=$(echo -n "$cmd" | wc -c)
|
multiWordEnd="'$"
|
||||||
filter="^'{0,1}.{0,$((cmdLen - 1))}="
|
|
||||||
fi
|
fi
|
||||||
|
if [[ $cmd == *" "* ]]; then
|
||||||
alias | grep -E "$filter" | grep -E "=$finder"
|
local finder="'$cmd$multiWordEnd"
|
||||||
|
else
|
||||||
if [[ $exact == true ]]; then
|
local finder=$wordStart$cmd$wordEnd
|
||||||
break # because exact case is only one
|
fi
|
||||||
elif [[ $longer = true ]]; then
|
alias | grep -E "=$finder"
|
||||||
break # because above grep command already found every longer aliases during first cycle
|
if [[ $exact = true || $longer = true ]]; then
|
||||||
|
break
|
||||||
|
else
|
||||||
|
cmd=$(sed -E 's/ {0,1}[^ ]*$//' <<< $cmd) # removes last word
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cmd=$(sed -E 's/ {0,}[^ ]*$//' <<< "$cmd") # remove last word
|
|
||||||
done
|
done
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
preexec_alias-finder() {
|
preexec_alias-finder() {
|
||||||
# TODO: Remove backward compatibility (other than zstyle form)
|
if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
|
||||||
zstyle -t ':omz:plugins:alias-finder' autoload && alias-finder $1 || if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
|
|
||||||
alias-finder $1
|
alias-finder $1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
#!/usr/bin/env zsh
|
|
||||||
# Write your bootstrap code here
|
|
||||||
@ -1,107 +0,0 @@
|
|||||||
#!/usr/bin/env zunit
|
|
||||||
|
|
||||||
@setup {
|
|
||||||
load ../alias-finder.plugin.zsh
|
|
||||||
|
|
||||||
set_git_aliases() {
|
|
||||||
unalias -a # all
|
|
||||||
alias g="git"
|
|
||||||
alias gc="git commit"
|
|
||||||
alias gcv="git commit -v"
|
|
||||||
alias gcvs="git commit -v -S"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find aliases that contain input' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder "git"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 1
|
|
||||||
assert "${lines[1]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find aliases that contain input with whitespaces at ends' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder " git "
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 1
|
|
||||||
assert "${lines[1]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find aliases that contain multiple words' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder "git commit -v"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 3
|
|
||||||
assert "${lines[1]}" same_as "gcv='git commit -v'"
|
|
||||||
assert "${lines[2]}" same_as "gc='git commit'"
|
|
||||||
assert "${lines[3]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find alias that is the same with input when --exact option is set' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder -e "git"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 1
|
|
||||||
assert "${lines[1]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find alias that is the same with multiple words input when --exact option is set' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder -e "git commit -v"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 1
|
|
||||||
assert "${lines[1]}" same_as "gcv='git commit -v'"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find alias that is the same with or longer than input when --longer option is set' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder -l "git"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 4
|
|
||||||
assert "${lines[1]}" same_as "g=git"
|
|
||||||
assert "${lines[2]}" same_as "gc='git commit'"
|
|
||||||
assert "${lines[3]}" same_as "gcv='git commit -v'"
|
|
||||||
assert "${lines[4]}" same_as "gcvs='git commit -v -S'"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find alias that is the same with or longer than multiple words input when --longer option is set' {
|
|
||||||
set_git_aliases
|
|
||||||
|
|
||||||
run alias-finder -l "git commit -v"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 2
|
|
||||||
assert "${lines[1]}" same_as "gcv='git commit -v'"
|
|
||||||
assert "${lines[2]}" same_as "gcvs='git commit -v -S'"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find aliases including expensive (longer) than input' {
|
|
||||||
set_git_aliases
|
|
||||||
alias expensiveCommands="git commit"
|
|
||||||
|
|
||||||
run alias-finder "git commit -v"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 4
|
|
||||||
assert "${lines[1]}" same_as "gcv='git commit -v'"
|
|
||||||
assert "${lines[2]}" same_as "expensiveCommands='git commit'"
|
|
||||||
assert "${lines[3]}" same_as "gc='git commit'"
|
|
||||||
assert "${lines[4]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
|
|
||||||
@test 'find aliases excluding expensive (longer) than input when --cheap option is set' {
|
|
||||||
set_git_aliases
|
|
||||||
alias expensiveCommands="git commit"
|
|
||||||
|
|
||||||
run alias-finder -c "git commit -v"
|
|
||||||
|
|
||||||
assert "${#lines[@]}" equals 3
|
|
||||||
assert "${lines[1]}" same_as "gcv='git commit -v'"
|
|
||||||
assert "${lines[2]}" same_as "gc='git commit'"
|
|
||||||
assert "${lines[3]}" same_as "g=git"
|
|
||||||
}
|
|
||||||
@ -40,12 +40,12 @@ plugins=(... git)
|
|||||||
| gbd | git branch --delete |
|
| gbd | git branch --delete |
|
||||||
| gbD | git branch --delete --force |
|
| gbD | git branch --delete --force |
|
||||||
| gbda | git branch --no-color --merged | grep -vE "^([+]|\s($(git_main_branch)|$(git_develop_branch))\s\*$)" | xargs git branch --delete 2>/dev/null |
|
| gbda | git branch --no-color --merged | grep -vE "^([+]|\s($(git_main_branch)|$(git_develop_branch))\s\*$)" | xargs git branch --delete 2>/dev/null |
|
||||||
| gbgd | LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -d |
|
| gbgd | git branch --no-color -vv |
|
||||||
| gbgD | LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -D |
|
| gbgD | git branch --no-color -vv |
|
||||||
| gbnm | git branch --no-merged |
|
| gbnm | git branch --no-merged |
|
||||||
| gbr | git branch --remote |
|
| gbr | git branch --remote |
|
||||||
| ggsup | git branch --set-upstream-to=origin/$(git_current_branch) |
|
| ggsup | git branch --set-upstream-to=origin/$(git_current_branch) |
|
||||||
| gbg | LANG=C git branch -vv | grep ": gone\]" |
|
| gbg | git branch -vv |
|
||||||
| gco | git checkout |
|
| gco | git checkout |
|
||||||
| gcor | git checkout --recurse-submodules |
|
| gcor | git checkout --recurse-submodules |
|
||||||
| gcb | git checkout -b |
|
| gcb | git checkout -b |
|
||||||
|
|||||||
@ -121,12 +121,12 @@ alias gba='git branch --all'
|
|||||||
alias gbd='git branch --delete'
|
alias gbd='git branch --delete'
|
||||||
alias gbD='git branch --delete --force'
|
alias gbD='git branch --delete --force'
|
||||||
alias gbda='git branch --no-color --merged | command grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | command xargs git branch --delete 2>/dev/null'
|
alias gbda='git branch --no-color --merged | command grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | command xargs git branch --delete 2>/dev/null'
|
||||||
alias gbgd='LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -d'
|
alias gbgd='git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -d'
|
||||||
alias gbgD='LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -D'
|
alias gbgD='git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -D'
|
||||||
alias gbnm='git branch --no-merged'
|
alias gbnm='git branch --no-merged'
|
||||||
alias gbr='git branch --remote'
|
alias gbr='git branch --remote'
|
||||||
alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
|
alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
|
||||||
alias gbg='LANG=C git branch -vv | grep ": gone\]"'
|
alias gbg='git branch -vv | grep ": gone\]"'
|
||||||
alias gco='git checkout'
|
alias gco='git checkout'
|
||||||
alias gcor='git checkout --recurse-submodules'
|
alias gcor='git checkout --recurse-submodules'
|
||||||
alias gcb='git checkout -b'
|
alias gcb='git checkout -b'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user