Snippets
What is a Snippet?
A Snippet is a reusable piece of command/script that you can insert into your connection terminal. It can be useful for frequently used commands or configurations.
How to add a snippet?
There are two ways to add a snippet in WebSSH:
- From the Snippets Section:
- Navigate to the "Snippets" section in the WebSSH application
- Click on the "+" top right button
- Fill in the details such as the snippet name, icon and the command/script you want to save
- Save the snippet using the top right button ☑️
- From a Connection:
- Launch an existing connection or create a new one (and launch it)
- Tap the
< />top button on the navigation bar - Click on the "+" top right button
- Fill in the details such as the snippet name, icon and the command/script you want to save
- Save the snippet using the top right button ☑️
How to use a snippet?
To use a snippet in your connection terminal:
- Launch the desired connection
- Tap the
< />top button on the navigation bar - Select the snippet you want to insert from the list
- The command/script from the snippet will be inserted into your terminal, ready to be executed — or run immediately when Execute on tap is enabled (see below)
Keyboard shortcuts
Since WebSSH 33.1 a snippet can be run with a keyboard shortcut, on macOS and on iPad with a hardware keyboard. Nine shortcuts are available: ⌥⌘1 to ⌥⌘9.
To assign one, edit the snippet and pick a shortcut in the Keyboard Shortcut row, then save. Assigning a shortcut is a WebSSH PRO feature.
- The snippet runs in the active terminal, which must be connected.
- A snippet restricted by tags only runs on the connections it matches.
- Picking a shortcut already used by another snippet moves it to the snippet you are editing.
- On macOS, the snippets holding a shortcut are listed in the menu bar, under Connection ▸ Snippets.
⌥⌘8 and the macOS Zoom
macOS reserves ⌥⌘8 for its accessibility Zoom when "Use keyboard shortcuts to zoom" is enabled in System Settings. In that case the shortcut never reaches WebSSH: pick another one.
Execute on tap
Since WebSSH 33.2 a snippet can run as soon as it is inserted, with no extra tap on Enter: edit the snippet, turn on Execute on tap, then save. WebSSH sends Enter right after the command, using the Carriage Return character of the connection's profile. Snippets that run on tap wear a ⏎ badge in the list.
- The switch is off by default: a snippet that runs by itself must be opted into.
- It also applies to keyboard shortcuts, to the Snippets context menu on macOS and to dynamic variables (Enter follows the command once the form is validated).
- A command whose text already ends with a line break runs on tap too, as it always did: WebSSH never sends Enter twice, so the switch is harmless on such a snippet. The ⏎ badge is shown in both cases.
- Run on Hosts is not affected: it executes the command directly, without a terminal.
Import snippets from a GitHub Gist
Since WebSSH 32.7 the ••• menu of the Snippets screen offers Import from Gist…: the files of a public gist become snippets, and a snippet can later be refreshed from its source. The same menu opens the WebSSH Library, a curated collection of ready-made snippets. See Import Snippets from GitHub Gist.
Run a snippet on several hosts
Since WebSSH 33.0 a snippet can be run on several connections at once, without opening a terminal: long press the snippet and choose Run on Hosts. See Run a command on multiple hosts.
Dynamic variables
A snippet can ask for values when it runs. Write a placeholder between triple braces and WebSSH shows a small form before sending the command:
echo "You are {{{ NAME : Awesome }}}!"
| Syntax | Behaviour |
|---|---|
{{{ NAME }}} |
Free text, empty by default |
{{{ NAME : default }}} |
Free text, prefilled with default (edge spaces are trimmed) |
{{{ NAME : " default " }}} |
Quotes keep the spaces and protect : and | inside the default |
{{{ NAME : first : second : third }}} |
A choice list, first selected |
{{{ NAME : default | secret }}} |
The value is typed in a secure field and hidden in the preview |
Rules:
- A name uses letters, digits and underscores, must not start with a digit and is case-sensitive (
{{{ Path }}}and{{{ PATH }}}are two variables). - The same name used several times is asked once; its first occurrence defines the default, the choices and the modifiers.
- A placeholder that does not follow the syntax is left as-is in the command, so you can see what went wrong.
- Values are inserted exactly as typed: WebSSH adds no shell quoting, and a value is never interpreted as a key token such as
<ctrl-c>nor as another placeholder. - Nothing is remembered between runs: every execution starts from the defaults.
Built-in variables
These names are prefilled from the current connection. They stay editable in the form.
| Name | Value |
|---|---|
{{{ HOST }}} |
Hostname or IP address of the connection |
{{{ USER }}} |
Username of the connection |
{{{ PORT }}} |
Port of the connection |
{{{ CONNECTION }}} |
Name of the connection |
{{{ DATE }}} |
Today, as YYYY-MM-DD |
Examples
ssh -p {{{ PORT }}} {{{ USER }}}@{{{ HOST }}}
tar czf backup-{{{ DATE }}}.tgz {{{ DIR : /var/www }}}
tail -n {{{ LINES : 100 }}} -f {{{ LOG : /var/log/syslog : /var/log/auth.log : /var/log/nginx/error.log }}}
mysql -u {{{ DB_USER : root }}} -p{{{ DB_PASSWORD | secret }}} {{{ DATABASE }}}