# 四、使用片段

## 2. 通过命令使用片段

insertSnippet 命令需要指定一个片段 id 做为参数，所谓 id ，就是在片段列表里，片段标题旁边的那一串字符（比如snippet_1578537460214）。

## 3. 片段脚本

### 3-2. 片段脚本示例

1. 1当前时间是 <% print(moment().format('YYYYMMDDHHmmss秒'))%>

## 4. Ace 编辑器片段语法

### 4-1. Insertion Points

After inserting the snippet text the cursor will move to the first position after the inserted text, when there are no insertion points specified. Specifying insertion points using the $character and a number to specify their sequence. 1. 1${1}

2. 2${2} 3. 3etc The${0} insertion point is always last in line. You can specify the text that is selected when jumping from one insertion point to the next one.

1. 1${1:some_text} In addition, it's possible to reuse the same variable multiple times. All instances will be selected using multiple cursors and changed at the same time when the user types. 1. 1for (var${1:i}=0; $1 <${2}; $1++) { 2. 2${0}

3. 3}

### 4-2. Using Special Variables

When triggering a snippet through a menu or command you can configure it to use the text selected prior to inserting the snippet in the resulting code.

1. 1(function(${1}) { 2. 2${0:${SELECTED_TEXT:/* code */}} 3. 3}(${1}));

Similarly the following variables can be used to insert other special text.

CURRENT_WORD The word at the cursor at the.
SELECTION The selected text.
SELECTED_TEXT The selected text.
CURRENT_LINE The current line.
PREV_LINE The previous line.
LINE_INDEX The index of the current line.
LINE_NUMBER The line number (index + 1).
SOFT_TABS "YES" or "NO".
TAB_SIZE The tab size.

## 5. Codemirror 编辑器片段语法

1. 1for (var ${index} = 0;${index} < ${array}.length;${index}++) {

2. 2var ${array_element} =${array}[${index}]; 3. 3${cursor}

4. 4}

code snippet is inserted in the editor :

1

The first variable index is selected. If you modify it, it modifies the other index variables :

2

If you key Tab, you select the next variable to replace (here array) that you can replace it too :

3

To quit the template mode, use Esc or type any code in the editor :

4