9

Is it possible to incorporate variables into snippets in Atom? This comes in handy with for loops for example, when you want to pre-fill spots that are about to come.

The snippets.cson entry with java as a source I would imagine. Unfortunately it is not working.

'.source.java':
    'For-Loop':
        'prefix': 'fori'
        'body': 'for (int ${1:VAR} = $2; ${VAR} < $3; ${VAR}++) {\n\t$3\n}'

2 Answers 2

13

Current version of Atom can support this kind of snippet. You can just put $1 at every places you want.

'.source.coffee':
  'For-Loop':
    'prefix': 'fori'
    'body': 'for (int ${1:i} = $2; ${1:i} < $3; ${1:i}++) {\n\t$4\n}'
Sign up to request clarification or add additional context in comments.

1 Comment

As this is now supported, this is the right answer.
10

Update 2016-09

This is now supported in Atom - please see the other answer for how to use it. I can't delete this answer since it is the accepted one...

Old Answer

The documentation for the snippets package has an example that shows how to predefine the default value for a variable:

'.source.js':
  'console.log':
    'prefix': 'log'
    'body': 'console.log(${1:"crash"});$2'

In the above example, crash is used as the default value for the log statement, allowing you to provide your own value by overwriting the crash default.

I guess what you're asking is whether there's a way to automatically use a value you typed and then apply it to the other instances of the same placeholder. So in your example, when you're at variable $1 and type foo, that it uses foo for all other occurrences of ${VAR}, right?

That's currently not possible from what I understand. I suggest you open a feature request at the snippets package's repo, or even better take a stab at adding this functionality and then creating a pull request.

Sorry if this doesn't help you, but you asked whether there was a way of doing this with the current version of Atom. The answer seems to be No at the present time.

4 Comments

Well. At least I know. Thanks!
This is now supported.
Thanks for the update - I was not aware that this was added.
FYI, for me ${1:"crash"} defaults to "crash" and not crash (ie, don't quote the default values unless you want those quotes). Also note that multiline snippets are possible using ''' and """.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.