Sublime Text 2 Default File Type on new file - html

I was looking around and the questions and answers did not seem to match what I am looking for. Anytime I open a new file it defaults to a plan text file. I mostly work with HTML files so I was wondering if there is a setting that would be changed so that when I open a new file it will default to HTML? Hope this is possible.
Rob

Create a new plugin Tools > Developer > New Plugin...
Paste this in:
import sublime, sublime_plugin
class EverythingIsPowerShell(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')
Save and call it NewTabSyntax.py. New tabs will now default to Powershell.
You can change the syntax to whatever you prefer. To find out the "path" of a particular syntax, simply open a file of that syntax, open the console (View > Show Console) and type:
view.settings().get('syntax')

pls install Package sublime-DefaultFileType https://github.com/spadgos/sublime-DefaultFileType
which will automatically sets the syntax for new files.

It is possible with the ApplySyntax Plugin. Once it is installed (e.g. via PackageControl), you can set the ApplySyntax User Settings like that:
"new_file_syntax": "HTML",
Now if you open a new file, your default syntax will be HTML. Of course you can set every Syntax you have installed.

Related

Sublime Text 2 - AutoFileName

Is it possible to configure the AutoFileName plugin for Sublime Text 2 to recognize TypeScript reference path attributes and allow auto-completion for other .ts files in my project?
For example, if I had a file structure like:
scripts
models
MyModel.ts
services
MyService.ts
Then in MyService.ts, I would want the path attribute in the reference tag to allow auto-completion of ../models/MyModel.ts
/// <reference path="../models/MyModel.ts" />
I was hoping to be able to do this using the "auto_complete_triggers" setting in my user/preferences.sublime-settings file, but really have no idea how to do so.
This is a syntax (tmLanguage) issue
AutoFileName must recognize a string pattern inside the comment line for it to work.
I use ArcticTypescript and have just made a pull request to fix this tmLanguage issue. Edit: It is merged now.
If you use another package for syntax highlighting like better-typescript please open an issue to correct the syntax definition.
Open Preferences -> Package Settings -> AutoFileName -> Settings-Default and copy the entire contents to a new file (you can set the syntax to JSON if you prefer), then close the Default file (you never want to make changes to the default settings for any plugin, always use the User settings in case you mess something up and need to revert). Modify the "afn_valid_scopes" setting to include "ts", and you should be all set. Save the file as Packages/User/autofilename.sublime-settings where Packages is the folder opened when selecting Preferences -> Browse Packages....
If you used the following setting in your Sublime user preferences:
"auto_complete_triggers":
[
{
"characters": "/",
"selector": "string.quoted.double.html,string.quoted.single.html, source.css"
}
]
then add a comma , after source.css then add string source.ts and save.

Unable to Edit Sublime Text's Default Settings

I would like to edit my default settings in Sublime Text 3 (beta build 3059) to not ignore the Vintage package - via Preferences > Settings - Default. I am running Sublime Text on Windows 7 Pro x64.
The Vintage package's documentation says to edit and save the default settings file to enable Vintage mode:
When I click the Settings - Default menu item, the default Preferences.sublime-settings file opens with expected content; but I cannot edit it. For example, deleting or backspacing to remove "Vintage" in "ignored_packages": ["Vintage"] does nothing.
I thought maybe the default settings file was marked readonly and tried to check it: C:\Users\me\AppData\Roaming\Sublime Text 3\Packages\Default\Preferences.sublime-settings does not exist. C:\Users\me\AppData\Roaming\Sublime Text 3\Packages\Default does not exist either.
To work around this, I tried to save the default settings file that Sublime Text opened for me - to see if that would create the Default directory and Preferences.sublime-settings in it. Instead Sublime Text gave the following error:
Unable to save C:\Users\me\AppData\Roaming\Sublime Text 3\Packages\Default\Preferences.sublime-settings
Error: The system cannot find the path specified.
Has anyone encountered this issue with Sublime Text 3 (specifically in trying to enable Vintage mode or otherwise) and worked around it...or found an authoritative explanation for it?
I reason that next I could try to add the missing Default directory myself, create an empty Preferences.sublime-settings text file in it, and try again to save the default settings file that Sublime Text opened for me; but this is starting to feel kludgy.
You should not edit the default settings. Add the files you want to ignore to the file Packages/User/Preferences.sublime-settings. You can open this file by going to Preferences - >Settings - User. Anything you set here will override the default settings.
I understand that you want to Not ignore the Vintage rather.
This still can be done using the Preferences.sublime-setting-User. AFAIK, any value given in this file overwrites the entries in Preferences.sublime-setting-Default.
Simply add this line with the "Vintage" removed in it to your User preferences and you'll be fine. (Remember to save the file after you did the edit and the change will happen instantly)
Also, not that VI mode in sublime is on edit more by default so make sure you press Esc first to make sure VI is activated.
// Settings in here override those in "Default/Preferences.sublime-settings",
// and are overridden in turn by file type specific settings.
{
"ignored_packages": []
}
Update
The latest format is,
{
"ignored_packages":
[
// Line below is commented out to enable Vintage.
//"Vintage"
],
// To start Sublime in Command moder
// rather than Insert mode.
"vintage_start_in_command_mode": true
}
Install 'PackageResourceViewer' from 'Install Package' in the Command Palette.
Then use 'PackageResourceViewer' command in the Command Palette.
Use that to extract/open the default packages you previously were unable to.
More information here : https://github.com/skuroda/PackageResourceViewer
I use Sublime Text3 recently in Windows 10. I'm trying to change the file: Packages/User/Preferences.sublime-settings. And met your problem "Enable to sa ve Preferences.sublime-settings".
I solve this by changing file Preferences.sublime-settings's property, in Security, edit "user"'s permissions. Allowed to modify.
And then I can edit and save Preferences.sublime-settings
When I install Sublime Text in Linux, I report the message NOTADIRECTORYERROR: [ERRNO 20] NOT A DIRECTORY. At first I thought it was because the Defalut folder was missing, but later I realized it wasn't. I then checked the Settings of my SublimeREPL. Sublime-setings and deleted the contents after bin, and found that the program worked fine.
old: "default_extend_env": {"PATH": "{PATH}:/home/bgnv5/anaconda3/bin/python"},
new: "default_extend_env": {"PATH": "{PATH}:/home/bgnv5/anaconda3/bin/"},
Spent a lot of time to find problems, I hope to help you.

Sublime Text Default Save Options

Why when I save a file in Sublime Text 3 is the default save location the Sublime install directory and why is the default file type nothing?
I want to set the default save location to the Desktop and the default file type to .txt, how can I do this?
Here are my settings:
{
"font_size": 9,
"hot_exit": false,
"ignored_packages": ["Vintage"],
"remember_open_files": false
}
Without no line of code:
Preferences -> Settings -> paste this "default_dir": "your/favorite/path"
And you are done.
More here: https://sublime-text-unofficial-documentation.readthedocs.io/en/latest/reference/settings.html#file-and-directory-settings
Doesn't (currently) address the default extension issue, but you can also try AdvancedNewFile. Rather than creating an unnamed buffer, this plugin creates a named file. The default location is configurable, though there is no default extension.
Disclaimer: I'm the maintainer of the AdvancedNewFile plugin.
Edit
I've updated AdvancedNewFile to support default file extensions.
You can install the Default File Type plugin manually. It's a very simple plugin, and while the Package Control page says that it's for Sublime Text 2 only, I just installed it under OSX and it works fine. To install, navigate to %APPDATA%\Sublime Text 3\Packages and run
git clone https://github.com/spadgos/sublime-DefaultFileType.git DefaultFileType
Then, copy Packages\DefaultFileType\default_file_type.sublime-settings to Packages\User and change its contents to the following:
{
"default_new_file_syntax": "Packages/Text/Plain text.tmLanguage",
"use_current_file_syntax": false
}
Save the file, and now whenever you hit CtrlN to create a new file, it will be set to plain text. The plugin only works with the key combo, not via the File -> New File menu option.
As far as the save location goes, I have a theory, but I haven't been able to find documentation to back it up. At least on Win7 (for me), it seems like the default save location is the directory which contains the file that was open when you hit CtrlN or File -> New File to create the new file. For example, I had my Packages\User\Preferences.sublime-settings file open when I created a new file, and hitting CtrlS opened the Save dialog in Packages\User. I saved the file to the Desktop, hit CtrlN for a new file, entered something, then hit CtrlS and the Save dialog opened in the Desktop.
So, while there isn't a preferences setting for default save location, at least on Windows you can tweak it by always keeping a Desktop file open, then creating new files while that Desktop file is focused.
This should be a built-in option, honestly, but it seems fairly simple to automate yourself. Hit Tools -> New Plugin
Then paste this over the file that's created, hit save and call it "DefaultLanguage.py" or something:
import sublime, sublime_plugin
class EverythingIsPowerShell(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')
Of course, you can change the language from PowerShell to ... whatever you prefer. You just need the relative path to the tmLanguage. You can get that by opening a file in your favorite language and then open the console (View->Show Console) and type:
view.settings().get('syntax')

Sublime Text 2: How to make Sublime run a command (fold code) when opening a file by default?

I would like to make it so that whenever I open a file in Sublime it will automatically do "Fold Level 2" Coding which is command shortcut Ctrl-K,Ctrl-2 (or CMD-K, CMD-2). I use both mac and pc.
I don't want to enter that shortcut everytime, instead I would like Sublime to automatically run that on opening a file. Please let me know if there a way to do that.
I think that the best solution to your problem is Buffer Scroll plugin. It remembers and restores a lot of things, folding included.
If you don't want to install that plugin, you can create your own:
Create new plugin Tools / New Plugin...
Insert code
import sublime, sublime_plugin
class Folding(sublime_plugin.EventListener):
def on_load(self, view):
view.run_command("fold_by_level", {"level": 2})
Save it in your User directory with the filename you prefer.
This will set folding level to 2, for every file you open.

Set default syntax to different filetype in Sublime Text 2

How do I set a default filetype for a certain file extension in Sublime Text 2? Specifically I want to have *.cfg files default to having Ini syntax highlighting but I cannot seem to figure out how I could create this custom setting.
In the current version of Sublime Text 2 (Build: 2139), you can set the syntax for all files of a certain file extension using an option in the menu bar. Open a file with the extension you want to set a default for and navigate through the following menus: View -> Syntax -> Open all with current extension as... ->[your syntax choice].
Updated 2012-06-28: Recent builds of Sublime Text 2 (at least since Build 2181) have allowed the syntax to be set by clicking the current syntax type in the lower right corner of the window. This will open the syntax selection menu with the option to Open all with current extension as... at the top of the menu.
Updated 2016-04-19: As of now, this also works for Sublime Text 3.
Go to a Packages/User, create (or edit) a .sublime-settings file named after the Syntax where you want to add the extensions, Ini.sublime-settings in your case, then write there something like this:
{
"extensions":["cfg"]
}
And then restart Sublime Text
In ST2 there's a package you can install called Default FileType which does just that.
More info here.
You can turn on syntax highlighting based on the contents of the file.
For example, my Makefiles regardless of their extension the first line as follows:
#-*-Makefile-*- vim:syntax=make
This is typical practice for other editors such as vim.
However, for this to work you need to modify the
Makefile.tmLanguage file.
Find the file (for Sublime Text 3 in Ubuntu) at:
/opt/sublime_text/Packages/Makefile.sublime-package
Note, that is really a zip file. Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it.
Edit the new Makefile.tmLanguage by adding the "firstLineMatch" key and string after the "fileTypes" section. In the example below, the last two lines are new (should be added by you). The <string> section holds the regular expression, that will enable syntax highlighting for the files that match the first line. This expression recognizes two patterns: "-*-Makefile-*-" and "vim:syntax=make".
...
<key>fileTypes</key>
<array>
<string>GNUmakefile</string>
<string>makefile</string>
<string>Makefile</string>
<string>OCamlMakefile</string>
<string>make</string>
</array>
<key>firstLineMatch</key>
<string>^#\s*-\*-Makefile-\*-|^#.*\s*vim:syntax=make</string>
Place the modified Makefile.tmLanguage in the User settings directory:
~/.config/sublime-text-3/Packages/User/Makefile.tmLanguage
All the files matching the first line rule should turn the syntax highlighting on when opened.
The best solution for me turned out to be to used the ApplySyntax package.
The steps are as follows:
Install the package via Package Control
CTRL + SHIFT + P and enter ApplySyntax: Browse Syntaxes. Find your desired syntax here and note the exact line shown, e.g. I was looking to set it to Markdown from the Markdown Editing package, so for me the line was MarkdownEditing/syntaxes/Markdown.
CTRL + SHIFT + P and enter ApplySyntax: Settings.
On line "new_file_syntax": "XYZ", enter the line from Step 2.
See here for further documentation.
I found this to work better than the DefaultFileType package, because it isn't limited to just new files created by pressing CTRL + N and captured new tabs opened by clicking the empty space to the right of an open tab.
I hope is useful to someone 11 years after the original question was asked. 😅