How to enable sublime text to take first line as file name while saving? - sublimetext3

Earlier the Sublime used to take first line as file name by default but now it's "untitled". Is there a way to enable it or is there a plugin for that?
Thanks

The first line is only used as the file name for unsaved files when the syntax is set to Plain Text. As soon as you change the syntax highlighting and type something, it will change the tab name to "untitled".
The implementation for this is in the Default package, set_unsaved_view_name.py file. To get it to work for all syntaxes:
Install PackageResourceViewer through Package Control if it is not already installed
Open Command Palette
Type PRV: and select PackageResourceViewer: Open Resource
Select Default
Select set_unsaved_view_name.py
Find if syntax != 'Packages/Text/Plain text.tmLanguage':
Select from there to the end of the if statement (the first return statement) (Python is indentation based) inclusive return to be commented out.
Go to the Edit menu -> Comment -> Toggle Comment
Save the file
Ensure that, in your preferences (user, syntax specific etc.), set_unsaved_view_name is not set to false
Note: these instructions are valid as at ST build 3131, and the implementation could change in future builds.

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.

Custom syntax in Sublime Text 3

I'm struggling to find out how to create a new syntax highlighting in Sublime Text 3 using the new .sublime-syntax style definition (most previous answers relate to old ways of doing it).
As of Sublime Text Build 3084, a new syntax definition format has been added, with the .sublime-syntax extension.
I can find the:
syntax rules
scope naming rules
colour scheme rules
But I can't find the most basic piece of information detailing how these tie together!
I'm not trying to create a theme, or tweaking an existing syntax definition. I just want to create syntax highlighting to files with an extension I plan on using for my own purposes.
In the syntax definition I have to specify a scope (e.g. scope: source.c) but where does that scope file live? Or rather, where do I create my scope file, and how do I name it, so that it loads?
How do I know that my syntax file, and the scope file it uses, are loaded and applied successfully?
Are there any compile or refresh steps, or does everything automatically reload?
Thanks.
A full discussion of how to create a custom syntax is well outside the bounds of something as simple as a Stack Overflow answer. Also I think you're making your problem more complicated than it actually is (although creating a syntax is pretty complicated in general).
In order to walk you through the steps that you would take to create a custom syntax, here's an example.
To start with, create a file with the following contents and save it somewhere as sample.ec, and leave the file open:
// This is a line comment
if (x == 2)
y = 1
else
z = 1
You'll notice that the syntax for this file is set to Plain Text (see the status line in the lower right), which is the default syntax for files that are unknown to Sublime.
Now, select Tools > Developer > New Syntax... from the menu. A buffer with the following will appear. Use File > Save to save the file; the location will default to your User package. The name you give it is not important, but make sure that the extension is sublime-syntax. In my example I'm calling my file Sample.sublime-syntax.
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
- ec
scope: source.example-c
contexts:
main:
# Strings begin and end with quotes, and use backslashes as an escape
# character
- match: '"'
scope: punctuation.definition.string.begin.example-c
push: double_quoted_string
# Comments begin with a '//' and finish at the end of the line
- match: '//'
scope: punctuation.definition.comment.example-c
push: line_comment
# Keywords are if, else for and while.
# Note that blackslashes don't need to be escaped within single quoted
# strings in YAML. When using single quoted strings, only single quotes
# need to be escaped: this is done by using two single quotes next to each
# other.
- match: '\b(if|else|for|while)\b'
scope: keyword.control.example-c
# Numbers
- match: '\b(-)?[0-9.]+\b'
scope: constant.numeric.example-c
double_quoted_string:
- meta_scope: string.quoted.double.example-c
- match: '\\.'
scope: constant.character.escape.example-c
- match: '"'
scope: punctuation.definition.string.end.example-c
pop: true
line_comment:
- meta_scope: comment.line.example-c
- match: $
pop: true
Now open the Sublime Console with View > Show Console or press the associated key binding. You'll see that the last line in the console is this:
generating syntax summary
Leaving the console open, click in the syntax file and perform another save operation again without changing anything. The same line appears in the console again.
Are there any compile or refresh steps, or does everything automatically reload?
As seen here, every time you modify the syntax definition, the file is recompiled and the results are cached. So there are no compile steps (other than saving) and nothing you need to do in order to refresh anything.
Now lets turn our attention back to the sample file. It's still open, and the syntax still says that it's Plain Text.
Now close the file and re-open it again; a shortcut for this is to use File > Open Recent > Reopen Closed File or it's associated key binding.
Notice that now that the file is re-opened, there are several changes. Firstly, the syntax name in the bottom right side of the window says Sample (or whatever you named your sublime-syntax file above). For another, the contents of the file are now syntax highlighted.
The colors you see are dependent on the color scheme you use, but an example might look like this:
// This is a line comment
if (x == 2)
y = 1
else
z = 1
How do I know that my syntax file, and the scope file it uses, are loaded and applied successfully?
You can see that the syntax file was compiled by the lack of an error message when you save your changes, and you can tell that it's applied by trying to use the syntax.
Here the syntax is being used automatically, but you'll find that if you check View > Syntax in the menu or click the current syntax name in the bottom right of the window, your syntax will appear there. Similarly there is now an entry in the command palette named Set Syntax: Sample (or whatever).
That leads us into your last question. If you go back to your sublime-syntax file, you'll see this at the top:
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
- ec
scope: source.example-c
The first thing to note is that file_extensions includes ec, and our sample file is called sample.ec; thus this syntax applies to it automatically due to it's name.
Now switch into the sample.ec file, place the cursor somewhere in the buffer and use Tools > Developer > Show Scope Name or press the associated key.
The content of the popup that appears will vary depending on where in the file the cursor is located, but the common point is that the scope that appears always starts with source.example-c.
In the syntax definition I have to specify a scope (e.g. scope: source.c) but where does that scope file live? Or rather, where do I create my scope file, and how do I name it, so that it loads?
As seen here, there is no such thing as a "scope file"; the sublime-syntax file directly specifies the scope as part of the syntax rules, so it's the only file that you need to create in order to create a syntax. It may look like a filename, but it is not one.
The scopes that are applied in the syntax matching rules in the syntax need to coincide with the scopes in your color scheme in order to be syntax highlighted; that's why you should use the scope naming rules to use the common set of scopes that all syntaxes share unless you're also planning to make a color scheme to go along with your syntax, but unless you use the recommended scopes, your syntax won't work well with other color schemes and your color scheme won't work well for other syntaxes.
From this starting point you can modify the sublime-syntax file here in order to make it highlight files the way you want. That would include changing the base scope at the top, applying an appropriate extension, and then including all of the rules that match your language.
As mentioned above, creating the actual rules to match your file is the most complicated part of creating a syntax unless your file format is very simplistic. It's outside the scope of something that could be conveyed in a Stack Overflow answer, but the official documentation linked above gives you some information on it.
Apart from looking at existing syntax files to see how they're doing what they do, you can also ask more directed questions on the Sublime forum.
I will shortly answer your questions, someone else can feel free to write a longer guide:
You put your syntax definitions inside a package: Select Preferences > Browse Packages... this should open your file explorer. There you can either create a new folder for a new package or use the User folder, which is the default user package. Inside that create a file YourSyntax.sublime-syntax.
You can open the ST console ctrl+` and it will output that the syntax is loaded and potential errors. You can also press ctrl+shift+p and write Set Syntax: YourSyntax in a buffer to directly see it.
You just need to save the file and it will reload the syntax definition.

Sublime Text 3 specific file type settings only has JSON file settings. How to create new?

I'm trying to create a syntax specific settings for file types in ST3. As per the documentation I am supposed to find what I need in
Sublime Text>Preferences>Settings - More>Syntax Specific - User
However, when I click that I only get JSON.sublime-settings file. What if I want some other settings file?
The settings file that opens when you select Prefereces > Settings - Syntax Specific is sensitive to the type of file that you're currently editing. So, if you happen to be in a JSON file when you invoke the menu command, you get the file for settings specific to JSON.
In order to get at the setting specific to a different syntax, open a file of that type first, or create an empty buffer and set the syntax to the desired language via View > Syntax from the menu, the command palette, or the menu that pops up when you click the file type in the right side of the status bar (where it will say Plain Text).
The file that you actually want to save is SyntaxName.sublime-settings in your User package, e.g. Python.sublime-settings for Python, etc. However to forestall any problems with the filename (like incorrect case or spelling) it's generally better to do it as above instead, particularly since the name of the syntax can sometimes be non-obvious.

How to make Sublime Text not autocomplete element names within text elements

When I write HTML in Sublime Text 3, I like having autocompletion of attributes and element names and so on, but Sublime Text is always over-predicting things and turning my text content into elements (inappropriately), when the vast majority of the document body I am writing is text, not elements. What gets really annoying is if I press Enter at the end of a line where it believes the word I am typing is a typo of a different element, I get some really frustrating behavior:
The workaround I see is to press EscEnter at the end of every line but that isn't particularly ergonomic (and I have severe RSI so I'd rather have things be ergonomic).
Here are the relevant portions of my Preferences.sublime-settings:
{
"auto_complete": false,
"auto_complete_selector": "source - (comment, string.quoted)",
"tab_completion": false,
"tab_size": 4,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"word_wrap": true
}
I have looked at the Sublime Text documentation regarding completions and snippets but it isn't obvious to me how to suppress completions entirely during a text context. I don't currently have any HTML-mode-specific options configured.
Sublime's HTML autocompletions are implemented in a Python script.
It is possible to edit it, such that HTML tags will only be offered when you type < and a letter - instead of offering them while typing "plain" text content inside HTML.
To do this:
Install PackageResourceViewer using Package Control (if it isn't already installed)
From the Command Palette, type PRV: O and select PackageResourceViewer: Open Resource
Select HTML
Select html_completions.py
Find the line # if the opening < is not here insert that (https://github.com/sublimehq/Packages/blob/77867ed8000601962fec21a012f42b789c42195b/HTML/html_completions.py#L243)
Change completion_list = [(pair[0], '<' + pair[1]) for pair in completion_list] to completion_list = []
Save the file
Note that this creates a file that will override the version that comes with ST, so when a new build of ST3 is released, your version will still be used. In case this file is ever updated, it might be a good idea to delete your version to ensure you have the latest changes, and then to re-apply the above steps (if necessary - maybe it will be "fixed" in a future update to not suggest anything when auto_complete is set to false). To do this:
Tools -> Browse Packages
HTML
Delete html_completions.py

Change SublimeREPL shell colour

I use SublimeText3 and try to change the colour for SublimeREPL Shell because its all white. Is that possible? Or is it possible to use colours from system prompt like PS1='' ?. I am running on ubuntu. I haven't found a soloution.
I assume you're trying to color the prompt in the SublimeREPL shell - if you want syntax highlighting of the commands you type, just change the syntax to Shell Script (Bash). To do this permanently, open your Packages folder (Preferences -> Browse Packages...), browse to SublimeREPL/config/Shell, and open Main.sublime-menu as a JSON file. Line 26 contains the "syntax" setting; just change the value to "Packages/ShellScript/Shell-Unix-Generic.tmLanguage", save the file, and the next time you start it the syntax will be applied.
However, if you're just trying to color the prompt, you'll have much more work to do. First, you'll have to create a custom .tmLanguage syntax definition file creating scopes for the various parts of the prompt you want to highlight, then you'll need to alter your color scheme's .tmTheme file to actually style the scopes. (If you're using the ST3 dev builds and have Build 3084 or newer, you can also use the new YAML-based .sublime-syntax format instead of the XML-based .tmLanguage one.)
If you're not using a dev build, the best way to write syntax definitions is to use the wonderful PackageDev package. I maintain an alternate - and better :) - syntax definition for Python and I much prefer using PackageDev's .YAML-tmLanguage format, which as you can tell is also based on YAML, but was around long before the new "official" .sublime-syntax format, and of course they're incompatible. However, it is quite easy to convert from YAML-tmL to tmL to sub-syn and back again, so it's no big deal.
However, as I was saying, the contents of your syntax definition will vary depending on the exact structure of your prompt, and what you want to do with it. For the following examples, I'm assuming you have the default Ubuntu user#hostname:/present/working/directory$ prompt. To create a new syntax definition, after installing PackageDev, select Tools -> Packages -> Package Development -> New Syntax Definition and you'll get the following:
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Syntax Name
scopeName: source.syntax_name
fileTypes: []
uuid: 7e1549b3-fb0b-44fc-a153-78a7fc2157c2
patterns:
-
...
The first line is required, don't mess with it. You can make name whatever you want. scopeName is obviously the identifier for the base scope, perhaps something like source.shell.prompt. fileTypes can be left blank, and the uuid left alone as well.
If you want to get a feel for how these files are supposed to work, feel free to check out PythonImproved.YAML-tmLanguage on Github, and also make use of the Sublime Text Unofficial Documentation page on the subject as well as the reference. There's also some info in PackageDev's README.
I'll let you develop the rest of the regexes, but here's one for matching the username to get you started:
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Shell Prompt
scopeName: source.shell.prompt
fileTypes: []
uuid: 7e1549b3-fb0b-44fc-a153-78a7fc2157c2
patterns:
- name: meta.username.prompt
match: ^([A-Za-z_][A-Za-z0-9_-]{0,31})(?=#)
...
You can see it working here.
Once your .YAML-tmLanguage is complete, save the file, open the command palette, and select PackageDev: Convert (YAML, JSON, PList) to.... This will build the .tmLanguage file and put it in the same directory as the .YAML-tmLanguage file. If it's not already under the Packages directory tree, copy it to your Packages/User directory, then modify the Main.sublime-menu file as described in the first paragraph. Finally, open your color scheme's .tmTheme file and edit it to add the scopes defined in your new syntax. Save it, restart Sublime for good measure, and you should be all set!

Resources