Some tools just refuse to die and remain useful for years. Pet is a really good example. I’ve been using it for over 6 years and is still be maintained by its creator, Teppei Fukuda, whom I thank from the bottom of my heart for creating such a great little thing.
The pet binary, available for most operating systems, gives you an infinite number of configurable shortcuts to commonly used shell commands. This can be anything from launching an SSH tunnel to starting containers. All you have to do is configure the commands in its configuration file ~/.config/pet/snippet.toml
, which can be committed to a git repository for easy sharing between your different machines.
In this article, I’ll show you the most important ways to use Pet, which can quickly become your best shell friend.
Installation
It’s as simple as putting the pet binary in your path, some Linux distributions may already have a package for it, prefer this way if applicable. On MacOS you can run
brew install knqyf263/pet/pet
Homebrew install fzf dependency, if installing by other means make sure you have it as well.
Once you have it in your path, the next thing to do is to configure your .bashrc
or .zshrc
for quick access to Pet main features.
To easily add a previously executed command as a Pet snippet.
function prev() {
PREV=$(fc -lrn | head -n 1)
sh -c "pet new `printf %q "$PREV"`"
}
To access your commands just by using CTRL-S
on zsh
function pet-select() {
BUFFER=$(pet search --query "$LBUFFER")
CURSOR=$#BUFFER
zle redisplay
}
zle -N pet-select
stty -ixon
bindkey '^s' pet-select
on bash
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'
Pet Commands
cli | description | |
---|---|---|
pet search or CTRL-s |
Search for a command | |
pet new -t |
Create a new command with a tags space separated | |
pet edit |
Edit Commands | |
pet list |
List all existing commands | |
pet exec |
Execute a command | |
pet clip |
Copy the selected commands | |
pet sync |
Sync up to the configured Gist | |
pet configure |
Configure | |
pet list -t $tag |
Filter snippets by tags |
Pet Tips & Tricks
In your Pet configuration file, variables can be used in commands between <
and >
; like this
git remote add origin git@github.com:planetrobbie/<REPO=default_value>.git
By using pbcopy
on OS X, you can copy snippets to clipboard to run a command remotely using a SSH connection, you’ll be able to paste the command from the clipboard.
pet search | pbcopy
Pet Configuration
You can configure the location of your snippet file, just run pet configure
and add the following section
[General]
snippetfile = "/home/<user>/.config/pet/snippet.toml"
Snippets are locally stored but Pet can be configured to sync them via Gist or GitHub. First get an access token from GitHub.
Now add one of the following code depending if you prefer Gist of GitLab
[Gist]
file_name = "pet-snippet.toml" # specify gist file name
access_token = "" # your access token
gist_id = "" # Gist ID
public = false # public or priate
auto_sync = false # sync automatically when editing snippets
[GitLab]
file_name = "pet-snippet.toml" # specify GitLab Snippets file name
access_token = "" # your access token
id = "" # GitLab Snippets ID
visibility = "private" # public or internal or private
auto_sync = false # sync automatically when editing snippets
If you set auto_sync
to true, Pet will be automatically sync’ed after addition
To conclude this article please find below a snippet example
[[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 = """
Have fun with your Pet.
Links
- Pet is available on https://github.com/knqyf263/pet