Snippets
Snippets provide a convenient way of inserting repetitive portions of code into your files. For example, an HTML snippet for the <img> tag would insert the opening and closing portions of the tag, as well as fixed places where you can enter image attributes. This can make writing code easier and faster.
Enable Snippets Plugin
To enable the Snippets plugin:
Select gedit ▸ Preferences ▸ Plugins.
Select Snippets to enable the plugin.
Browse Snippets
To browse available snippets:
-
Select Tools ▸ Manage Snippets.
Make sure the syntax is set appropriately. The status bar at the bottom of the gedit window will show the current language setting. This language setting is what allows gedit to insert the proper snippets. Gedit will normally detect the language or syntax of the file that you're using, but sometimes you may need to set it.
Browse the snippets that are available for your language or syntax.
Using Snippets
To insert a snippet into your current document:
Type the desired snippet name anywhere in your current document.
Press Tab to insert the snippet associated with the input term.
Snippet Example Usage
For example, if you have set the syntax to HTML, type head , and press the Tab key. The text would have auto completed into a snippet as:
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
</head>
Adding Snippets
You can add new Snippets that work either globally (that is, they are language agnostic) or only in a particular syntax.
Select Tools ▸ Manage Snippets.
-
From the Manage Snippets sidebar select the desired syntax and click on the "+" button to list existing snippets for the selected syntax.
To add a snippet that works globally (that is, when no particular syntax or language has been set for the document), select Global from the Manage Snippets sidebar.
To add a new snippet, click on the "plus" icon in the Manage Snippets sidebar.
Input a name for the new snippet. The name of the snippet can be different from the snippet you want to add.
-
Under the Activation section, you must input a term for the Tab trigger: textarea. You will be using this term to insert your snippet.
If you wish to insert a snippet with a keyboard shortcut, then click your mouse pointer on Shortcut key: textarea and press the desired combination of keys. Once you have pressed and released the keys, the keyboard shortcut will be set.
Under the textarea for Edit:, input the desired text for the snippet. You may use ${n} to indicate variables, where n is replaced with a number that represents the numerical order of variables included.
New Snippet Example
Consider you have created a snippet with the term greetings in Tab trigger:. The Edit: textarea contains:
<greetings>Hello ${1:wonderful}${2:amazing} world!
</greetings>
You can insert this snippet by typing greetings in the document and pressing the Tab key. The following text snippet will be inserted:
<greetings>Hello wonderfulamazing world!</greetings>
The words "wonderful" and "amazing" can be retained or deleted depending on your choice of usage. To do so, press Tab to choose between "wonderful" and "amazing" and press Del to delete the least preferred option.