× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Sublime Code Snippets : Autogenerate Java key constants - an example

Arnav Gupta
Nov 15, 2015
<p>I largely use [Jetbrains](http://jetbrains.com) products for my day to day development work (which is mostly Android). I really love the _Live Template_ feature that is available on IntelliJ, for example if I type `psfs` and press `Tab` I get the following on the screen<br> ```java<br> public static final String<br> ```</p> <p>These days, sometimes I use Sublime Text 3 as well, because unless I need all the features of an IDE, it's too heavy to open up, especially if other stuff is running on the laptop. One of those days when I was using Sublime, I needed to create a lot of Database key constants - i.e. in the form of<br> ```java<br> public static final String KEY_ONE = "key_one"<br> ```</p> <p>So it so happens that there exists functionality like live templates on Sublime as well, in form of **Snippets**</p> <p>To create a new Snippet, go to Tools -&gt; New Snippet.<br> Ideally you save your snippets in `~/.config/sublime-text-3/Packages/User/MySnippet.sublime-snippet`</p> <p>Here is an example config for the Snippet that creates Java key constants</p> <p>```xml<br> &lt;snippet&gt;<br>     &lt;content&gt;public static final String ${1/(.+)/(\U\1)/g} = "${1}";&lt;/content&gt;<br>     &lt;tabTrigger&gt;jkey&lt;/tabTrigger&gt;<br>     &lt;scope&gt;source.java&lt;/scope&gt;<br>     &lt;description&gt;public static final String KEY = "key"&lt;/description&gt;<br> &lt;/snippet&gt;<br> ```</p> <p>Now when you type `jkey` and press tab, it creates<br> ```java<br> public static final String | = "|"<br> ```</p> <p>The pipes '|' represent the cursor position. When you start typing in lowercase for example `my_key_one`, automatically the variable name updates in uppercase `MY_KEY_ONE`.</p>
comments powered by Disqus