Thanks to visit codestin.com
Credit goes to github.com

Skip to content

a-happin/zabbrev

 
 

Repository files navigation

zabbrev

ZSH abbreviation expansion plugin

Feature

Differences from original repository

Usage

Configuration

(Root) key value type
abbrevs List<Abbr>
Abbr key value type description
name Option<String> abbreviation name
context String default is "" (empty)
see below
global bool default is false
see below
abbr String a trigger string (required either abbr or abbr-regex)
^ abbr-regex String a trigger regex (required either abbr or abbr-regex)
snippet String the string to be expanded (required)
operation String expansion method
replace-self: replace the last argument with snippet (default)
replace-command: replace the first argument with snippet
replace-all: replace whole command with snnipet
append: insert snnipet after the last argument
prepend: insert snippet before the first argument
evaluate bool false: insert as string (default)
true: do zsh parameter expansion, then insert

Customize conditions

context == "" context != ""
global == false only trigger at the first argument only trigger at the second argument
global == true trigger anywhere trigger anywhere if the first argument is context

Special variables

Following variables are available if evaluate == true

name description
$1 expands to trigger string

Setup

In your .zshrc

$ eval "$(zabbrev init --bind-keys)"

Examples

Normal abbreviations

behaves like zsh aliases

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # normal abbreviations
  - name: git
    abbr: 'g'
    snippet: 'git'

  - name: editor
    abbr: 'e'
    snippet: '${EDITOR}'
    evaluate: true

then

$ g<Space>
#  ↓ expanded
$ git 

$ e<Space>
#  ↓ expanded
$ nvim 

Add default option

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # add default option
  - name: mv -i
    abbr: 'mv'
    snippet: '-i'
    operation: append

then

$ mv<Space>
#  ↓ expanded
$ mv -i 

Prepend sudo

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # prepend sudo
  - name: sudo apt
    abbr: 'apt'
    snippet: 'sudo'
    operation: prepend

then

$ apt<Space>
#  ↓ expanded
$ sudo apt 

Subcommand abbreviations

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # subcommand abbreviations
  - name: git commit
    context: 'git'
    abbr: 'c'
    snippet: 'commit'

  - name: git push -u origin HEAD
    context: 'git'
    abbr: 'pu'
    snippet: 'push -u origin HEAD'

  - name: git pull --rebase origin CURRENT_BRANCH
    context: 'git'
    abbr: 'pr'
    snippet: 'pull --rebase origin $(git symbolic-ref --short HEAD)'
    evaluate: true

then

$ git c<Space>
#  ↓ expanded
$ git commit 

$ git pu<Enter>
#  ↓ expanded
$ git push -u origin HEAD

$ git pr<Enter>
#  ↓ expanded
$ git pull --rebase origin main

Fake command

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # fake command
  - name: extract tar
    context: 'extract'
    abbr-regex: '\.tar$'
    snippet: 'tar -xvf'
    operation: replace-command

  - name: compress tar
    context: 'compress'
    abbr-regex: '\.tar$'
    snippet: 'tar -cvf'
    operation: replace-command

then

$ extract archive.tar<Enter>
#  ↓ expanded
$ tar -xvf archive.tar

$ compress archive.tar<Space>
#  ↓ expanded
$ tar -cvf archive.tar 

Associated command

behaves like zsh suffix aliases

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # associated command
  - name: run jar file
    abbr-regex: '\.jar$'
    snippet: 'java -jar'
    operation: prepend

then

$ ./main.jar<Space>
#  ↓ expanded
$ java -jar ./main.jar 

Like a function

behaves like zsh functions

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # like a function
  - name: mkdircd
    context: 'mkdircd'
    abbr-regex: '.+'
    snippet: 'mkdir -p $1 && cd $1'
    operation: replace-all
    evaluate: true

then

$ mkdircd foo<Space>
#  ↓ expanded
$ mkdir -p foo && cd foo 

Global abbreviations

behaves like zsh global abbreviations

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # global abbreviations
  - name: '>/dev/null'
    abbr: 'null'
    snippet: '>/dev/null'
    global: true

then

$ type cargo null<Space>
#  ↓ expanded
$ type cargo >/dev/null 

Global abbreviations with context

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # global abbreviations with context
  - name: git current branch
    context: 'git'
    abbr: 'B'
    snippet: '$(git symbolic-ref --short HEAD)'
    global: true
    evaluate: true

then

$ git show B<Space>
#  ↓ expanded
$ git show main 

$ echo B<Space>
#
$ echo B 

As one pleases

# ~/.config/zsh/zabbrev.yaml
abbrevs:
  # as one pleases
  # You don't have to remember shortcut key.
  - context: 'cd'
    abbr: 'f'
    snippet: $(fd --type d --hidden --no-ignore --exclude .git | fzf --preview 'exa -lha --time-style long-iso --color=always {}')
    evaluate: true
  - context: 'cd'
    abbr: 'g'
    snippet: $(fd --type d --hidden --follow '^.git$' ~ -x dirname | fzf --preview 'git -c color.status=always -C {} status')
    evaluate: true
  # choose commit interactively
  - context: 'git'
    abbr: 'i'
    snippet: rebase -i $(git log --graph --all --oneline --color=always | fzf --ansi --no-sort --reverse --tiebreak index -0 --height=60% --preview "git show --color=always \$(printf '%s' {} | grep -io '[0-9a-f]\{7,\}' | head -1)" | \grep -io '[0-9a-f]\{7,\}' | head -1)
    evaluate: true

then

$ cd f<Space>
#  ↓ expanded
$ cd ./Downloads

$ git i<Space>
#  ↓ expanded
$ git rebase -i 544f368

Installation

$ git clone https://github.com/a-happin/zabbrev.git && cd zabbrev && cargo install --path .

Alternatives

About

zsh abbreviation expansion plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 97.8%
  • Shell 2.2%