| 📦 Turbo Golo d710c1b k33g yesterday | 1 | # How to insert snippets from a menu |
| 2 | |
| 3 | This guide shows how to set up reusable pieces of text and put them into a file at the cursor. It assumes Turbo Golo is already installed. |
| 4 | |
| 5 | ## Get a starter file |
| 6 | |
| 7 | Start the editor **from the project's own directory**, then choose **Snippets ▸ Create snippets file** (`Alt-N`, then `C`). |
| 8 | |
| 9 | That writes `.turbo-golo/snippets.toml`, filled in with a dozen Golo constructs and a couple of general ones, and opens it — coloured, because Turbo Golo colours TOML: |
| 10 | |
| 11 | ```toml |
| 12 | [[snippet]] |
| 13 | name = "main" |
| 14 | group = "Golo" |
| 15 | languages = ["golo"] |
| 16 | body = ''' |
| 17 | function main = |args| { |
| 18 | println("Hello, Golo!") |
| 19 | }''' |
| 20 | |
| 21 | [[snippet]] |
| 22 | group = "General" |
| 23 | name = "Hello" |
| 24 | body = "Hello!!!" |
| 25 | ``` |
| 26 | |
| 27 | Each `[[snippet]]` becomes one line of the menu. The file is read every time the menu opens, so editing it takes effect immediately — no restart. |
| 28 | |
| 29 | ## Insert one |
| 30 | |
| 31 | Open **Snippets** (`Alt-N`). Snippets sharing a `group` appear together in a submenu of that name; one with no group goes into **General**. |
| 32 | |
| 33 | | Key | Effect | |
| 34 | | --- | --- | |
| 35 | | `Alt-N`, or `F10` then `→` to Snippets | Open the menu | |
| 36 | | `↑` `↓` | Move down the groups | |
| 37 | | `→`, or `Enter` | Open the highlighted group | |
| 38 | | `↑` `↓` then `Enter` | Insert the highlighted snippet | |
| 39 | | `←` | Back out of a group | |
| 40 | | `Escape` | Put the whole menu away | |
| 41 | |
| 42 | The snippet goes in at the cursor. **Lines after the first are indented to match the line you inserted it on**, so a multi-line snippet dropped into a nested block lands where you would have typed it: |
| 43 | |
| 44 | ``` |
| 45 | function main = |args| { |
| 46 | | ← cursor here |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | becomes, after **Golo ▸ foreach**, |
| 51 | |
| 52 | ``` |
| 53 | function main = |args| { |
| 54 | foreach item in list[1, 2, 3] { |
| 55 | println(item) |
| 56 | } |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | It is one undo step: `Ctrl-Z` takes the whole snippet back out. |
| 61 | |
| 62 | ## Keep snippets across every project |
| 63 | |
| 64 | Put them in `~/.config/turbo-golo/snippets.toml` — the same directory your own themes go in. Those appear in every project, and a project's own file adds to them rather than replacing them. |
| 65 | |
| 66 | Where a project and you use the same `name` in the same `group`, **the project's wins**: it is the more specific statement of the two. |
| 67 | |
| 68 | ## Show a snippet only where it makes sense |
| 69 | |
| 70 | Add `languages`, using the names the editor uses — `golo`, `toml`, `markdown`, `javascript`, `html`, `bash`: |
| 71 | |
| 72 | ```toml |
| 73 | [[snippet]] |
| 74 | name = "strict mode" |
| 75 | group = "Shell" |
| 76 | languages = ["bash"] |
| 77 | body = "set -euo pipefail" |
| 78 | ``` |
| 79 | |
| 80 | That snippet then appears only when a shell script is the front window. Leave `languages` out and the snippet is offered everywhere, which is what you want for a licence header or a `TODO`. |
| 81 | |
| 82 | A group left with nothing after filtering does not appear at all. |
| 83 | |
| 84 | ## Write a Golo body |
| 85 | |
| 86 | Two habits, both taken from the starter file: |
| 87 | |
| 88 | - **Indent with two spaces.** It is what every example in the GoloScript documentation uses, and Golo has no formatter to disagree with, so it is the only convention there is. |
| 89 | - **Use a literal string** — `'''…'''`, single quotes — for any body with a backslash in it. A Golo string carries `\n` and `\"` the way a Go string does, and in a TOML basic string (`"""…"""`) those escapes are resolved before the editor sees them: `println("caught: \"" + e + "\"")` would arrive with real quotation marks in it and no longer parse. Inside `'''` a backslash is just a backslash. |
| 90 | |
| 91 | ## Variants |
| 92 | |
| 93 | - **You started the editor from a subdirectory.** The project's file is not found: only `./.turbo-golo` is looked at, the same rule `settings.toml` follows. Your own snippets still appear. |
| 94 | - **The file has a mistake in it.** The menu shows a greyed-out `Cannot read snippets` where the groups would be, and **Create snippets file** is still there. Open the file and fix it. |
| 95 | - **You want a tab in a body.** In a basic string, write `\t` and TOML turns it into a tab when it reads the file. In a literal string, `\t` stays two characters — type the tab itself. |
| 96 | |
| 97 | ## See also |
| 98 | |
| 99 | - Every key of the file and every rule: [Snippets reference](../reference/snippets.md) |
| 100 | - Why the menu is rebuilt each time it opens, why insertion re-indents, and why the Golo bodies are literal strings: [Snippets](../explanation/snippets.md) |
| 101 | - The other file in `.turbo-golo`: [Project settings](../reference/project-settings.md) |