Sublime Text 2 different indentations settings - sublimetext2

I want to configure my Sublime Text 2 to have two different types of indentations:
4 spaces for general use
2 spaces for my html pages
I think it should be done by editing the settings, but I don't know what to write.

The key setting is "tab_size". By default, if you look at Preferences -> Settings-Default, it is
"tab_size": 4
The items in this file should absolutely not be modified. Instead, copy anything you want to change to the file opened by Preferences -> Settings-User, and alter the values there. It is saved as Packages/User/Preferences.sublime-settings where Packages is the folder opened by selecting Preferences -> Browse Packages....
If you would like to have specific settings for certain syntaxes, open a new file in Sublime with JSON syntax. Like any other Sublime preferences file, open and close it with curly braces { }. Put your desired settings in it, one per line, each followed by a comma , except the last entry (basically, it should be valid JSON, except JavaScript comments are also allowed).
{
"tab_size": 2,
"translate_tabs_to_spaces": true
}
Finally, save it in your Packages/User folder as Syntax Name.sublime-settings - in your case, it should be HTML.sublime-settings. These settings should now be applied to any newly-opened or newly-created HTML files.

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.

Have Sublime 2 replace tabs with 4 spaces in Python but 2 spaces in JavaScript?

As the title suggests, I'm trying to set variable tab-for-space replacements according to the file format. Is this possible? I've only been able to set one universal setting for this.
Open up a file of the format you want to set preferences for and do the following:
Click Sublime Text 2 -> Preferences -> Settings - More -> Syntax Specific - User
That will open a file for preferences for the format of the file you're editing; e.g., if you're editing a .c file, you'll be setting the preferences for C language files.
Add this to the file that Sublime Text opens after you navigate as above:
{
"tab_size": <whatever you want>,
"translate_tabs_to_spaces": true
}

How to edit multiple files through "Find results" in Sublime Text

When I search multiple files via Command + Shift + F, the result is returned as something like a text file. This text file is editable, but changes made don't affect the original files.
Is it possible to do such that changes in "Find Results" propagates to the original source file?
The Find Results Apply Changes plugin was created to do just that.
You can install it through Package Control's "Install Package" option.
Before using this plugin, make sure that you have UTF-8 encoding enabled using the menu:
File > Save with Encoding > UTF-8
Once installed, you can apply any change you made to a "Find Results" buffer back to the files:
Search for "foo" in a folder (Sublime's default shortcut is CTRL+SHIFT+S)
This will open a "Find Results" buffer listing all the files with "foo" in it.
Change the instances of "foo" for "bar" or something else...
Go to the menu:
Find > Find Results - Apply Changes
This will write all the changes made back to the files and save the modified files automatically.
By default, using menus is the only way to make it work. However, it is quite tiresome and doesn't save as much time as it does with a keyboard shortcut. You can set your chosen keyboard shortcut by adding a new line in
Preferences > Key Bindings - User
by adding:
{ "keys": ["ctrl+r"], "command": "find_results_apply_changes" },
Don't use CTRL+S as it will overwrite saving file shortcut.
Warning!: According to the author of the plugin:
Uses regions to allow you do multiline changes, but when inserting new
newlines, will corrupt files if you commit more than once, this
because the new newlines will shift the line numbers. Will also
'corrupt' files if you add/remove newlines in other instances of the
modified files. eg in another tab. To prevent corruption this packages
will alert you and prevent most of these.
(This is a modified version of the description from the Find Results Apply Changes Github page.)
The shortest workaround I can think of would be to open the target file from search results by simply double clicking the path and then jumping to the according line using Ctrl+G on Windows or ⌃+G on Mac OS.
That's the way I do it and must say it is only a matter of seconds, even without the plugin.

How to turn removing whitespaces off for some files in Sublime Text 2?

I have an option trim_trailing_white_space_on_save turned on. And for some files I should prevent removing trailing white spaces, because they are important.
How to remove this behaviour for some files, e.g. *.dat?
Have you already tried to create a configuration file for that specific extension and put trim_trailing_white_space_on_save = false ?
Settings Files
Settings files are consulted in this order:
Packages/Default/Preferences.sublime-settings
Packages/Default/Preferences (< platform >).sublime-settings
Packages/User/Preferences.sublime-settings
< Project Settings >
Packages/< syntax >/< syntax >.sublime-settings
Packages/User/.sublime-settings
< Buffer Specific Settings >
In general, you should place your settings in Packages/User/Preferences.sublime-settings.
If you want to specify settings for a certain file type, for example, Python, you should place them in Packages/User/Python.sublime-settings.
http://www.sublimetext.com/docs/2/settings.html
In Sublime 3, just open any file with the extension you'd like to have specific settings for, and go to Preferences > Settings - Syntax Specific.
In my case I did it for Markdown (.md) and Sublime created a Markdown.sublime-settings file in which I added the following:
"trim_trailing_white_space_on_save": false,
In your case, for a .dat file, Sublime will create a Plain text.sulbime-settings file in which you can add the exact same setting.

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. 😅