| 📦 Turbo Go 3d7798b k33g 13h ago | 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 Go 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-go/snippets.toml`, filled in with a few worked examples, and opens it — coloured, because Turbo Go colours TOML: |
| 10 | |
| 11 | ```toml |
| 12 | [[snippet]] |
| 13 | name = "if err != nil" |
| 14 | group = "Go" |
| 15 | languages = ["go"] |
| 16 | body = """ |
| 17 | if err != nil { |
| 18 | return err |
| 19 | }""" |
| 20 | |
| 21 | [[snippet]] |
| 22 | name = "TODO" |
| 23 | body = "TODO: " |
| 24 | ``` |
| 25 | |
| 26 | 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. |
| 27 | |
| 28 | ## Insert one |
| 29 | |
| 30 | Open **Snippets** (`Alt-N`). Snippets sharing a `group` appear together in a submenu of that name; one with no group goes into **General**. |
| 31 | |
| 32 | | Key | Effect | |
| 33 | | --- | --- | |
| 34 | | `Alt-N`, or `F10` then `→` to Snippets | Open the menu | |
| 35 | | `↑` `↓` | Move down the groups | |
| 36 | | `→`, or `Enter` | Open the highlighted group | |
| 37 | | `↑` `↓` then `Enter` | Insert the highlighted snippet | |
| 38 | | `←` | Back out of a group | |
| 39 | | `Escape` | Put the whole menu away | |
| 40 | |
| 41 | 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: |
| 42 | |
| 43 | ``` |
| 44 | func f() { |
| 45 | | ← cursor here |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | becomes |
| 50 | |
| 51 | ``` |
| 52 | func f() { |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | It is one undo step: `Ctrl-Z` takes the whole snippet back out. |
| 60 | |
| 61 | ## Keep snippets across every project |
| 62 | |
| 63 | Put them in `~/.config/turbo-go/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. |
| 64 | |
| 65 | 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. |
| 66 | |
| 67 | ## Show a snippet only where it makes sense |
| 68 | |
| 69 | Add `languages`, using the names the editor uses — `go`, `toml`, `markdown`, `javascript`, `html`, `bash`: |
| 70 | |
| 71 | ```toml |
| 72 | [[snippet]] |
| 73 | name = "strict mode" |
| 74 | group = "Shell" |
| 75 | languages = ["bash"] |
| 76 | body = "set -euo pipefail" |
| 77 | ``` |
| 78 | |
| 79 | 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`. |
| 80 | |
| 81 | A group left with nothing after filtering does not appear at all. |
| 82 | |
| 83 | ## Variants |
| 84 | |
| 85 | - **You started the editor from a subdirectory.** The project's file is not found: only `./.turbo-go` is looked at, the same rule `settings.toml` follows. Your own snippets still appear. |
| 86 | - **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. |
| 87 | - **You want tabs in a body.** Write `\t`, as the starter file does — TOML turns it into a tab when it reads the file. |
| 88 | |
| 89 | ## See also |
| 90 | |
| 91 | - Every key of the file and every rule: [Snippets reference](../reference/snippets.md) |
| 92 | - Why the menu is rebuilt each time it opens, and why insertion re-indents: [Snippets](../explanation/snippets.md) |
| 93 | - The other file in `.turbo-go`: [Project settings](../reference/project-settings.md) |