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?

4
  • This isn't a question about programming. Try posting it on SuperUser instead. Commented Dec 8, 2015 at 12:30
  • 5
    @MaxArt From What topics can I ask about here? in the help center, 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 about them on this site that are perfectly on-topic. Commented Dec 8, 2015 at 20:06
  • Sublime text is python based right if we want to develop a plugin? Commented Dec 9, 2015 at 9:24
  • any update from your work that we can use?? Commented Jan 29, 2016 at 23:53

3 Answers 3

11
+25

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
Is it hard to generate such snippets via a script, @TheNewbie?
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.
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 [email protected].
Ok lets do this. I've sent you a request on skype.
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.

Comments

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?

1 Comment

Yes I'm aware of this plugin but this is not what I want.

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.