Share and Use Code Snippets

A Snippet hosts reusable code in files that you can use in the project and share with other project members. The file could include a small block of reusable source code or text that can be incorporated into larger modules.

Content in snippet files may not be code, but it should be useful. This content could be notes that you want to share with project members, or something you want to keep private, such as a reminder to yourself. If a snippet is shared, project members can copy or download the snippet files and then use them in their own applications.

A snippet can contain several files. When you create a snippet, you can add one file only, but you can add additional files after creating the snippet.

Create and Manage Snippets

You can create a snippet from the Snippets page or from a text selection in a code editor. You can only add one file when you create the snippet, but you can add more files later.

This table describes how you can create and manage a snippet.
Action How To

Create a snippet

  1. In the navigation bar, click Snippets Snippets.
  2. Click + Create Snippet.

  3. On the Snippet Details page of the New Snippet wizard, enter the snippet name and description.

  4. In Visibility, select Private if you don’t want to share the snippet’s files and keep them for personal use. Select Shared to share the snippet’s files with project members.

  5. To edit the snippet and add more files immediately after creating the snippet, select the Edit snippet when finished check box.

  6. Click Next.

  7. In the Snippet Content page of the New Snippet wizard, enter content for the default file of the snippet.

    If you don’t enter content, an empty file snippet1.txt is added to the snippet.

  8. Click Finish.

Create a snippet from a selection

You can create a snippet from a file that’s open in the code editor.

  1. In the open file, select the text.

  2. Right-click and select New Snippet from Selection.

  3. On the Snippet Details page of the New Snippet wizard, enter the snippet name and description.

  4. In Visibility, select Private if you don’t want to share it and keep for personal use. Select Shared to share the snippet with project members.

  5. To edit the snippet and add more files immediately after creating the snippet, select the Edit snippet when finished check box.

  6. In the Snippet Content page of the New Snippet wizard, enter content for the default file of the snippet.

    If you don’t enter content, an empty file is added to the snippet.

  7. Click Finish.

Share or stop sharing a snippet

After creating a snippet you may want to share it with your team members, or stop sharing it if it is already shared. You can set the share status of a snippet that you own.

To share a snippet, in the My Snippets view of the Snippets page, click Share Share. The icon changes to Shared Shared. To stop sharing a snippet, click Shared Shared. The icon changes to Private Private.

Edit a snippet’s title

You can edit the title of a snippet that you own. On the Snippets page, click the snippet name, and then click Edit the Edit icon.

Delete a snippet

You can delete a snippet that you own. On the Snippets page, click the snippet name, and then click Delete. In the Delete Snippet dialog box, click Yes to confirm.

Add and Manage Files of a Snippet

You must be the creator of the snippet to add or manage its files.

Action How To

Add a file

  1. Open the snippet.

  2. Scroll down and after the snippet’s files, click Add File.

  3. In the header, enter the file name with extension. In the editor, enter the file’s content.

    The editor supports various code editing features such as autocomplete, indentation, syntax highlighting, code folding, and bracket matching.

  4. After adding the content, scroll up and at the top of the page, click Save.

    To save the updates and stay on the Edit Snippet page, select Save To save the updates and exit, select Save and Exit.

Edit a file

  1. Open the snippet.

  2. If necessary, rename the file and configure its properties.

  3. In the editor, update the file contents.

  4. At the top of the page, click Save.

    To save the updates and stay on the Edit Snippet page, select Save To save the updates and exit, select Save and Exit.

Delete a file

  1. Open the snippet.

  2. For the file that you want to delete, on the right side of the file header, click Remove File the Delete icon.

  3. In the Delete Snippet File dialog box, click Yes to confirm.

  4. At the top of the page, click Save.

    To save the updates and stay on the Edit Snippet page, select Save To save the updates and exit, select Save and Exit.

Copy Contents of a Snippet File

You can copy contents of a snippet file manually from the Snippets page or insert it from the context menu in the code editor component.

The code editor is available in various editing pages and input fields such as the Edit Wiki page, edit mode of the readme file, snippet file editor, merge request comment box, and the shell command box of the Configure Build page.

Action How To

Copy from the Snippets page

  1. Open the snippet.

  2. For the file whose contents you want to copy, click Copy the copy icon.

    In some browsers, you must press Ctrl+C to copy the content to the clipboard after clicking Copy the copy icon.

  3. Paste the contents into the text field.

Insert from the context menu

  1. In the code editor, right-click, and select Insert from Snippet.

  2. In the Select Snippet page of the Insert From Snippet wizard, select the snippet, and click Next.

  3. In the Select File page, click the file name whose contents you want to insert.

  4. Click Finish.

Add a Comment to a Snippet

Add a comment to a snippet to share information with other developers.

To add a comment to a snippet:
  1. Open the snippet.
  2. Scroll down to the Comments section.
  3. Enter the comment in the comment box.
    Use the project’s wiki markup language to format the comment.
  4. Click Add.

Use Git with Snippets

You can use Git to clone a snippet repository and manage its files. After you clone the snippet’s repository, you can view the file history, and update and commit files locally. However, you can only push updates to repositories for snippets that you own.

  1. Open the snippet.
  2. On the Snippet Details page, at the top, click Clone, and then click Copy the copy icon to copy the HTTP or the SSH URL of the snippet repository.
  3. Use Git commands to clone the repository, update files, and push the commits to the project.

Download an Archive of the Snippet

You can download a zip or a tgz file of the snippet to your computer. You may want to do this if you back up the files of the snippet before deleting it.

  1. Open the snippet.
  2. On the Snippet Details page, at the top, click Clone, and the select Download ZIP or the Download TGZ option.
The downloaded file is an archive file that contains the latest content of all the files in the snippet. To view previous versions of the files, you must clone the snippet repository, and then use Git commands to show the history of the files.