Skip to content

Commit

Permalink
feat(add): support forcing add over system command
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed May 9, 2020
1 parent d0aeaaf commit 57eb4a8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
13 changes: 6 additions & 7 deletions README.md
Expand Up @@ -161,7 +161,7 @@ Default is regular.
### Options

```shell
[(--add | -a )] [(--session | -S) | (--user | -U)] [(--global | -g)] [--dry-run] [--quiet] arg
[(--add | -a )] [(--session | -S) | (--user | -U)] [(--global | -g)] [--dry-run] [--quiet] [--force] arg
| (--clear-session | -c) [--quiet]
| (--erase | -e ) [(--session | -S) | (--user | -U)] [(--global | -g)] [--dry-run] [--quiet] arg
| (--expand | -x) arg
Expand All @@ -183,7 +183,7 @@ zsh-abbr has options to add, rename, and erase abbreviations; to add abbreviatio
#### Add

```shell
abbr [(--add | -a)] [(--session | -S) | (--user | -U)] [(--global | -g)] [--dry-run] [--quiet] ABBREVIATION=EXPANSION
abbr [(--add | -a)] [(--session | -S) | (--user | -U)] [(--global | -g)] [--dry-run] [--quiet] [--force] ABBREVIATION=EXPANSION
```

Add a new abbreviation.
Expand Down Expand Up @@ -219,17 +219,16 @@ abbr a=b\;c # allowed
abbr a="b|c" # allowed
```

```shell
abbr a='a"b"c' # bad - will turn into `"a"b"c"`, which will error
abbr a='a\"b\"c' # good
```

User-scope abbreviations can also be manually to the user abbreviations file. See **Storage** below.

The session regular, session global, user regular, and user global abbreviation sets are independent. If you wanted, you could have more than one abbreviation with the same ABBREVIATION. Order of precedence is "session command > user command > session global > user global".

Use `--dry-run` to see what would result, without making any actual changes.

Will error rather than overwrite an existing abbreviation.

Will warn if the abbreviation would replace an existing command. To add in spite of the warning, use `--force`.

#### Clear Sessions

```shell
Expand Down
39 changes: 22 additions & 17 deletions man/abbr.txt
Expand Up @@ -6,7 +6,7 @@ NAME
zsh-abbr - manage zsh abbreviations

SYNOPSIS
abbr (--add | -a) [scope] <abbreviation> <expansion>
abbr (--add | -a) [scope] [--force] <abbreviation> <expansion>

abbr (--clear-session | -c)

Expand All @@ -24,9 +24,9 @@ SYNOPSIS

abbr --list

abbr --list-abbreviations | -l
abbr (--list-abbreviations | -l)

abbr --list-commands | -L | --show | -s
abbr (--list-commands | -L | --show | -s)

abbr (--rename | -R) [scope] <old_abbreviation> <new_abbreviation>

Expand Down Expand Up @@ -55,6 +55,11 @@ Options
Adds a new abbreviation, causing abbreviation to be expanded as
expansion.

Will error rather than overwrite an existing abbreviation.

Will warn if the abbreviation would replace an existing command.
To add in spite of the warning, use [--force].


o --clear-session or -c

Expand All @@ -74,7 +79,7 @@ Options
o --export-aliases [<destination>]

Exports a list of alias command for user abbreviations, suitable
for pasting or piping to whereever you keep aliases. If a <des-
for pasting or piping to whereever you keep aliases. If a <des-
tination> is provided, the commands will be appended to it.


Expand All @@ -90,13 +95,13 @@ Options

o --import-git-aliases

Adds two abbreviations for each git alias: a regular abbrevia-
Adds two abbreviations for each git alias: a regular abbrevia-
tion, and a "g"-prefixed global abbreviation.


o abbr --list

Lists the available abbreviations without their expansions.
Lists the available abbreviations without their expansions.
Equivalent to fish's `abbr --list`.


Expand All @@ -107,14 +112,14 @@ Options

o --list-commands or -L (or --show or -s as in fish)

Lists all abbreviations as commands suitable for export and
Lists all abbreviations as commands suitable for export and
import.


o --rename old_abbreviation new_abbreviation or -R old_abbrevia-
o --rename old_abbreviation new_abbreviation or -R old_abbrevia-
tion new_abbreviation

Renames an abbreviation, from old_abbreviation to new_abbrevia-
Renames an abbreviation, from old_abbreviation to new_abbrevia-
tion.


Expand All @@ -132,7 +137,7 @@ Options

o --user or -U

Abbreviations available to all current and future ses-
Abbreviations available to all current and future ses-
sions.


Expand All @@ -142,18 +147,18 @@ Options

o --global or -g

Abbreviation will expand anywhere on a line, rather than
Abbreviation will expand anywhere on a line, rather than
only in command (first word) position.


All except for --clear-session, --expand, --export-aliases, --list-
All except for --clear-session, --expand, --export-aliases, --list-
abbreviations, and --list-commands can be tried without making changes:


o --dry-run Show whats the result of the command would be.
o --dry-run Show whats the result of the command would be.


All except for --clear-session, --expand, --export-aliases, --list-
All except for --clear-session, --expand, --export-aliases, --list-
abbreviations, and --list-commands can be run with reduced output:


Expand All @@ -166,7 +171,7 @@ Options
EXAMPLES
abbr gco="git checkout"

"gco" will be expanded as "git checkout" when it is the first
"gco" will be expanded as "git checkout" when it is the first
word in the command, in all open and future sessions.


Expand All @@ -184,9 +189,9 @@ EXAMPLES

abbr -e -S -g gco;

Erase the global session abbreviation "gco". Note that because
Erase the global session abbreviation "gco". Note that because
expansion is triggered by [SPACE] and [ENTER], the semicolon (;)
is necessary to prevent expansion when operating on global
is necessary to prevent expansion when operating on global
abbreviations.


Expand Down
10 changes: 7 additions & 3 deletions man/man1/abbr.1
Expand Up @@ -3,7 +3,7 @@
zsh\-abbr \- manage zsh abbreviations
.SH SYNOPSIS

\fBabbr\fR (\fB\-\-add\fR | \fB\-a\fR) [\fIscope\fR] <\fIabbreviation\fR> <\fIexpansion\fR>
\fBabbr\fR (\fB\-\-add\fR | \fB\-a\fR) [\fIscope\fR] [\fI\-\-force\fR] <\fIabbreviation\fR> <\fIexpansion\fR>

\fBabbr\fR (\fB\-\-clear\-session\fR | \fB\-c\fR)

Expand All @@ -21,9 +21,9 @@ zsh\-abbr \- manage zsh abbreviations

\fBabbr \-\-list\fR

\fBabbr \-\-list\-abbreviations\fR | \fB\-l\fR
\fBabbr (\-\-list\-abbreviations\fR | \fB\-l\fR)

\fBabbr\fR \fB\-\-list\-commands\fR | \fB\-L\fR | \fB\-\-show\fR | \fB\-s\fR
\fBabbr\fR (\fB\-\-list\-commands\fR | \fB\-L\fR | \fB\-\-show\fR | \fB\-s\fR)

\fBabbr\fR (\fB\-\-rename\fR | \fB\-R\fR) [\fIscope\fR] <\fIold_abbreviation\fR> <\fInew_abbreviation\fR>

Expand All @@ -46,6 +46,10 @@ The following options are available:

Adds a new abbreviation, causing \fIabbreviation\fR to be expanded as \fIexpansion\fR.

Will error rather than overwrite an existing abbreviation.

Will warn if the abbreviation would replace an existing command. To add in spite of the warning, use [\fI\-\-force\fR].

.IP \(bu
\fB\-\-clear\-session\fR or \fB\-c\fR

Expand Down
21 changes: 17 additions & 4 deletions zsh-abbr.zsh
Expand Up @@ -20,9 +20,11 @@ ZSH_ABBR_USER_PATH=${ZSH_ABBR_USER_PATH=${HOME}/.config/zsh/abbreviations}

_zsh_abbr() {
{
local action dry_run has_error number_opts opt logs output quiet \
release_date scope should_exit text_bold text_reset type version
local action dry_run force has_error number_opts opt logs output \
quiet release_date scope should_exit text_bold text_reset \
type version
dry_run=${ZSH_ABBR_DRY_RUN:-0}
force=${ZSH_ABBR_FORCE:-0}
number_opts=0
quiet=${ZSH_ABBR_QUIET:-0}
release_date="April 22 2020"
Expand Down Expand Up @@ -397,9 +399,15 @@ _zsh_abbr() {
return
fi

# Warn if abbreviation would interfere with system command use, e.g. `cp="git cherry-pick"`
# Apply force to add regardless
if [[ $cmd && ${cmd:0:6} != 'alias ' ]]; then
_zsh_abbr:util_warn "The alias \`$abbreviation\` was not added because a command with the same name exists"
return
if (( force )); then
_zsh_abbr:util_warn "\`$abbreviation\` will now expand as an abbreviation"
else
_zsh_abbr:util_warn "The alias \`$abbreviation\` was not added because a command with the same name exists"
return
fi
fi

if [[ $scope == 'session' ]]; then
Expand Down Expand Up @@ -674,6 +682,11 @@ _zsh_abbr() {
"--export-aliases")
_zsh_abbr:util_set_once "action" "export_aliases"
;;
"--force"|\
"-f")
force=1
((number_opts++))
;;
"--global"|\
"-g")
_zsh_abbr:util_set_once "type" "global"
Expand Down

0 comments on commit 57eb4a8

Please sign in to comment.