Search code examples
javascriptcoffeescriptatom-editorcode-snippets

Atom snippets: Multiple line snippets in atom editor


I recently switched to Atom text editor for programming from Geany text editor. I have lots of python snippets in geany. In geany writing snippets is super easy.

For example:

# for geany text editor (snippets.conf file)  
h=# Author : Bhishan Poudel\n# Date   : {date}\n  

If I type h and then hit enter, i will get the above snippet with current time.

How can we do so in ATOM?

My attempt so far is this:
I edited the snippets.cson file like this:

'.source.python':
  'example1':
    'prefix': 'h'
    'body': '
      #!/usr/bin/env python\n
      # -*- coding: utf-8\n
      #\nDate: {date}\n
      #Imports\n
      import numpy as np
      '

But, this did not work well.

Related links are:
Atom editor: snippet for inserting a timestamp
http://flight-manual.atom.io/using-atom/sections/snippets/
Atom Editor: multiple snippets


Solution

  • I have a solution for the multiline part of your question and a suggestion for the naming. What i did was this:

    '.source.python':
      'header and imports for python':
        'prefix': 'pyhead'
        'body':"""
      #!/usr/bin/env python
      # -*- coding: utf-8
      #Date: $1
      #Imports
      import numpy as np
      $2
      """
    

    the $1 indicates, that you jump to this is after the snipped is inserted. So that way you could file in the date yourself (not optimal I know). The $2 will be the place for the next TAB key. The other part is, that you use a prefix which is easy to identify. The multiline part is done with the """ at the beginning and end of the body so everything should be inserted smoothly