Sublime keeps using 4-space tab for some files instead of 2-space tab even if I've done all the settings, why and how do I fix? - sublimetext3

I'm editing files using Sublime 3.2.2 on my iMac.
I have already set tab-size to 2:
It works for some files but doesn't for some files - remaining 4-sized tabs. I can't even find a clue about which is right and which is not.
Are there any other places I need to check except the settings?

There are two things to consider here. First, this setting (shown here with the default value):
// Set to false to disable detection of tabs vs. spaces on load
"detect_indentation": true,
When this setting is turned on, when a file opens it's examined to try and detect how it;s indented. This will override the tab_size in that particular file with whatever the detected tab size is, and can also override translate_taba_to_spaces as well if the file appears to be indented a particular way.
The setting defaults this way so that you can use your settings to determine how you want to create new files while still allowing you to work with existing files in a sane way; setting it to false turns off this detection.
The second thing to note is that preferences can be set on a per-file-type manner, which also overrides the settings in your user preferences for files of that type.
As an example, YAML files have these settings applied (this is from YAML/YAML.sublime-settings):
{
// YAML mandates that tabs aren't used for indentation
"translate_tabs_to_spaces": true,
// In practice, editing YAML files with anything other than two space
// indentation is tedious, due to the "- " list prefix
"tab_size": 2,
}
So, in a YAML file, regardless of your settings, the tab size defaults to 2 and using spaces over tabs is enforced.
As such, the takeaway here is that if changing the detection setting above doesn't work, check to see if the setting only seems to be "not working" in a particular type of file, and if so you can use Preferences > Settings - Syntax Specific while you have a file of that type open to enforce your settings there.

Related

set tab-stop = 2 in vim permanently for a file

How to set the tab size as 2 for a file permanently in vim as whenever I open a file in other editors like nano or upload the file in github then my indentations are all incorrent whenever I try to resize the tab to 2 for an existing file which has all incorrect indentations. The tab-stop=2 does not permanently resizes the tab and I see all incorrect indentation when I open the same file in nano or view it in github.
Tabs don't have an inherent size so it is up to each program to decide how to display them and there is simply no way to guarantee that a tab will always look the same everywhere.
This is precisely the main issue people have with tabs: you can tell $SOME_TOOL and $SOME_OTHER_TOOL that a tab takes two spaces but that setting can't possibly be carried over to every tool.
Modelines are editor-specific (and they are too intrusive anyway) and Editorconfig is not universally supported so there is really no universal solution beyond using spaces for indentation.

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 it possible to share SublimeText preferences *and* override one specific pref?

I share Sublime prefs between two machines (home, work) using Git.
My monitor at work has a higher DPI than my monitor at home, so in one of the two machines I'd like to override font_size.
I was hoping for:
The possibility to override using a command-line flag (to do something like subl --override-pref font_size=15), like kitty --override does.
Another level of prefs cascading below User.
, but I don't think any of this exists in Sublime. Language-specific config is not what I'm looking for, I want something global, but only in one of the two machines.
Ideas? Workarounds? Thanks.
The general mechanism at play for settings files is that when Sublime loads a sublime-settings file by name, it pulls all of the similarly named files across all of the known packages and combines them together (the same also happens for many other resource files) with content from later files overriding anything that appears in an earlier file.
The order that's imposed here is lexically by package, with Default always being first and User always being last. That's why the default settings are in the Default package and your custom settings are in the User package. Additionally syntax specific settings also apply (as do settings specific to projects).
Apart from this mechanism there's no direct way to override settings without some sort of manual intervention on your part. Potential solutions for this sort of problem include the following examples:
Don't sync the Preferences.sublime-settings file
If the file isn't synced across multiple machines, then this problem becomes moot because each machine can easily have it's own unique settings. The downside to that is that each machine then has it's own unique settings, which is a pain in the butt if you often move from machine to machine and things don't quite work the same way.
Use separate git branches
An alternative here if you're using git such as you are is to try and keep separate branches per host or per host type (like hi_dpi and reg_dpi or some such). Then on each machine check out the appropriate branch.
The obvious downsides here are having to try and cross-sync desired settings changes (for both User as well as any packages you might install) between branches, which is less than ideal unless you really love git.
Use extra Preferences.sublime-settings files
Here the idea is that you don't include the font_size setting in your User/Preferences.sublime-settings file at all. Instead, you use Browse Packages from the command palette to open the Packages folder, then create a new folder there with some arbitrary name. Inside of that folder include a Preferences.sublime-settings file that contains only the font_size setting.
Doing this on multiple machines, you can sync the settings in your User folder across machines without also syncing the preference that contains the font_size. As a note, if you create the file while Sublime is already running, you may need to quit and restart to get it to notice that the settings file exists; this only applies the one time, though.
Use a plugin
Looking at the link provided above, the ultimate trump card for any setting is a setting that's been applied directly to a view. Given that, you can use a plugin that selectively always applies a specific font size to any newly created or opened file:
import sublime
import sublime_plugin
import socket
class CustomFontListener(sublime_plugin.EventListener):
hostname = socket.gethostname()
def on_new(self, view):
if self.hostname in ("host1", "host1.example.com", "host2"):
view.settings().set("font_size", 20)
on_load = on_new
Now any time you open a file or create a new buffer, if the current hostname is in the list you've configured the view will immediately get an appropriate font_size applied; otherwise the one from the preferences would be used instead.
You could also extend this to be configurable via it's own settings file, apply a different font size depending on the host name, etc.
Settings in views are persisted in the sublime-session file and also in the workspace files associated with sublime-project files, so these settings will remain in place even across restarts.
Something to keep in mind is that the internal commands for changing the font size (via Preferences > Font or via the mouse wheel keyboard shortcuts) work by writing a new font_size to your user preferences.
If you're using separate preference files, then doing this will add font_size to your User settings and you will need to manually remove it and modify the other settings file.
If you're using the plugin, then these shortcuts won't seem to do anything because it applies a font_size that overrides the User preference, but in fact as outlined above your preferences file end up being changed and you may not notice right away.
So whichever way you go, if you tend to use those you may need to make manual adjustments to settings files in the aftermath. It's also possible to create smarter versions of those commands as well, if this is the sort of thing that happens often.

how do I exclude tmp files in Sublime "Go to Anything"?

I've tried this but it doesn't seem to work:
{
// index_exclude_patterns indicate which files won't be indexed.
"index_exclude_patterns": ["tmp/*", "*.cache", "*.log"],
}
Perhaps I need to reindex? But I don't know how to do that...
Your issue is that the index is used to collect symbols for Goto Definition and has no effect on the list of files that are presented to you when you use Goto Anything. So adding those folders to the list of files to exclude in indexing doesn't stop you from seeing them in the file list.
Goto Anything offers you all of the files that are currently contained in your project (if you're using one) or all open folders, excluding any files that it considers to be binary, so in order to stop files from appearing in the Goto Anything panel you need to either have them removed from the project or considered to be binary.
The folder_exclude_patterns and file_exclude_patterns settings allow you to indicate what folder and file patterns respectively should not be considered part of the project. Extending those settings would remove the files from the sidebar and thus from Goto Anything.
If you want to be able to still see the files in the sidebar but not be offered the ability to jump to them with Goto Anything, you can modify the binary_file_patterns setting. That tells Sublime what files are considered to be binary, which will stop it from showing them in the Goto Anything panel or searching them via Find in Files but will still show them in the sidebar for you to manually open.
folder_exclude_patterns and file_exclude_patterns can be used in your sublime-project file (if you're using one) so that they can be set on a project by project basis. binary_file_patterns only works as a global setting, however.
Either way, you probably want to base your custom settings on the defaults or you may accidentally have files appear in your project that are currently being hidden.

Changing identification settings in Sublime doesn't work for certain existing files

In my Sublime user settings I have "tab_size": 2, which works great for all new files. But some third party files seem to have their own identification settings embedded into them somehow (in which case my user settings have no effect).
How to overcome them?
The "tab_size" setting is only for new files and existing files that use tabs (instead of spaces) for indentation. If a file uses spaces (which I suspect yours do), Sublime will automatically determine the indentation size. You would need to reindent the file to match your preferred tab size.

Resources