abbr.fish: add abbr, a command to manipulate abbreviations

Work on #731.
This commit is contained in:
David Adam 2014-10-05 12:17:46 +08:00
parent 6a0931aeae
commit 501c3d5518
3 changed files with 117 additions and 0 deletions

44
doc_src/abbr.txt Normal file
View file

@ -0,0 +1,44 @@
\section abbr abbr - manage fish abbreviations
\subsection abbr-synopsis Synopsis
\fish{synopsis}
abbr -a word="phrase"
abbr -s
abbr -l
abbr -r word
\endfish
\subsection abbr-description Description
`abbr` manipulates the list of abbreviations that fish will expand.
Abbreviations are user-defined character sequences or words that are replaced with longer phrases after tehy are entered. For example, a frequently-run command such as `git checkout` can be abbreviated to `gco`. After entering `gco` and pressing @key{Space} or @key{Enter}, the full text `git checkout` will appear in the command line.
Abbreviations are stored, by default, in a universal variable.
The following parameters are available:
- `-a 'WORD=PHRASE'` or `--add 'WORD=PHRASE'` Adds a new abbreviation, where WORD will be expanded to PHRASE. Only a single argument is supported - protect the contents with quotes or escapes.
- `-s` or `--show` Show all abbreviated words and their expanded phrases in a manner suitable for export and import.
- `-l` or `--list` Lists all abbreviated words.
- `-r WORD` or `--remove WORD` Remove the abbreviation WORD.
\subsection abbr-example Examples
\fish
abbr -a 'gco=git checkout'
\endfish
Add a new abbreviation where `gco` will be replaced with `git checkout`.
\fish
abbr -r gco
\endfish
Remove the `gco` abbreviation.
\fish
ssh another_host abbr -s | source
\endfish
Import the abbreviations defined on another_host over SSH.

View file

@ -0,0 +1,5 @@
complete -c abbr -f -s a -l add -d 'Add abbreviation'
complete -c abbr -s r -l remove -d 'Remove abbreviation' -xa '(abbr -l)'
complete -c abbr -f -s s -l show -d 'Print all abbreviations'
complete -c abbr -f -s l -l list -d 'Print all abbreviation names'
complete -c abbr -f -s h -l help -d 'Help'

68
share/functions/abbr.fish Normal file
View file

@ -0,0 +1,68 @@
function abbr --description "Manage abbreviations"
if test (count $argv) -lt 1 -o (count $argv) -gt 2
printf ( _ "%s: Expected one or two arguments, got %d\n") abbr (count $argv)
__fish_print_help abbr
return 1
end
switch $argv[1]
case '-a' '--add'
if __fish_abbr_get_by_key "$argv[2]" >/dev/null
printf ( _ "%s: abbreviation %s already exists\n" ) abbr (__fish_abbr_print_key "$argv[2]" )
return 2
end
set -U fish_user_abbreviations $fish_user_abbreviations "$argv[2]"
return 0
case '-r' '--remove'
set -l index (__fish_abbr_get_by_key "$argv[2]")
if test $index -gt 0
set -e fish_user_abbreviations[$index]
return 0
else
printf ( _ "%s: no such abbreviation %s\n" ) abbr (__fish_abbr_print_key "$argv[2]" )
return 3
end
case '-s' '--show'
for i in $fish_user_abbreviations
echo abbr -a \'$i\'
end
return 0
case '-l' '--list'
for i in $fish_user_abbreviations
__fish_abbr_print_key $i
end
return 0
case '-h' '--help'
__fish_print_help abbr
return 0
case '' '*'
printf (_ "%s: Unknown option %s\n" ) abbr $argv[1]
__fish_print_help abbr
return 1
end
end
function __fish_abbr_printable
echo (__fish_abbr_print_key $argv)'="'(echo $argv | cut -f 2 -d =)'"'
end
function __fish_abbr_get_by_key
for i in (seq (count $fish_user_abbreviations))
switch $fish_user_abbreviations[$i]
case (__fish_abbr_print_key $argv)'=*'
echo $i
return 0
end
end
echo 0
return 1
end
function __fish_abbr_print_key
echo $argv| cut -f 1 -d =
end