Snippets
Saving code that you often reuse in Espresso is much more flexible and powerful than many other editors thanks to Espresso's powerful snippet syntax.
Using snippets
There are several ways to access snippets:
- Click the + button in the editor's toolbar and choose a snippet from its menu. The menu lists the snippets that apply to the language you are editing: the standard ones that come with Espresso, and your own.
- Type a snippet's trigger and press tab. A snippet that has a trigger shows it beside its name in the menu.
- Choose Edit ⟩ Commands… (shift command P) and type part of the snippet's name: see Running a command by name
- Insert snippets through built-in tools (like CodeSense or Zen)
Regardless how you insert a snippet, you will find that Espresso snippets offer several features that you will not find in other editors.
For instance, if you insert a snippet and find that some of the text is already selected or the cursor is somewhere in the middle of the snippet that typically means that the snippet includes tab stops. Tab stops are special areas in a snippet that you jump between using tab (to move forward) and shift tab (to move backward). If your snippet includes selected text try hitting tab to jump to other portions of it.
A common pattern in the snippets that are included with Espresso is to provide a default value and select it. If you want to use the default, you can hit tab again to start moving through the various parts of the snippet, and if you want to do something different, you can delete the default text and continue from there.
For instance, the CSS snippet that is inserted when you use CodeSense to create a background shorthand property looks like this:
The example text (selected in blue) assumes that you want to use all of the available portions (except background-attachment, which is left out since it is rarely used). If instead all you want is to specify a color, you can hit delete and then enter your color choice. If you do indeed want to use all of the shown items, you can start hitting tab to jump between them one at a time starting with the color:
Followed by the image path:
And so forth.
You may also find some snippets that automatically fill out different areas depending on what you enter in a particular tab stop, or that insert default data like the selection before you inserted the snippet or similar. You will need to explore on a per-snippet basis what each does.
If you need to enter a literal tab character while in a tab stop, hit option tab.
Creating snippets
To create a new snippet, click the + button in the toolbar and choose Edit Snippets... from the bottom of its menu. A sheet opens with your snippets on the left.
Click New Snippet, give the snippet a name, and type what it should insert. New Group and New Submenu let you keep a large collection in order.
Quick tip
Remember that there are some characters that have special meaning in snippet syntax! If you need a literal dollar ($) or backtick (`), you will need to escape it with a backslash: \$ or \`.
Besides its text, a snippet has three settings:
- Zen Trigger: if you enter anything here, you can insert your snippet by typing whatever you entered and hitting tab. For instance, if you give your snippet a trigger of
my_snippetthen whenever you typemy_snippetin the editor and hit tab, it will be replaced with your snippet. A trigger is typed by itself, and works alongside Zen shortcuts. - Snippet For: the language the snippet is offered in, or All Languages
- Re-Indent: whether Espresso lays the snippet out to match the code it is inserted into. Automatic suits most snippets; choose Indentation Only or Off if yours is laid out in a way you want kept.
To give a snippet a keyboard shortcut, find it in the Keyboard preferences.
To find out how to use tab stops and other goodies in your snippet, you will need to use the special snippet syntax.
Favorites in the toolbar
The same sheet puts the snippets and text actions you use most in the toolbar. Choose Customize Toolbar Actions… from the bottom of the toolbar's action menu (the gear), or click Add Actions to Toolbar… in the sheet. Star an action or snippet, or drag it into the list of favorites, and it gets a button of its own in the toolbar of every document it applies to.