Set syntax for a specific file name in Sublime Text 2/3 - sublimetext2

I have a program that uses a file called user.cfg to get its user defined configuration settings. The odd thing is that they chose the syntax for this file to be Tcl (it's not odd that it is Tcl, it's odd they chose the .cfg extension instead of .tcl). So, when I open this file in Sublime Text, it doesn't know what syntax highlighting scheme to choose.
What I would like to do is set the syntax highlighting for user.cfg to Tcl, but not all .cfg files to Tcl.
I have seen this question which is very similar to mine, except in that case the special file name had no extension so Sublime Text knew to assign Ruby highlighting to only that one file. Unfortunately, I have an extension so the solution given there will not work for me.
Is there any known way to get Sublime Text base a highlighting scheme on the full filename?

Take a look at the ApplySyntax plugin.

The previous answer is completely true; however, I thought it would be better to have it here all in one place rather than going on another webpage to find the list of procedure to apply it
Sublime text 3
This is found here
Ensure Package Control is installed. Instructions are found here.
In Sublime Text, press Ctrl+Shift+P (Win, Linux) or Cmd+Shift+P (macOS) to bring up the quick panel and start typing Package Control: Install Package.
Select the command and it will show a list of installable plugins.
Start typing ApplySyntax; when you see it, select it.
Restart to be sure everything is loaded proper.
Enjoy!

Related

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.

view.run_command("example") not working

I created first plugin for sublime text 3 with name relative:
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
Then save it to path
/pathToSublime3/Packages/relative/relative.py
Then try to test it in command window (cntrl + `):
view.run_command('example')
I expected to see Hello, World! in the beginning of current opened file, but fail. What I do wrong?
If your experience was like mine, you may have copied several super basic plugins to try to find one that worked. I think the failure of the others was affecting the Hello World one. For what it's worth, here are the things I did that seem to have worked.
I'm using ST3 on Win7, so maybe there are differences in plugin development between that and ST2.
Note that as I tried many different permutations of these, I don't exactly know which ones are the real fix. But if these get you working, then maybe you can start making incremental changes and see when it breaks.
Moved all *.py files that were my plugins into "Packages" folder. (I read many posts that said that plugin files in the "Packages\User" or "Packages\NameOfPlugin" folder should be read, but I haven't tried them yet. I put it in "Packages" to make sure that finding the files wasn't a problem.)
Restart Sublime after every change. (The docs say you don't have to, but if you have errors in your plugins, it seems that the auto-reload feature doesn't work. Which kinda makes sense. Once I got the errors out of all my plugins, the auto-reload feature started working correctly.)
A few of the simple/tutorial plugins I copied off the web (even some from sublimetext.com!) referenced/imported "sublimePlugin" which errorred on my ST3 Win7 instance. Apparently the correct library to import is "sublime_plugin". The two places this is used, in basic plugins, are 1) at the very top in the import section, as well as 2) in the class line, which I'll illustrate below...
# didn't work for me...
class ExampleCommand(sublimePlugin.TextCommand):
# DID work for me...
class ExampleCommand(sublime_plugin.TextCommand):
'Ctrl+N' create a new file, then try it again. Don't run it in the old file.
All I need is close sublime text 3 and open it again. Now Hello World works fine
The console doesn't open with Ctrl + ` For Sublime Text 2, but this example is written in the v2 documentation.
Using that command opens up a popup starting with # which is to open another file.
The console opens by doing Ctrl + F1. After that, you can write that code in there, and the text will be added into your current view (in any view, in fact).
Be sure to read the "Where to Store Plugins", I made the mistake of creating a folder (package) when the hello_world.py is simply a plugin. Also keep in mind from the documentation window commands and text commands, cause in this case since it is a text command you must execute it in the "view" that you want it to place the text (which can't be the plugin definition file that you created.
Hope that helps anyone in the future as that was a few things I came across

Sublime Text 2 - writing a script to process multimarkdown

There is an excellent package to preview markdown written using sublime text. I wanted to know how I might modify it, or perhaps use it as the basis for writing one that could process multi-markdown.
I'm using Linux (Ubuntu) and I'm currently calling multimarkdown in the terminal on each file.
One of the issues I think I might face is that multimarkdown refuses to parse an open file for some reason.
Any thoughts on how I might begin this or if an alternative solution exists would be very gratefully received.
There is a MultiMarkdown option in the syntax list - View > Syntax > Markdown > MultiMarkdown. Isn't that working? By the way the syntax (and almost any other) files are in packages folder of Sublime Text 2. This is the syntax file:
/Users/[username]/Library/Application Support/Sublime Text 2/Packages/Markdown/Markdown.tmLanguage
And MultiMarkdown file is in the same directory.
I'm intrigued by your statement that "multimarkdown refuses to parse an open file". Can you provide more information, or email me, or open an issue on github? MultiMarkdown doesn't check to see whether a file is open or not --- it simply reads the file and processes it. So if there's a problem, then the OS is not allowing MMD to proceed for some reason...
I don't have Sublime Text installed on Ubuntu (I generally use a command line only version of Ubuntu for testing MMD), so I can't test this situation exactly. I haven't had any other reports of difficulties parsing files (open or not). I don't have any trouble with Sublime Text 2 on Mac OS X and MMD.
As an alternative, you could try using one of the support scripts (e.g. mmd if you want MMD->HTML) and see if that has the same problem with open files.
PS> Are you using the latest build of MultiMarkdown, e.g. 4.2+?? (Though it shouldn't change anything related to open files)

How to change the load path in Octave

I have a problem with loading images and editing scripts in Octave. I think Octave has the wrong path or something. When I say 'edit', it says:
sh: 1: emacs: not found
What does that mean?
I'm pretty new to both Octave and MATLAB, but when I'm at school using MATLAB, for example, writing the command 'edit' works...
How do I know where to put files I want to load in Octave, and how do I change the path?
The problem is not with your path, the problem is that you didn't set up EDITOR (see help EDITOR and the editor option at help edit). As you have no environmental variable for it, it defaults to emacs so when you run edit, it tries to open the file in emacs and fails because you don't have it installed. Set up EDITOR for your text editor of choice at .octaverc file (If I remember correctly, the text editor to use is set as a persistent variable inside edit.m so you'll have to restart octave) or an environmental variable (I'd recommend this one).
It works in matlab because they made their own text editor. Octave is not a text editor, it's a programming language, it would be a waste of time to reinvent the wheel, and you should be able to choose whatever you are comfortable to code with.

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