Override the default tab autocomplete content in Sublime Text 3 - sublimetext3

What's the correct way to override the JavaScript "fun" tab completion for example?
I've created a new file in my user package called: my.sublime-completions and I understand that file requires two properties (scope & completions)?
Thanks in advance for the help :)

The documentation on completions and the two See Also links at the top should provide plenty of detail on how to structure your .sublime-completions file for use in JavaScript. For example, the following exactly replicates the default snippet (use \n for newlines and \t for tabs, if needed):
{
"scope": "source.js",
"completions":
[
{ "trigger": "fun", "contents": "function ${1:function_name} (${2:argument}) {\n ${0:// body...}\n}" }
]
}
The easiest way to override the default fun JavaScript snippet is to first install the PackageResourceViewer plugin via Package Control. Once that's set up, restart Sublime and open the Command Palette with ⌘⇧P (OS X) or CtrlShiftP (Windows/Linux). Type prv to bring up the PackageResourceViewer options, and choose PackageResourceViewer: Open Resource. Scroll down the list and select JavaScript, then open the function-(fun).sublime-snippet file, using XML for syntax highlighting if you wish. If you are using a more recent version of Sublime, the file may be located under JavaScript/Snippets.
Next, set the contents of the file to the following:
<snippet>
<content><![CDATA[]]></content>
<tabTrigger>fun</tabTrigger>
<scope>source.js</scope>
<description>Don't Use</description>
</snippet>
Save the file, and you should be all set. Assuming your .sublime-completions file is set up correctly, when you type fun in a JS file you should only see your completion, not the default one.
Good luck!

Related

How to configure Sublime Text 3 editor settings for specific file extensions?

I want to change some editor settings that's only applicable to certain file extensions.
As a test I created 2 files with these contents (in essence overriding what I have as default):
> cat Preferences.sublime-settings
{
"tab_size": 2,
"translate_tabs_to_spaces": true
}
> cat Powershell.sublime-settings
{
"tab_size": 12,
"translate_tabs_to_spaces": true
}
> cat Ps1.sublime-settings
{
"tab_size": 12,
"translate_tabs_to_spaces": true
}
I closed and reopened Sublime Text and pressing the tab key still produces 2 spaces for tabs instead of 12.
Any ideas on how to make it work? Thank you.
This answer assumes you have already installed and been trying to configure the PowerShell package from Package Control.
The reason your settings are not working is because the settings' file names that you tried are wrong. The PowerShell package uses the file name PowershellSyntax.sublime-settings for its settings, that is what you need to use.
Most packages use their package name as the file name for their .sublime-settings file but clearly not all do. In this case there is a much older PowerShell package called stposh which already made use of the file name PowerShell.sublime-settings so, in all probability, the developer of the newer PowerShell package was forced to choose a different file name to avoid a conflict. Clearly the choice of PowershellSyntax.sublime-settings made setting up the package less intuitive, in my opinion the settings file name should be mentioned in the package's README.
// Save as 'PowershellSyntax.sublime-settings'
// in your Sublime Text config 'User' folder.
{
// 8 or even just 2 is probably better to test with
// but stick with the massive 12 if you want to. :)
"tab_size": 12,
"translate_tabs_to_spaces": true
}
In future if a .sublime-settings file does not work, look on the package's homepage which is always linked from Package Control. A browse through the source file names will usually quickly reveal the correct settings file name to use. If the package is already installed, you could also open its .sublime-package file, which is a zip archive, these are stored in the Installed Packages folder.
Some Extra Hints:
The PowerShell Package should automatically recognise the following file extensions .ps1, .psm1, .psd1 and use its syntax when they are opened. To force another file extension to automatically use that syntax, e.g. .ps, click on the currently active syntax name on the far right of the status bar, a context menu will be shown, hover the mouse pointer over Open all with current extension as..., and then select PowerShell from the list.
To manually instruct Sublime Text to convert tabs to spaces, or vise versa, click on where it says either Spaces: n or Tab Size: n on the status bar, doing so will open a context menu. You can then switch between spaces and tab indentation by selecting or unselecting Indent Using Spaces. Open the same context menu again and select whichever conversion is then required from the bottom 2 menu items. This last stage can also be performed by typing indentation into the Command Palette and choosing from the self-explanatory options.

Is there a way to add a dictionary to Sublime text and enable predictive typing using it?

I want the editor to suggest words to me as I type. It does this currently to the words that are already present in the editor. But I want it to suggest from a standard dictionary.
Is there any way to do this?
You can write a completions file. Just insert all words you want to complete into the completions list. Select Preferences > Browse Packages... and open the folder User, there you create a file with the extension .sublime-completions, e.g. MyWords.sublime-completions with the content:
{
"scope": "text", // <- your language scope here, press ctrl+alt+shift+p
"completions":
[
"hello",
"world",
"foo",
"bar",
]
}
I don't know about the Sublime text editor but Atom supports this functionality.
You can download the atom from here.
BTW Atom is free of cost rather than sublime. Both have same functionality. But to execute program you can use command line to execute.
Here is the link for Atom https://atom.io/
and here is the link for the autocomplete package for English : https://atom.io/packages/autocomplete-en-en
Hope this may helps you.
Go through the images provided in the Second link.

How to change VIM PHP auto formatting options

I have tried googling this extensively, but all I can find are plugins which format code in the author's preferred way. What I would like to do is change the auto format options so that I can setup VIM to use the same formatting as the other editors my team uses.
For example, I would like:
public function test($param)
{
// code here
}
rather than:
public function test($param){
// code here
}
or
public function test($param)
{
// code here
}
Is this possible without a plugin? Are there formatting templates somewhere that I can edit? Thanks :)
Is this possible without a plugin?
Yes.
Are there formatting templates somewhere that I can edit?
Sure. Vim is the most customizable text editor in universe. :)
So, let's start understanding snippets.
Snippets are blocks of text that can be inserted in your text using some pre-defined keys. The idea of snippets is to easily put in your file some chunk of text you use often. Snippets are these "templates" you mentioned.
To use snippets with Vim, you need to install the garbas/vim-snipmate plugin. You probably had it installed, since it seems that you can use them. This plugin search in you .vim folder for .snippets files and open them every time you open a file with predetermined extension. For example, when you create the foo.html file, vim-snipmate plugin searches for the html.snippets file and load it. After that, everytime you type, for example, html and press tab, Vim will write the <html> tag, because in your html.snippets file there's a snippet telling Vim to do so. Every programming language needs its own .snippets file, and loads it at the start. It's common to have a _.snippets file too, that loads with all file extension. It's a global snippet file.
To edit your snippets, you have to find where are your .snippets files. In Linux, open your terminal and type:
cd ~/.vim
find -name *.snippets
And then you'll see where are your snippet files. Assuming they are ~/.vim/snippets, for example, you open your java snippets with a:
vim ~/.vim/snippets/java.snippets
A .snippets file commonly looks like this: java.snippets file
These +-- lines are compressed lines you can expand and contract typing za in normal mode. In the blue line you always see snippet something written. The something is the shortcut you need to type and press tab when you're editing a file to use the snippet. For example in this java.snippets file there is a snippet called snippet po. So, when you're editing a java file, type po and press tab, Vim will inserted protected {}.
Snippets have a simple language, you can understand a lot just by seeing them in the .snippets file and typing them in another one. If you want to understand more about creating snippets, Google about vim snippets, and you'll find lots of stuff about it.
If you find that you don't have snippets in your .vim folder, or have insufficient ones, you can install a lot of excelent scripts with the honza/vim-snippets extension on Github.

How to run command on save in Sublime Text 3?

Sublime Text offers built-in commands, such as paste, new_window, toggle_comment etc. In addition, some plugins offer their own commands that can be used for key binding.
Can I force any of these commands to be ran on file save? The reason I need is because I'd like to run CSScomb on file save instead of / in addition to having key binding for it. The command name is css_comb.
Sublime-hooks package allows you to run packages based on event (on new, on save, etc.), so you can use it to achieve your goal. Just add this code to CSS syntax settings:
"on_pre_save_language": [
{
"command": "css_comb"
}
]
If you are familiar with plugins maybe you can make a plugin that extends EventListener and override on_post_save or on_pre_save methods.

Prevent Sublime Text 3 from tab completing 'sup' to '<sup>' in a non-HTML file

When I type sup then TAB in Sublime Text 3, I get <sup>. That isn't what I want; I only want tab completions based on what is in my current file.
How can I limit tag style tag-completions to just HTML files?
I was hoping to find a setting called tag_complete_file_extensions or similar.
I don't currently understand the implications of these default preference settings, but they don't look like what I want:
{
// Controls what scopes auto complete will be triggered in
"auto_complete_selector": "source - comment, meta.tag - punctuation.definition.tag.begin",
// Additional situations to trigger auto complete
"auto_complete_triggers": [ {"selector": "text.html", "characters": "<"} ],
}
At the least, I'd appreciate an answer pointing me in the right direction. I don't mind forking a package or writing some custom code.
I believe this behaviour is provided by the Emmet (formerly ZenCoding), or similar package and not sublime directly.
Are you sure syntax is set accordingly for the files you get html tag completion?
By default Emmet detects that you are working on html files and enables tag completion (in my case I did not have to configure anything to get this functionality). You should have a closer look at your packages and your configuration and possibly update existing ones.
If you need any further help with the configuration files, I'm happy to help!
Cheers!

Resources