Snippets in Onivim 2
Snippets are text templates that simplify entering blocks of source code. Snippets are especially useful for code blocks that are repeated frequently with minor variations.
In insert mode, as you type, Onivim shows available snippets alongside completion items.
The snippet syntax follows the Code Snippet Syntax (which is a subset of the TextMate Snippet Syntax). In addition, Onivim supports many of the same snippet configuration options as Visual Studio code, such as "editor.snippetSuggestions"
.
Using snippets
When a snippet is selected in the completion pop-up, pressing Tab will expand the snippet.
While a snippet is active, the mode will show as Snippet
in the bottom-right, on the status bar. Pressing Tab while a snippet is active will move the cursor to the next placeholder, while pressing Shift+Tab will move the cursor to the previous placeholder.
Once the cursor has been moved to the final placeholder, snippet mode will deactivate, and the editor will be returned to insert mode..
Installing snippets
Onivim 2 comes bundled with snippets for several languages, including JavaScript, TypeScript, Reason, and others.
Extensions from open-vsx can be installed to provide additional snippets:
You can explore extensions that provide snippets by using the @category:"snippets"
search filter in the extensions pane.
Snippet syntax
Onivim's snippet syntax follows the same snippet syntax as Code, including:
Tabstops:
$1
,$2
, ...$n
designate cursor positions within the snippet.$0
is a special tabstop, designating the final cursor position. If there are multiple instances of the same tabstop, they will be synchronized and updated together.Placeholders: Default values can be specified for tabstops, like
${1:default}
.Variables:
$name
specifies a variable that can be inserted, such as$CURRENT_YEAR
.
Customizing snippets
Snippets may be customized globally or per-filetype - these are stored in the user snippets
folder:
- If the snippet file ends with extension
.json
, the filename is the relevant filetype - for example,bat.json
provides snippets for thebat
filetype. - If the snippet file ends with extension
.code-snippets
, the snippets file applies globally, to all filetypes.
To configure your snippets:
- Open the command palette (Control+Shift+P / Command+Shift+P)
- Select "Configure user snippets"
- Choose the filetype to modify
- Press Enter to open the relevant snippet file
- Modify and save
Binding a snippet to a key
Snippets can also be bound to keys, using the "editor.action.insertSnippet"
command, for example: