Sublime Forum

Comments in plain text

#1

I would like to be able to use comments similar to the Bash style in a plain text document.
So when a line begins with # i would like all text following on that line to change to a different color.
I have attempted to edit my color scheme (Mariana) by editing the Comment rule:
{
“name”: “Comment”,
“scope”: “comment, punctuation.definition.comment”,
“foreground”: “var(blue6)”
},

However it does not change the colors of comments.
I suspect it is because the scope is not accurate for the type of comment I want to use, but I don’t know what that should be.

I have also attempted to find the correct type using the PackageResourceViewer, but when i try to open the resource for Bash it is not listed.

To be clear, the file is not a bash file, it is note code, it is a plain text list that i want to be able to change font color of a given line like a comment in code.

Thanks

0 Likes

#2

Syntax highlighting requires both - a syntax definition (provided by .sublime-syntax files), which assigns scope names to tokens, and a color scheme (.sublime-color-scheme), which assigns colors to those scopes.

Plain Text syntax definition doesn’t provide any rules to scope anything.

0 Likes

#3

I use these two Syntax Definitions for more or less that purpose (actually, to view miscellaneous config files that have one of these two stances about what are valid comments)

SourceWithComments.sublime-syntax

%YAML 1.2
---
name: 'Source (with #… Comments)'
file_extensions: []
scope: source.with-comments
contexts:
  main:
    - match: '#'
      push:
        - meta_scope: comment.line
        - match: ^
          pop: true

SourceWithWholeLineComments.sublime-syntax

%YAML 1.2
---
name: 'Source (with ^#… Comments)'
file_extensions: []
scope: source.with-comments.whole-line
contexts:
  main:
    - match: '^#'
      push:
        - meta_scope: comment.line
        - match: ^
          pop: true
0 Likes

#4

Thank you so much for this.

Do you know how I can chose the color of the comment?
Do i just add a “comments”: colorvalue line to the .sublime-color-scheme file?

Edit -
Found a file in [C:\Users{USER}\AppData\Roaming\Sublime Text\Packages\User] ending in .tmTheme In that XML file was a code block that contained:

name Comment scope comment settings foreground #747166

Changing the hexcode in that block allowed me to choose the color desired for the comments.

Edit 2 -
After further testing it seems the following 4 files are required to be in the above mentioned directory for the theme to work. In this case I named it PlainTextComments:
PlainTextComments.sublime-settings
PlainTextComments.sublime-syntax
PlainTextComments.tmLanguage
PlainTextComments.tmTheme

If those four files are present with the correct coding, then i can put a # in front of a line and it changes the color and essentially makes a comment in a plain text file.

Sorry if this is obvious, but i wanted to add it for others like myself who didn’t realize.

1 Like

#5

I just realised that, given your original use-case, it might actually make sense to replace the scope source.with-comments with text.plain.with-comments in the various file(s). Because otherwise for example the spellchecker will not be active in the non-comment lines.

Your 4-file approach sounds a bit overcomplicated to me, but it’s probably too hard to debug over the forum.

When a file is set to use this new Syntax (either manually, or automatically based on its file extension), the comments should already have the normal comment colour (like in Bash) without having to configure anything extra. To give them a special custom comment colour, I would run UI: Customize Color Scheme from the Command Palette and then add a rule such as the following to the rules array in the file on the right side:

{
    "scope": "source.with-comments comment",
    "foreground": "#9900bb"
},
1 Like

#6

Oh, that actually works a lot better than the four files. Well, it provides the same result but is a lot easier to understand and implement. I think i ended up with extra files from modifying color schemes, maybe? Either way adding the one yaml file and then modifying the used color scheme works great. Thank you!

1 Like