Is there any shortcut-key in sublime text 3 to change language? - sublimetext3

Actually there is an easy way to change language in Sublime Text 3, that's on the bottom right corner. But I need the fast way to change while my hands are on the keyboard. Is there any shortcut-key to change language in Sublime Text 3?
Thank you.

All of the non-hidden syntaxes in Sublime are automatically added to the View > Syntax main menu, which is the same menu that appears when you click on the file type in the bottom of the window. Additionally all syntaxes are added to the Command Palette as commands that start with Set Syntax:.
So the easiest and fastest non-mouse way to swap the syntax on a file is to open the command palette an enter just enough filter text to find and select the command that will swap to your desired syntax, such as in the image below to switch to HTML.
Sublime remembers what command you select for any given command palette input, so for extra speed you can use filter text like sh and manually select the Set Syntax: HTML command that appears to tell Sublime that's the command you want. Now whenever you enter sh it will automatically select that command for you by default.
The set_setting command can be used to set any setting, including the syntax setting, so you could also bind a key to that command to switch easily to an often used syntax. However that requires that you know the full package resource name of the syntax in question and it will not properly set up the syntax specific settings (that requires a plugin that uses view.assign_syntax()).
There may be a package available on package control that provides such a command already, but I'm not aware of any offhand.

Related

Sublime Text plugin development: modifying the Quick Panel to add a custom text title or label?

I'm developing a Sublime Text 3 plugin which uses the quick panel a.k.a. the command palette.
I would like to add a textual title/label at the top of the quick panel when the user opens my plugin's quick panel menu, because my plugin runs in various modes and I want to make it clear which mode is currently running (Test, Pre-Live, Live, etc.)
Is this possible, and how?
This a mockup of what I want to achieve:
This is not possible in a quick panel, no. The only thing you can provide to the quick panel display-wise is the list of items that you want the user to choose from. Each item in the list can contain multiple lines of data however (with the restriction that every row has to contain the same number of lines):
Something like that could potentially be used to provide context on the items in the list that allows the user to know what mode you're in.
The Command Palette and the quick panel aren't the same thing (although they are visually similar); the command palette only displays commands that you can choose. By using an input handler, you can customize the display somewhat:
In this sample, View Package File is the selected command, but the command has control over what that text says (it's just the command name by default). You also have the power to provide a "preview" (basically any extra information that you want) about the currently selected item:
So given that, depending on your use case your command could present its interface in the command palette to give this kind of contextual clue.

Sublime text shortcut to open keybindings and settings

I would like to be able to map a keyboard shortcut to be able to open the settings and open the keybindings.
I would like it to behave the exact same as going to Menu > Preference > Settings \ Keybindings.
Is this possible?
If a command appears in a Menu or in the command palette, then it's possible to bind it to a key provided you can determine what the command is and what arguments it takes.
One way to determine that for a menu item is to use View Package File from the command palette and then open the appropriate menu resource. The main menu always comes from a Main.sublime-menu resource, so once you enter the command you can enter Main.sublime-menu in order to see all of the resources that match, then pick the one for the appropriate package.
For a command that appears in the command palette you can do the same by entering sublime-commands to see all of the files that are contributing commands to the command palette and then select the appropriate one to see what command it's executing.
For determining the command bound to a key (in case you want to go the other way and add it to a menu or the command palette) you would look for sublime-keymap files instead.
These options require you to either know the package that is contributing the command or be able to infer it based on what it does. The Default package is what contains the default Sublime behaviour; other files augment the files from that package.
Another method is to open the Sublime console with Ctrl+` or the menu item View > Show Console and then enter the command sublime.log_commands(True) to turn on command logging.
With that enabled you can take the action you want to know the command for and the console will tell you the command being executed and what arguments it's taking.
In your particular case, the output in the console would be:
command: edit_settings {"base_file": "${packages}/Default/Default ($platform).sublime-keymap", "default": "[\n\t$0\n]\n"}
command: new_window
The first of these is the appropriate command, and the second is that command executing a command of it's own to open a new window for the settings to be stored in.
Armed with that, you can make an appropriate key binding:
{
"keys": ["ctrl+alt+shift+k"],
"command": "edit_settings",
"args": {
"base_file": "${packages}/Default/Default ($platform).sublime-keymap",
"default": "[\n\t$0\n]\n"
}
},
Note that recent (as of the time of this answer) builds of Sublime have a bug in which commands executed from the command palette don't always get logged, in which case if that's how you want to determine the correct command you would have to look in the resource directly.
To add a more simple approach to OdatNurd's fantastic answer: I recently found myself wanting a shortcut to open my keybindings file and came across a slightly longer route using the Command Pallet that I was content with.
This doesn't answer the specific issue of creating a keyboard shortcut to open the user's .sublime-keymap file, but it does solve the general issue of getting to that file in a timely manner, without having to use the mouse.
The approach is:
Ctrl+Alt+P (Open Command Pallet)
Type: "keybind"
This gives:
Hit Enter and the user's .sublime-keymap file will be opened as it would be if you navigated to Menu > Preferences > Key Bindings.

Keybind for auto-indent/reindent block of code in ST3

Is there a way to add a keybind to reindent a highlighted block of code in Sublime Text 3?
I know there is a "reindent" option in Edit > Line > Reindent, but it has no keybind.
Also, that reindent funcion is not so "smart" and in some cases it gives weird results. Is there an addon that better solves this issue?
I mostly code in JavaScript if that helps.
Covering the first part of your question, it's possible to bind a key to anything that exists in the Menu or command palette, it's just a matter of finding out what command and arguments you need.
The easiest way to do that would be to open the Sublime console with Ctrl+` or View > Show Console, then enter the command sublime.log_commands(True), execute the command and see what it says:
>>> sublime.log_commands(True)
command: reindent {"single_line": true}
You can run the command command with False instead of True to turn off logging, or just restart Sublime.
With that knowledge, you can create a key binding using the command and arguments that were displayed by using Preferences > Key Bindings and adding the binding in the right hand pane.
In this case, that woulld look something like this (change the key as appropriate):
{
"keys": ["ctrl+alt+r"],
"command": "reindent",
"args": {
"single_line": true
}
},
Once you do this, not only is the key binding active but Sublime will also display the key that you selected in the menu next to the menu item as an extra reminder for you.
For the second part of your question, indeed the internal reindent and reformat of code is not ideal in Sublime; partially this is the result of its indentation system being powered by some simple regular expressions in the same manner as TextMate for compatibility reasons.
In any case, you can search Package Control for third party packages that might allow for better formatting/reformatting of code. In the case of JavaScript, something like JsFormat might be what you want.
Typically such a package only provides integration with an external tool that does the work and thus requires you to also install an external third party tool to function. In the specific case of JsFormat however, it bundles it's own formatter directly.

How to display a text file (.txt) as a code file with color in Sublime Text 3

I'm using sublime text 3. And I want to display a text with color like a code file in Sublime. But I don't know how to do. Please help me!.
Thanks you!
You can manually set the syntax of any file using the Command Palette and type "Set Syntax: " or from the bottom right dialog (by default it reads "Plain Text").
If you want Sublime Text to remember your choice, you can also do that from the syntax selection dialog mentioned above, or View > Syntax > Open all with current extension as…".
I think if you need your text file for purposes like note taking, there is a nice hack.
Go to View > Syntax and then select haskell. Haskell syntax highlighting is subtle and works fine. To highlight something just capitalise the first letter and it pops in a different colour. Numbers are highlighted in a different colour again, increasing readability. Finally brackets commas etc have different colour which further helps.
Well, coloring is based on the syntax, like every programming language has its syntax, but the pure text file is hard to detect the syntax since it can be anything. So if you use the Sublime to coding,try save it as a file with file extension first, then the Sublime will detect by itself, however you could also do this manually.
The ability to create your own syntax highlighting rules is one of the excellent features of SublimeText.
Have a look here at some other people wanting to build their own syntax highlighting rules:
How to not highlight object keys such as 'do'/'package' as keywords in Sublime?
Sublime Text - C++ Highlight
That explains the basic tools you need to use to do what you want to do.
Following the same ideas there you can build a syntax highlighting scheme for whatever it is you want to achieve in your text file based on whatever syntax rules you are trying to follow. Hard to imagine what those are for a text file without you supplying exact details but if you want to do it Sublime Text gives you the power to do it.
If you give that a try and have trouble with developing the relevant Regular Expression/s to do what you want to do then post what you have done and how it is not working the way you hoped and perhaps we can help you get to the end of the game.

How can I create a short cut to wrap selection sublime text

I want to be able to select some content in sublime text, then press cmd+ctrl+l and then the selected content to be wrapped with an advanced version of markdown link. for example
[ $selection ](linkGoesHere "titleGoesHere")
You can likely get this working by writing a snippet or plugin to suit your needs.
You will be able to bind a plugin to your preferred keystroke, but snippets are triggered as you type in your file. Therefore a plugin is more likely to be your solution (since this requires some text to be written, selected, then the command executed), but if that ends up as a dead end, you might be able to use a snippet to accomplish the same task.

Resources