Search code examples
gitintellij-ideacode-snippets

Sharing IntelliJ code snippets amongst team (using Git)


I know that IntelliJ has "Live Templates" that allow the developer to have a number of keywords that expand into a block or snippet of code. The IDE comes with a number of useful keywords, but our team would like to:

  1. Create custom keywords + code snippets.
  2. Share those code snippets between developers.

Proposal: One of the developers could set up the code snippets using IntelliJ's live templates UI, export those settings to an XML file, commit it the team's repo, periodically everyone pulls the file and imports the updated code snippets to their IntelliJ's.

Is there a more efficient way to achieve this? One of the flaws of the proposed solution is that developers cannot have their own snippets (because they'll be replaced on the next import).


Solution

  • Your solution is the best one. The disadvantage you mention is not correct as when importing live templates from xml file, it only adds imported ones on top of those you already have so it does not override your custom live templates.

    See Exporting and Importing Settings for more detailed explanation. This allows you to import not only live templates, but also code formatting rules and other settings, which is good to have uniform among entire team.


    Alternatively you can Share Live Templates directly:

    To create an XML copy of live templates

    1. Open the Live Templates page of the Settings/Preferences dialog.
    2. Select the desired templates. Use Ctrl and Shift keys for multiple selection.
    3. Right-click the selection, and choose Copy on the context menu. An XML copy of the selected templates is created in the system clipboard.

    To paste the live templates

    1. In the Live Templates page of the Settings/Preferences dialog, select the group where you want to insert the copied live templates.

    2. Right-click the selection, and choose Paste on the context menu.

    3. An XML copy of the selected templates is inserted from the system clipboard to the selected group of live templates.