Snippets
A snippet is a command you save once and run again from any session: df -h, a docker logs line, a deploy script. A snippet can hold variables, so ZestSSH asks for the parts that change each time before it runs.
Snippets are free on every plan. They sync with the rest of your data when you use sync.
Where snippets are
Section titled “Where snippets are”- On a phone: More > Snippets. On a tablet, the Snippets tab at the bottom of the screen. Inside a session, open the terminal toolbar’s menu and choose Snippets.
- On a computer: Snippets in the side bar. Inside a session, the Inspector (in the session header) lists them under Snippets, and the command palette (Ctrl+Shift+K; on a Mac ⌃⇧K) finds them too. Type
#first in the palette to search snippets only.
The built-in snippets
Section titled “The built-in snippets”ZestSSH comes with 51 ready-made snippets in seven categories: System, Services, Docker, Network, Files, Security and Packages. They are not added by themselves. To add them, open Snippets on a phone or tablet while it is empty and tap Load defaults. A computer has no such button; snippets added on a phone reach your computer through sync.
A few of the built-ins ask before they run: Reboot, Prune Unused (docker system prune -f), Update (Debian) and Update (RHEL). The Services and Docker snippets pick their service or container from a list read from the server (see Lists from the server).
A built-in category can be renamed and recolored, but not deleted.
Start with no snippets (computer)
Section titled “Start with no snippets (computer)”From 4.3, a computer’s Snippets page with no snippet yet is a start page, Commands you type again and again. It says what a snippet is, where it runs (from the page, or from a terminal’s Snippets button) and that ${NAME} in a command asks for a value when it runs. Then:
- Take a few to start: eight starters in four folders, three of them ticked to begin with. Tick the ones you want and choose Add 3 snippets: they are saved as your own, in those folders, to change or delete like any other.
| Folder | Starter | Command |
|---|---|---|
| System | Disk usage (ticked) | df -h |
| System | Follow a service’s log (ticked) | journalctl -u ${SERVICE} -f |
| Docker | Restart a container (ticked) | docker restart ${NAME} |
| Docker | Running containers | docker ps |
| Network | Who is listening | sudo ss -tlnp |
| Network | Ping a host | ping -c 4 ${HOST} |
| Files | Biggest folders here | du -sh * | sort -h | tail |
| Files | Follow a log file | tail -f ${FILE} |
- Save from your history: tick commands of this device’s command history and choose Save 3 snippets. Each is saved as a snippet named after its command; rename it any time. The list shows each command once per host, newest first. With history off or empty it says so, and in Recording mode it shows no commands.
- New snippet opens the editor beside the starters.
In the editor, the Usage part shows only once the snippet has run on this computer or a workflow runs it, and empty fields show examples, such as “e.g. Restart Docker” for Label.
Create a snippet
Section titled “Create a snippet”On a phone:
- Open Snippets (in More on a phone) and tap + (New snippet).
- Fill in Display name, Command and, if you like, Category (optional). A new snippet starts in the Default snippet category you set under Settings > Terminal and SSH.
- Turn on Confirm before running for a command you want to be asked about every time.
- Save it.
On a computer:
- Open Snippets in the side bar (under Utilities) and click New snippet.
- Fill in Label, Command, Category and, if you like, About (a note on what it does) and Default host (the host its Run goes to; Ask each time where none is set).
- Under Run options, turn on Confirm before running if you want to be asked first.
- Click Save.
On a phone you can also select text in a terminal, long-press it and choose Save as snippet. On a computer, a command in History has Save as snippet.
Once the snippet is stored, ZestSSH says so with its name: Snippet added: for a new one, Snippet saved: for one you changed (in the notification center on a computer, at the top of the screen on a phone). A save that fails says “The snippet was not saved.”, with Copy details.
Variables
Section titled “Variables”Put a placeholder in the command, and ZestSSH asks for its value when the snippet runs. What you enter reaches the command as one piece of text, so a value like a; rm -rf ~ stays text and never runs as a second command. Standing alone it goes in single quotes. Where the command already has the placeholder in quotes, as in grep "${WORD}" log or grep '${WORD}' log, it stays inside those quotes, and the same holds inside $(...), backticks and a here-document. Inside $(( )), only what a sum can use goes in. A command that runs its own argument (eval, sh -c) still runs what it is given, and these are the rules of a POSIX shell: a host whose login shell is fish, PowerShell or cmd has others.
| Placeholder | What happens when the snippet runs |
|---|---|
${HOST} |
You are asked for a value. The name is the field’s label. |
${HOST:-web-01.example.com} |
The default after :- is used, and you are not asked. |
${TOKEN:secret} |
You are asked, and what you type is hidden. ${TOKEN:password} works the same. |
$[main,develop,staging] |
You pick one of the listed choices. |
$[DOCKER] |
You pick from a list read from the server (see below). |
If the same placeholder appears more than once, you enter it once and it fills every place. A default does too: in mkdir -p ${DIR:-/tmp/a}; rm -rf ${DIR}/*, both are /tmp/a. The older list form ${[main,develop]} still works.
A value typed into a ${NAME:secret} field is left out of a team’s session log.
The snippet editor lists the variables it found in the command (Variables, “Detected from the command”), each with its Name, Default, Prompt and whether it is Required. There you can give a variable a default (this rewrites ${NAME} as ${NAME:-value} in the command) and a prompt.
Examples:
ping -c 4 ${HOST}tail -n ${LINES:-50} /var/log/sysloggit checkout $[main,develop,staging]Lists from the server
Section titled “Lists from the server”A list placeholder with one of these keywords is filled by a command ZestSSH runs on the host of the session:
| Keyword | Read with | Shown as |
|---|---|---|
$[DOCKER] |
docker ps --format '{{.Names}}' |
Docker Containers |
$[PM2] |
pm2 jlist |
PM2 Processes |
$[SYSTEMD] |
systemctl list-units --type=service --state=running |
Systemd Services |
$[SCREEN] |
screen -ls |
Screen Sessions |
$[TMUX] |
tmux list-sessions |
Tmux Sessions |
Where there is no session to read the list from (Snippets on a phone or tablet, for example), or the list comes back empty, you type the value in instead.
Run a snippet
Section titled “Run a snippet”In a session
Section titled “In a session”- Phone: open the terminal toolbar’s menu, choose Snippets and tap one.
- Computer: click a snippet in the Inspector, or pick it in the command palette.
ZestSSH first asks for the variables, then asks the questions described in Before a snippet runs. Then it types the command at the prompt. You press Enter to run it.
Auto-execute snippets presses Enter for you after a snippet picked inside a session (off at first). On a phone it is in Settings > Terminal and SSH; on a computer, it is Press Enter after pasting a snippet in the options button at the top of the Snippets page.
If a snippet is set to ask first and the command also trips dangerous command protection, you are asked once, not twice.
In a snippet of several lines, every line but the last runs as it is typed, as if you pressed Enter after each one. The last line waits for your Enter.
From Snippets on a phone or tablet
Section titled “From Snippets on a phone or tablet”Snippets (in More on a phone, a tab of its own on a tablet) is not attached to a session. Tapping a snippet asks for its variables, asks if the snippet is set to ask first, and then copies the finished command: “Command copied to clipboard”. Paste it into a session.
On the Snippets page, a long press on a snippet starts selecting it (see Selecting many items); with only that one selected, Snippet actions has Edit, Copy command, Duplicate, Move to…, Copy to… (where there is a place to go) and Delete. In a session, a long press on a snippet opens them at once. Long-press a category for Edit category (its name and color) and Delete group. The menu at the top has Export snippets and Import snippets (a JSON file). In a team, Export snippets is there only for an owner, and not while the team’s rules forbid exports.
From a computer’s Snippets page
Section titled “From a computer’s Snippets page”The Snippets page lists your snippets by category, as a List or as Cards. Above them:
- Filter by name or command searches.
- All (with the count), Has variables, Asks first and a chip for each category narrow the list (more categories fold into “2 more”).
- The sort menu has Your order, Name A-Z, Name Z-A, Recently updated and Arrange categories… (drag groups into order, hide them, pick their color; this syncs). A hidden category’s snippets come back with Show hidden.
Each snippet shows its command, how many variables it has, Asks first where it is set to ask, how many times it ran on this computer and when (“Run 3 times · 2d ago”, or Never run), and how many workflows use it. Its Run button runs it; the arrow beside it (Where to run it) has Run in the session you’re in and Run on….
Select, or Ctrl+click (Cmd+click on a Mac), selects several snippets for Move to category…, Move to…, Copy to… or Delete at once. See Selecting many items.
Click a snippet to see it on the right: its command with the variables marked, each variable with what it asks for (“Asks for: HOST”, or “Picks from:” for a list) and its default, and under Run its Default host (or The session you’re in, while you are in one), Ask before running and Used by workflows. At the bottom, Run on… picks a host, and Run (or Run on web-01, with a default host) runs it. With no default host and no session open, it says “No session is open: open one, or choose Run on… for a host.” With several open while you are on this page, it says “You’re not in a session: pick one of your 3 open sessions, or Run on… for another host.”, and Run asks which.
Where a snippet’s run goes:
- To its Default host, if it has one.
- Otherwise to the session in front, if one is connected, or to the one session open, if only one is.
- Run on… asks where (Run snippet on…). Its first choice, on a computer and on an Android tablet with the computer’s layout, is Local terminal, a new terminal on this device (see In a terminal on this device). Then come your saved hosts. With Active first on, as it is here unless you turn it off, the hosts with an open session stand first, the last used on top, then the rest; each by its groups.
On a host with an open SSH session, the snippet is filled in and entered there (“Sent to web-01”). On a host with none, ZestSSH opens a session to it, as Connect does, and enters the snippet once it is connected: “Opening web-01: the snippet runs there once it is connected.” If the session does not connect: “web-01 did not connect, so the snippet was not run.”
A snippet’s menu (its three dots, or a right-click) has Edit, Copy, Duplicate, Run on… (or Run on web-01, with a default host), Run on hosts… and Delete. Run on hosts… runs it on several hosts at once and shows each result. See Run on hosts.
Deleting a snippet asks first: “A workflow with a step that runs it stops at that step the next time it runs, with an error naming the snippet.”
Duplicate copies a snippet with its notes and its variables’ descriptions.
In a terminal on this device
Section titled “In a terminal on this device”From 4.0, a snippet can run on the device you are using, with no host. On a computer, and on an Android tablet that shows the computer’s layout, Run on… lists Local terminal (“A new terminal on this device”) first, above your hosts. Click it, or type the start of its name in the search and press Enter.
- ZestSSH asks for the snippet’s variables first. A list from the server (
$[DOCKER]and the others) is typed in, as there is no host to read it from. - Then it asks what every run asks: dangerous command protection, or the snippet’s own Confirm before running.
- Only then does it open a new terminal on this device, in a tab in front, and say “Opening Local terminal: the snippet runs there once it is connected.” The snippet is entered there once the shell is ready.
A run you cancel at a question opens no terminal. The shell is your computer’s own, or Android’s (see Local shell). It is yours whichever space is on screen, a team’s included, so nothing of it goes to a team’s session log.
The terminal counts as a session: at the free plan’s limit nothing opens, and ZestSSH says “The free plan opens up to 5 sessions at a time. Upgrade to Squeezed for unlimited sessions.” (3 on a tablet). If the shell does not start: “Local terminal did not connect, so the snippet was not run.”
An iPhone or iPad has no shell of its own for ZestSSH to open, so it does not offer Local terminal.
Before a snippet runs
Section titled “Before a snippet runs”Two things can ask before a snippet runs in a session:
- Confirm before running, a switch on each snippet. ZestSSH shows the command and asks you to confirm.
- Dangerous command protection, a setting that is on at first. It checks the finished command for things that could do lasting harm, such as deleting a folder the system needs, formatting a disk, piping a download into a shell, or shutting the host down. If it finds one, it asks “Run this command?” and names what it found. It checks any snippet, whether or not the snippet is set to ask.
You find Dangerous command protection under Settings > Security and privacy, on a computer and on a phone. Don’t ask again, on the question about lasting harm, turns the setting off. The question that only asks for a look has no such box.
The question says what it found and what to do about it, in a sentence for each thing, and on a computer it marks that part of the command when you point at the sentence. What can do lasting harm is asked as “Run this command? It could do lasting harm”, with Run anyway. What only needs a look (a short script, a command inside a string, dd reading a disk with nowhere to write) is asked as Check this command, with Run it. What runs inside a string or a short script is read as well, so sh -c 'rm -rf /' is asked about as lasting harm, and so is dd with an of=, which writes over the file or disk it names.
So are find deleting what it finds at the root, at a folder the system needs or at the home folder, wipefs -a, and shred of a device or, with -u, of a path outside the folder it runs in. What only reads or only speaks of a thing asks nothing: cat ~/.ssh/authorized_keys, crontab -l, dd with of=/dev/null, man shutdown, last reboot. That is for a line that is one plain command and nothing else. With a second command, a $(...), a redirect, a comment or a quote on the line, it is asked about like any other.
A very long text is read as far as its first 128 KB. What is past that is not known, so it is asked about as lasting harm.
A value you typed with dots is checked as it will run, and stays dots in the question: in the command and in the sentence that quotes it, and in the question before a broadcast too.
Run a snippet when you connect
Section titled “Run a snippet when you connect”A host can run one snippet each time you connect to it:
- Open the host’s editor. On a computer, go to Advanced. On a phone, tap Advanced options.
- Under Automation, pick the snippet in Run snippet (optional).
- Save the host.
The snippet runs right after the host’s Startup command (optional), if it has one. A snippet that is set to ask first, or that needs a value from you (a variable without a default, a list, or an empty default like ${DIR:-} where the command would be a dangerous one with it empty), is skipped, because nobody is there to answer. A variable with a default takes it, at every place it stands.
When you delete a snippet, every host that ran it on connect stops doing so.
Snippets in automation and workflows
Section titled “Snippets in automation and workflows”A workflow can run a snippet as one of its steps, and a zestssh://snippet link or another app can run one on a saved host (see Automation). These runs find the snippet by its name. A zestssh:// link first asks “Run this from a link?”, showing the snippet, the host and the command it will run. A link, another app and a schedule do not ask a snippet’s own Confirm before running question, as nobody is there to answer it. A workflow you start yourself does (see Run a workflow).
In these runs, the values the link, the app or the workflow step passes fill the snippet’s placeholders by name: {{name}}, and the ${NAME}, ${NAME:-default} and ${NAME:secret} forms above (a link passes them as var_name=value). Each value reaches the command as one piece of text, as under Variables. A variable with a default that is passed no value, or a blank one, takes its default at every place it stands. A plain ${NAME} with no value is left as it is, for the server’s shell. With nobody there, nobody is asked, and lists from the server are not read. A value passed blank for a variable with no default, or a list or a secret with none, stops the run and names it, and so does a plain ${NAME} with no value, or an empty default like ${NAME:-}, where the command would be a dangerous one with it empty: rm -rf ${DIR}/* never reaches a shell with DIR blank.
Next steps
Section titled “Next steps”- History suggestions: commands you ran before, offered as you type.
- Workflows: several commands and snippets in a row.
- Run on hosts: one command on many hosts.