Keymap Sublime Text 2 File Type? - html

I am working on setting up my own keymaps and was wondering if there is the option to set keys to switch the file type that is being worked on. So for example, if I have a regular plain text file and want it to be a css file, I would have a keymap that would change the document type to css. Possible? If so, please explain to me how you have done this.
Rob

The keybinding for this would be:
{
"keys": ["YOUR_SEQUENCE"],
"command": "set_file_type",
"args": {"syntax": "Packages/CSS/CSS.tmLanguage"}
}
How to discover command names:
Open the console
Type sublime.log_commands(True)
Go to an open tab
Open the command palette and type Set Syntax: CSS
The name of the command and it's required arguments should be logged to the console. From there you just put it in the right JSON syntax.

While the individual shortcut solution is great, it requires editing the config files and most importantly remembering all the shortcuts you create for each sytax.
In the case of switching file formats it might be more useful to quickly access the required format via the command palette:
Press CTRL+SHIFT+P to bring up the Command Palette
Type CSS to highlight Set Syntax: CSS command
Press ENTER
This is great because it provides quick access to all the formats available. Start typing set syntax... and all the available formats will be shown.

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.

Can we make use of syntax highlighting feature to remove all comments from a source file in SublimeText?

I have a bunch of source files written in different languages, and I would like to strip all comments from the source files.
While writing regex is certainly an option, depending on the input files, I may have to handle cases where the character to denote comment appears inside string literals. There is also the need to maintain a list of regex for different languages.
The syntax highlighting seems to do quite a good job at highlighting the comments, but there doesn't seem to be any command to remove all comments in the Command Palette.
Is there any way to leverage the syntax highlighting feature in SublimeText to remove all comments from source files in different languages?
Based on nhahtdh's answer, the following plugin should work for both Sublime Text 2 and 3
import sublime_plugin
class RemoveCommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
comments = self.view.find_by_selector('comment')
for region in reversed(comments):
self.view.erase(edit, region)
Create a new file with Python syntax, and paste the code above into it. Save the file in your Packages/User directory (accessible via Preferences -> Browse Packages...) as remove_comments.py. You can now either run the plugin via the console, or bind a key combination to it. To run via the console, just type
view.run_command('remove_comments')
in the console, and all the comments in the current view will be deleted.
To bind a key combination, open Preferences -> Key Bindings-User and add the following (surround it with square brackets [] if the file is empty):
{ "keys": ["ctrl+alt+shift+r"], "command": "remove_comments" }
Save the file, and you can now hit CtrlAltShiftR (or whatever key combination you choose) and all comments in the current file will be deleted.
Assumption
We will make use of the syntax highlighting rules in Sublime Text to remove all comments, so the method below works only if the syntax highlighting works correctly for the language of your source file.
For most languages, the syntax highlighting rules do quite a good job at recognizing the comments. However, it would be best if you take another look at your files to see if there is any anomaly in syntax highlighting.
The current method only works for Sublime Text 2.
Solution
Open the Console via View > Show Console or the key combination Ctrl+`
Copy and paste the following commands line by line:
e = view.begin_edit()
len([view.erase(e, r) for r in reversed(view.find_by_selector('comment'))])
view.end_edit(e)
After the last command, the edit will be applied and all comments will be removed.

Why doesn’t this hotkey configuration for Sublime Text work?

I have configured Ctrl+B to open a file in my browser, but when I press Ctrl+B when the editor is focused on some HTML page, it doesn’t work. Why not?
more details:
what makes me confused is that this ctrl+b works if it is combined to other command such as close, but why not openInBrowser command, so does st2 support this command, how to know whether it support this command or not?
Edit 1:
you may find openInBrowser command in this link http://www.sublimetext.com/docs/commands
Edit 2
#MattDMo what is the corresponding name of command then, i cannot find them in its official document http://www.sublimetext.com/docs/2/
The command you are looking for is open_in_browser, not openInBrowser (which is an old Sublime Text 1 command). So, your keymapping should work with that. However, if you weren't already aware, CtrlB is already mapped to the Build command, used for running build systems. It's not a very good idea to overwrite built-in commands, especially one as important as this one, so I'd suggest changing your keybinding to:
{ "keys": ["ctrl+alt+b"], "command": "open_in_browser" }
This isn't in use by any of the default Sublime commands, although some plugins may use it.

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.

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.