Chapter 13 · Pro modulesPro 模块Pro 模組

Code snippets代码片段程式碼片段

Categories, placeholders, insertion and how snippets differ from macros.分类、占位符、插入方式,以及片段与宏的区别。分類、佔位符、插入方式,以及片段與巨集的區別。

Pro

What a snippet is

A stored piece of text with optional placeholders. Inserting it writes the text at the caret and lets you tab through the placeholders.

Placeholder syntax

func ${1:name}(${2:args}) -> ${3:Type} {
    // ${4:body}
}

${1:…} marks the first field, ${2:…} the second, and so on. The text after the colon is the default value, which stays selected so typing replaces it.

Organising

  • Snippets live in categories, so SQL and Swift snippets do not mix.
  • Each snippet has a name, a body and optionally a trigger shortcut.
  • Editing a snippet updates future insertions; text already inserted is not retroactively changed.

Inserting

Pick the snippet from the snippets panel, or use its trigger shortcut. The insertion is a normal editing operation and can be undone with ⌘Z.

Snippet vs macro

SnippetMacro
InputStored textRecorded keystrokes
VariablesPlaceholdersNone
Repeatable on changing contentYesOnly literally

Limits

  • No regex transforms, no date/time expansion and no clipboard variables.
  • Placeholder numbering is flat; nested or mirrored tab stops are not supported.

Pro

什么是代码片段

一段保存下来的文本,可带占位符。插入时写入光标处,并允许用 Tab 在占位符之间跳转。

占位符语法

func ${1:name}(${2:args}) -> ${3:Type} {
    // ${4:body}
}

${1:…} 是第一个字段,${2:…} 是第二个,依此类推。冒号后的内容为默认值,插入后保持选中,直接输入即可替换。

组织方式

  • 片段按分类管理,SQL 与 Swift 的片段不会混在一起。
  • 每个片段有名称、内容,以及可选的触发快捷键。
  • 修改片段只影响之后的插入,已经写入正文的内容不会追溯变更。

插入

在片段面板中选择,或使用触发快捷键。插入就是普通编辑操作,可用 ⌘Z 撤销。

片段与宏的差别

片段宏
输入存储的文本录制的按键
变量占位符无
可作用于变化的内容可以只能逐字重放

限制

  • 没有正则变换、没有日期时间展开、没有剪贴板变量。
  • 占位符编号是平铺的,不支持嵌套或镜像制表位。

Pro

什麼是程式碼片段

一段儲存下來的文字,可帶佔位符。插入時寫入游標處,並允許用 Tab 在佔位符之間跳轉。

佔位符語法

func ${1:name}(${2:args}) -> ${3:Type} {
    // ${4:body}
}

${1:…} 是第一個欄位,${2:…} 是第二個,依此類推。冒號後的內容為預設值,插入後保持選取,直接輸入即可取代。

組織方式

  • 片段按分類管理,SQL 與 Swift 的片段不會混在一起。
  • 每個片段有名稱、內容,以及可選的觸發快速鍵。
  • 修改片段只影響之後的插入,已經寫入正文的內容不會追溯變更。

插入

在片段面板中選擇,或使用觸發快速鍵。插入就是普通編輯操作,可用 ⌘Z 復原。

片段與巨集的差別

片段巨集
輸入儲存的文字錄制的按鍵
變數佔位符無
可作用於變化的內容可以只能逐字重放

限制

  • 沒有正則變換、沒有日期時間展開、沒有剪貼簿變數。
  • 佔位符編號是平鋪的,不支援嵌套或鏡像制表位。