A snippet framework for zsh. Create new snippets as you go. Type snippet alias, press key, expand snippet - if snippet did not exist you are prompted to create one and can then carry on editing.
This framework makes creating snippets low enough friction that actually do it.
Features | zsh alias | global alias | zsh-snippets | zshnip |
---|---|---|---|---|
Supports pipes | ❌ | ✅ | ✅ | ✅ |
Snippets automatically saved | ❌ | ❌ | ❌ | ✅ |
Forgot snippet name 😧 | 😬 | 😬 | 😬 | 😄 (just guess and create a new snippet if wrong) |
Remembers cursor position | ❌ | ❌ | ❌ | ✅ (useful for commands with pipes ) |
How snippets are defined | With an editor | editor | editor | 😄 at shell itself 😄 |
Tweak command before run | ❌ | ❌ | ✅ | ✅ |
Create snippets while creating snippets | ❌ | ❌ | ❌ | ✅ |
This gallery of features provides animated GIFs that document many features.
This 20 second animated gif gives a good summary of why you might want to use zshnip.
This library uses the same approach and incorporates some code from William G Hatch's ([email protected]) original library that was in turn influenced by code on forums. The define-as-you-go features were added by [email protected].
Code for rendering the presentation to MP4 is taken from asciinema2gif by tav (public domain).
Using zplug
zplug facetframer/zshnip
zplug install
zplug load
Using zgen
zgen load facetframer/zshnip
zgen init
Raw installation
cd ~
git clone https://github.com/facetframer/zshnip.git .zshnip-repo
source .zshnip-repo/zshnip.zsh
Manually create key-bindings for defining snippets. (you can use different bindings if desired)
bindkey '\ej' zshnip-expand-or-edit # Alt-J
bindkey '\ee' zshnip-edit-and-expand # Alt-E
You will need to add the appropriate commands above to your .zshrc
to reload at startup.
After having installed zshnip and defined key-bindings (see the section above):
- Type your snippet alias (e.g.
gs
) - Run
zshnip-expand-or-edit
withAlt-J
(or your own key-binding) - A prompt will appear within your shell to define your snippet
- Type your snippet (e.g.
git status
) and pressENTER
. - Your snippet will have expanded.
- After this, when you press
Alt-J
after your snippet alias (e.g.gs
) this expansion will be used.
See this section of the presentation.
Defining snippets can be problematic:
- Learning other people's snippets is boring
- One only knows what snippets you want while you are carrying out a task, not later.
- Going to an editor to define a snippet can hijack another task.
Certainly, you can even supports pipes etc in your aliases using global aliases (alias -g g='| grep '
).
You could also write a wrapper about alias
to persist aliases and use something like
https://github.com/jarmo/expand-aliases-oh-my-zsh to expand them.
Using snippets have some benefits
- You can verify that snippets expand to what you want before running them (useful if you have a lot of snippets)
- The define-as-you-go features have various benefits.
Here is a list of the snippets the maintainer uses on a regular basis. These are not built-in, and you might want to phrase them differently, but they could act as inspiration.
gs -> git status
mh -> --help
mf -> --force
g -> | grep -i
gv -> | grep -v
sda -> sed -n '//,$ p'
gap -> git add -p
o -> $(!!)
lm1 -> | tail -n 1
xarm -> xargs -n 1 kill
xarmp -> xargs -p -n 1 kill
sso -> | grep -o ''
sss -> | grep -P '^'
w1 -> | awk '{ print $1 }'
wm1 -> | rev | awk ' { print $1 }'
si -> sudo apt-get install
sa -> sudo !!
ag -> !! | grep
The code for generating the feature gallery also performs consistency testing.
Combining testing and generation of documentation like this has various benefits (see doctest).
To run these tests run animation/record-gallery.sh
- https://github.com/1ambda/zsh-snippets is another extension of willghatch's library.
- I am an extensive user of yasnippet in emacs. This will have without doubt influenced features in this project.
LGPLv3 (see LICENSE for details.)
Copyleft licenses can be controversial. This project feels like more of application than a library so a copyleft license seems like a good choice. If there is a compelling reasons to place this under a more permissive license this would not be difficult.