Pet
Simple command-line snippet manager
Install / Use
/learn @knqyf263/PetREADME
Pet - CLI Snippet Manager
<div align="center"> <img src="doc/logo.png" width="350"> </div>Motivation
pet is a simple command-line snippet manager (inspired by memo).
I have a hard time remembering complex command or ones that I rarely use. Moreover, it is difficult to find them in shell history.
It's time to let go of the expectation of remembering every command, and focus on productivity and finding the right commands as fast as possible. It's fun when you're 2 years in and work with 2 tools, but less so when you're a decade in and work across backend/frontend/infrastructure with tons of tools. You most probably relate to this if you're a developer.
pet is a simple tool that allows you to save, tag, search, and execute command-line snippets easily! It's now nearly 8 years old and is used by many developers around the world.
pet is written in Go, and therefore you can just grab the binary releases and drop it in your $PATH.
You can use variables (<param> or <param=default_value> ) in snippets.
TOC
- Main features
- Parameters
- Examples
- Features
- Hands-on Tutorial
- Usage
- Snippet
- Configuration
- Installation
- Migration
- Contribute
Main features
pet has the following features.
- Register your command snippets easily.
- Use variables (with one or several default values) in snippets.
- Search snippets interactively
- Run snippets directly.
- Edit snippets easily (config is just a TOML file).
- Sync snippets via Gist or GitLab Snippets automatically.
Creating a snippet
You can create a snippet by running pet new.
$ pet new
Command> echo Hello world!
Description> print Hello world
To see all available arguments, run pet new --help.
Multiline commands can be entered by using the multiline argument pet new --multiline
You can use also use variables in snippets, these are called parameters. More information on that in the next section.
You can also tag snippets to search for them faster. More information on that in the tag section.
Parameters
There are <n_ways> ways of entering parameters.
They can contain default values: Hello <subject=world>
defined by the equal sign.
They can even contain <content=spaces & = signs> where the default value would be <content=<mark>spaces & = signs</mark>>.
Default values just can't <end with spaces >.
They can also contain multiple default values:
Hello <subject=|_John_||_Sam_||_Jane Doe = special #chars_|>
The values in this case would be :Hello <subject=|_<mark>John</mark>_||_<mark>Sam</mark>_||_<mark>Jane Doe = special #chars</mark>_|>
Examples
Some examples are shown below.
Register the previous command easily
By adding the following config to .bashrc or .zshrc, you can easily register the previous command.
bash prev function
function prev() {
PREV=$(echo `history | tail -n2 | head -n1` | sed 's/[0-9]* //')
sh -c "pet new `printf %q "$PREV"`"
}
zsh prev function
cat .zshrc
function prev() {
PREV=$(fc -lrn | head -n 1)
sh -c "pet new `printf %q "$PREV"`"
}
fish
See below for details.
https://github.com/otms61/fish-pet
Select snippets at the current line (like C-r) (RECOMMENDED)
bash
By adding the following config to .bashrc, you can search snippets and output on the shell.
This will also allow you to execute the commands yourself, which will add them to your shell history! This is basically the only way we can manipulate shell history.
This also allows you to chain commands! Example here
You can also customize the search and list commands with options, example -t or --tags, for example to only search the subset of snippets tagged with myjob pet search -t myjob.
cat .bashrc
function pet-select() {
BUFFER=$(pet search --query "$READLINE_LINE")
READLINE_LINE=$BUFFER
READLINE_POINT=${#BUFFER}
}
bind -x '"\C-x\C-r": pet-select'
zsh
cat .zshrc
function pet-select() {
BUFFER=$(pet search --query "$LBUFFER")
CURSOR=$#BUFFER
zle redisplay
}
zle -N pet-select
stty -ixon
bindkey '^s' pet-select
fish
See below for details.
https://github.com/otms61/fish-pet
Expand snippet parameters inline on shell
You can expand parameters using the shell instead of the builtin TUI dialog. This allows you to edit the parameters with native shell features, like tab-completion, highlighting, etc.
bash
function pet-select() {
BUFFER=$(pet search --raw --query "$READLINE_LINE")
READLINE_LINE=$BUFFER
READLINE_POINT=${#BUFFER}
}
bind -x '"\C-x\C-r": pet-select'
function _pet_move_cursor_to_next_parameter() {
match="$(echo "$READLINE_LINE" | perl -nle 'print $& if /<.*?>/')"
if [ -n "$match" ]; then
default="$(echo "$match" | perl -nle 'print $& if /(?<==).*(?=>)/')"
match_len=${#match}
default_len=${#default}
pre_match=${READLINE_LINE%%$match*}
parameter_offset=${#pre_match}
READLINE_POINT="$((${parameter_offset} + ${default_len}))"
READLINE_LINE="${READLINE_LINE:0:$parameter_offset}${default}${READLINE_LINE:$parameter_offset+$match_len}"
fi
}
bind -x '"\C-n": _pet_move_cursor_to_next_parameter'
zsh
function pet-select() {
BUFFER=$(pet search --raw --query "$LBUFFER")
CURSOR=$#BUFFER
zle redisplay
}
zle -N pet-select
stty -ixon
bindkey '^s' pet-select
function _pet_move_cursor_to_next_parameter() {
match="$(echo "$BUFFER" | perl -nle 'print $& if /<.*?>/')"
if [ -n "$match" ]; then
default="$(echo "$match" | perl -nle 'print $& if /(?<==).*(?=>)/')"
match_len=${#match}
default_len=${#default}
parameter_offset=${#BUFFER%%$match*}
CURSOR="$((${parameter_offset} + ${default_len}))"
BUFFER="${BUFFER[1,$parameter_offset]}${default}${BUFFER[$parameter_offset+$match_len+1,-1]}"
fi
}
zle -N _pet_move_cursor_to_next_parameter
bindkey '^n' _pet_move_cursor_to_next_parameter
Copy snippets to clipboard
By using pbcopy on macOS, you can copy snippets to clipboard.
Allow to register from history when using fzf
Just export this to your .bashrc or .zshrc file. This will show your history
as default (when using fzf) and it also binds the alt+s key combination
to allow you to search and save some previous used command command.
export FZF_CTRL_R_OPTS="
--reverse
--cycle
--info=right
--color header:italic
--header 'alt+s (pet new)'
--preview 'echo {}' --preview-window down:3:hidden:wrap
--bind '?:toggle-preview'
--bind 'alt-s:execute(pet new --tag {2..})+abort'"
Features
Edit snippets
The snippets are managed in the TOML file, so it's easy to edit.
<img src="doc/pet04.gif" width="700">Sync snippets
You can share snippets via Gist.
<img src="doc/pet05.gif" width="700">Usage
Usage:
pet [command]
Available Commands:
clip Copy the selected commands
configure Edit config file
edit Edit snippet file
exec Run the selected commands
help Help about any command
list Show all snippets
new Create a new snippet
search Search snippets
sync Sync snippets
version Print the version number
Flags:
--config string config file (default is $HOME/.config/pet/config.toml)
--debug debug mode
-h, --help help for pet
Use "pet [command] --help" for more information about a command.
Snippet
Run pet edit
You can also register the output of command (but cannot search).
[[snippets]]
command = "echo | openssl s_client -connect example.com:443 2>/dev/null |openssl x509 -dates -noout"
description = "Show expiration date of SSL certificate"
output = """
notBefore=Nov 3 00:00:00 2015 GMT
notAfter=Nov 28 12:00:00 2018 GMT"""
Run pet list
Command: echo | openssl s_client -connect example.com:443 2>/dev/null |openssl x509 -dates -noout
Description: Show expiration date of SSL certificate
Output: notBefore=Nov 3 00:00:00 2015 GMT
notAfter=Nov 28 12:00:00 2018 GMT
------------------------------
Configuration
Run pet configure
[General]
snippetfile = "path/to/snippet" # specify snippet directory
editor = "vim" # your favorite text editor
column = 40 # column size for list command
selectcmd = "fzf" # selector command for edit command (fzf or peco)
backend = "gist" # specify backend service to sync snippets (gist, ghe or git
Related Skills
node-connect
337.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
337.4kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
83.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
