I have a bunch of .rst files containing reStructuredText. ST3 colors syntax in the files nicely. I also have .txt files that I would like ST3 to highlight in this same way (they are include files, and because I am using Sphinx, they cannot be named .rst). Can I make ST3 display these .txt files using the same syntax highlightings as for .rst files? Is this possible to do on a project level? So that, for example, .txt files display normally in other projects. I’m a newbe to ST so pls forgive if this was covered. Couldn’t find anything on it.
Sublime Text 3 syntax highlighting
Have you tried View -> Syntax -> Open all with current extension as -> reStructuredText when editing a txt file?
Yes. No. As QED1224 mentioned you can make all .txt files be opened as reStructuredText but you can’t do that per-project.
On another note, why does Sphinx require you to use .txt files? I am using it as well and using .rst files just fine.
[quote=“FichteFoll”]
Yes. No. As QED1224 mentioned you can make all .txt files be opened as reStructuredText but you can’t do that per-project.
On another note, why does Sphinx require you to use .txt files? I am using it as well and using .rst files just fine.[/quote]
Thanks QED1224. That works but I was hoping for a project setting to do this.
As for why, I get warnings and errors from Sphinx when I attempt to include .rst files from other .rst files. My index.rst file has (blank lines removed for brevity, and indents removed by this bbs editor):
… toctree::
:maxdepth: 2
Introduction
And then in introduction.rst:
… include:: lectures/lecture-01.rst
This gives a warning that lecture-01 is not included in any toctree. Also in the real project I get errors concerning duplicate labels and references. Changing .rst to .txt allows compilation. I don’t know why but I’ve not been able to get around this.
A tiny plugin for this should be easy enough. Just listen to the on_load event, check project_file_name(), then call set_syntax_file as applicable for the project.
[quote=“Tori”]
A tiny plugin for this should be easy enough. Just listen to the on_load event, check project_file_name(), then call set_syntax_file as applicable for the project.[/quote]
Here’s a rough example of the above (tested on 3095):
import sublime_plugin
class TextHighlighter(sublime_plugin.EventListener):
def on_load(self, view):
project_name = view.window().project_file_name().split('/')-1]
if project_name == 'Test.sublime-project': # Replace with project name
if 'Plain text' in view.settings().get('syntax'):
view.set_syntax_file('reStructuredText.sublime-syntax')
You would just need to add your project name and place it in your User Packages folder.
[quote=“QED1224”]
[quote=“Tori”]
A tiny plugin for this should be easy enough. Just listen to the on_load event, check project_file_name(), then call set_syntax_file as applicable for the project.[/quote]
Here’s a rough example of the above (tested on 3095):
import sublime_plugin
class TextHighlighter(sublime_plugin.EventListener):
def on_load(self, view):
project_name = view.window().project_file_name().split('/')-1]
if project_name == 'Test.sublime-project': # Replace with project name
if 'Plain text' in view.settings().get('syntax'):
view.set_syntax_file('reStructuredText.sublime-syntax')
You would just need to add your project name and place it in your User Packages folder.[/quote]
Awesome ! A future improvement would be to have a flag in the project file (or an external file) instead of hardcoding project names.
But a great start.
[code]import os.path
import sublime_plugin
class TextHighlighter(sublime_plugin.EventListener):
def on_load(self, view):
overrides = view.settings().get(‘syntax_overrides’, {})
_, ext = os.path.splitext(view.file_name())
if ext in overrides:
view.set_syntax_file(overrides[ext])
[/code]
Project file could look like this:
[code]{
“folders”:
{
"path": "."
}
],
"settings": {
"syntax_overrides": {
".py": "Packages/JavaScript/JSON.sublime-syntax"
}
}
}
[/code]
Edit: I figured that ApplySyntax might support this kind of common use pattern, and actually it does, so you could just use that: facelessuser.github.io/ApplySynt … ific-rules
It’s also more powerful.