21

I am using Sublime Text to code my website where I have a JavaScript file with a lot of functions. I use those functions quite frequently and every time I do, I have to type the whole function out.

I noticed that for each function, I could create a Sublime Text snippet with a shortcut. However there is a huge list of functions and they keep changing.

Is there a way where in I could just import this JavaScript file and this snippet file is created, such that I have my autocompletes ready to use?

venimus
  • 5,907
  • 2
  • 28
  • 38
Neophile
  • 5,660
  • 14
  • 61
  • 107
  • This isn't a question about programming. Try posting it on [SuperUser](http://superuser.com/) instead. – MaxArt Dec 08 '15 at 12:30
  • 5
    @MaxArt From [What topics can I ask about here?](http://stackoverflow.com/help/on-topic) in the [help], software questions are allowed if they cover "*[...] software tools commonly used by programmers*". Sublime Text, like vim, emacs, Notepad++, etc., is a programming editor, and there are [tens of thousands of questions](http://stackoverflow.com/questions/tagged/sublimetext2+or+sublimetext3+or+sublimetext+or+vim+or+vi+or+emacs+or+notepad%2b%2b) about them on this site that are perfectly on-topic. – MattDMo Dec 08 '15 at 20:06
  • Sublime text is python based right if we want to develop a plugin? – Neophile Dec 09 '15 at 09:24
  • any update from your work that we can use?? – prieston Jan 29 '16 at 23:53

3 Answers3

11

A simple snippet that creates three opening and closing p tags:

<snippet>
    <content>
      <![CDATA[
  <p>
    $1
  </p>
  <p>
    $2
  </p>
  <p>
    $3
  </p>
      ]]>
    </content>
  <tabTrigger>p3</tabTrigger>
  <scope>text.html</scope>
</snippet>

Save it as html-p3.sublime-snippet in (Mac OS X) /Users/yourname/Library/Application Support/Sublime Text 2/Packages/User and you can enter p3+tab to create three <p> tags. The $1, $2, $3 are where your cursor will jump after you press tab. This allows you to easily add content without having to select manually.

This great blog post explains everything you need to know about Sublime Text snippets:

You can use snippets for CSS as well as HTML (actually, you can use snippets with any language or text that works inside Sublime Text).

To summarize, you can put all of your function snippets in between the <snippet><content><![CDATA[ *content here*]]></content></snippet> and save it as a .snippet file in the default preferences folder of Sublime Text.

Palec
  • 12,743
  • 8
  • 69
  • 138
Elon Zito
  • 2,872
  • 1
  • 24
  • 28
  • Hello @Elon: Thanks for your answer. I have already investigated what you have mentioned above and the example you provided involves me writing the snippet myself rather than just dumping the javascript file and getting the snippet on the other end. This would also need changing every time because the list of functions keep growing and I wouldn't want to keep updating it all the time. I'd rather prefer to upload a file and let sublime text do the work for me. Is there something similar? – Neophile Dec 17 '15 at 09:56
  • Is it hard to generate such snippets via a script, @TheNewbie? – Palec Dec 18 '15 at 09:23
  • Well I was rather thinking of developing a plugin that would do this automatically based on a file. That would be a great project I think. – Neophile Dec 18 '15 at 12:27
  • 1
    I don't imagine it would be a difficult thing to do to have the code automatically wrapped. I imagine the snippet shortcut name would be whatever the function's name is. I'd be happy to work with you on creating a Sublime Text plugin that does this. Feel free to Skype me - simsketch or simsketch@gmail.com. – Elon Zito Dec 18 '15 at 15:40
  • 1
    Ok lets do this. I've sent you a request on skype. – Neophile Dec 20 '15 at 21:18
3

Sublime Text should automatically autocomplete the function names (not the parameters) if everything is in one big file. The only possible issue I can think of is that Sublime Text doesn’t recognize the file type. Check if View → Syntax is set to JavaScript.

If you want full autocompletion with parameters, try Tern for Sublime.

Palec
  • 12,743
  • 8
  • 69
  • 138
Fels
  • 1,214
  • 2
  • 13
  • 27
2

You can try javascript plugin for sublime which will help in auto completion while writing code in js. Here is the list of javascript plugin:

http://www.sitepoint.com/essential-sublime-text-javascript-plugins/

Here is the way to setup plugin in sublime:

How to install plugins to Sublime Text 2 editor?

Community
  • 1
  • 1
Dipitak
  • 97
  • 1
  • 1
  • 3