Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete of custom JavaScript functions in Sublime Text

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?

like image 962
Neophile Avatar asked Dec 08 '15 12:12

Neophile


People also ask

How do I enable autocomplete in Sublime Text?

Usage. By default, Sublime Text will automatically show the completions popup when a user is editing source code or markup, but not within prose in comments, strings or markups. Pressing the Esc key will hide the completions popup. To manually show the completions popup, press Ctrl+Space.


2 Answers

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.

like image 112
Elon Zito Avatar answered Nov 08 '22 13:11

Elon Zito


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.

like image 3
Fels Avatar answered Nov 08 '22 11:11

Fels