mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2025-12-15 16:23:15 +08:00
feat(plugin): add plugin to easily save aliases and functions
This commit is contained in:
parent
f8022980a3
commit
b618fbd357
27
plugins/fishysave/README.md
Normal file
27
plugins/fishysave/README.md
Normal file
@ -0,0 +1,27 @@
|
||||
## fishysave
|
||||
|
||||
Plugin to save and update functions and aliases directly from shell, reminiscent of the fish "funcsave" feature.
|
||||
|
||||
## Install
|
||||
|
||||
add fishysave to the plugins array of your zshrc file:
|
||||
```bash
|
||||
# ~/.zshrc
|
||||
plugins=(... fishysave)
|
||||
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Save an alias
|
||||
alias lsal="ls -al"
|
||||
fishysave lsal
|
||||
|
||||
# Save a function
|
||||
function lsa() {
|
||||
ls -al
|
||||
}
|
||||
fishysave lsa
|
||||
|
||||
```
|
||||
39
plugins/fishysave/fishysave.plugin.zsh
Normal file
39
plugins/fishysave/fishysave.plugin.zsh
Normal file
@ -0,0 +1,39 @@
|
||||
local base_dir="${ZSH_SAVE_DIR:-$HOME/.zshrc_fishy}"
|
||||
|
||||
function fishysave() {
|
||||
local name="$1"
|
||||
|
||||
local base_dir="${ZSH_SAVE_DIR:-$HOME/.zshrc_fishy}"
|
||||
local alias_dir="$base_dir/aliases"
|
||||
local func_dir="$base_dir/functions"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "No parameter provided"
|
||||
echo "Usage: save <alias or function>"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if alias "$name" &>/dev/null; then
|
||||
local alias_def
|
||||
alias_def=$(alias "$name")
|
||||
|
||||
echo "alias $alias_def" > "$alias_dir/$name.zsh"
|
||||
echo "Alias $name saved to $alias_dir/$name.zsh"
|
||||
elif whence -w "$name" | grep -q function; then
|
||||
local func_def
|
||||
func_def=$(functions "$name")
|
||||
echo "$func_def" > "$func_dir/$name.zsh"
|
||||
echo "Function $name saved to $func_dir/$name.zsh"
|
||||
else
|
||||
echo "Couldn't find a declared function or alias named '$name'"
|
||||
return 2
|
||||
fi
|
||||
}
|
||||
|
||||
mkdir -p "$base_dir/aliases" "$base_dir/functions"
|
||||
|
||||
setopt localoptions nullglob
|
||||
|
||||
for file in "$base_dir"/aliases/*.zsh "$base_dir"/functions/*.zsh; do
|
||||
[[ -f $file ]] && source "$file"
|
||||
done
|
||||
Loading…
Reference in New Issue
Block a user