Search code examples
intellij-idea

How to auto-generate comment using custom shortcut in IntelliJ?


I'm trying create a custom shortcut in IntelliJ to generate some predefined comments.

For example, if I write //-- and press enter, it should auto-generate:

//------------------------------------------------------------------------------//

//------------------------------------------------------------------------------//

The Postfix Completetion feature looks like it could be used for this but I didn't figure out how to set it up for this scenario. Can someone help?

If Postfix Completetion is not the answer, can I do it with another feature?


Solution

  • You can create a Live Template (Preferences | Editor | Live Templates). For example, a template like the following:

    //------------------------------------------------------------------------------//
    $END$
    //------------------------------------------------------------------------------//
    

    The $END$ indicates where the text cursor will be placed when the Live Template text is inserted. At the bottom of the Live Template settings screen you can configure in which languages/contexts the Live Template should be available.

    Preferences | Editor | Live Templates