In VS Code with Python how do I make the first parameter (self) in class methods italics?

Viewed 1943

I'm using Python I came from Pycharm where it automatically formatted the first parameter (often self) in class methods to be in italics which I found easier to review when going through my code. Can you configure VS Code settings to do this? I see there is the following and I played with it adding function color but couldnt get the ability to just make the first parameter of class methods be in italics for Python.

https://code.visualstudio.com/docs/getstarted/themes

Thx.

1 Answers

You are able to do this by editing the settings.json file. To open this file and get to the settings, press Ctrl+Shift+P and enter Open Settings (JSON) from within VS code. Then, you'll want to add the following keys and values at the top level:

Edit: I had forgotten the "textMateRules" key which is why you were having trouble seeing the changes. I also commented out everything except "variable.parameter.function.language.special.self.python" since you mentioned that's all you want modified.

{
    "editor.tokenColorCustomizations": {
        "textMateRules": [
            {
                "scope": [
                    //"variable.language.special.self.python",
                    "variable.parameter.function.language.special.self.python",
                    //"variable.language.special.cls.python",
                    //"variable.parameter.function.language.special.cls.python"
                ],
                "settings": {
                    "fontStyle": "italic"
                }
            }
        ]
    }
}

What you're doing here is telling the syntax highlighter to take the self and cls keywords (everything within the "scope" list) and apply an italic style to them.

The "scope" list is (in English):

  1. "variable.language.special.self.python": The keyword self that is not part of a function definition argument list.
  2. "variable.parameter.function.language.special.self.python": The keyword self when it is used as part of a function definition argument list.
  3. "variable.language.special.cls.python": Same as (1) but with cls.
  4. "variable.parameter.function.language.special.cls.python": Same as (2) but with cls.

This changes their appearance everywhere which may or may not be what you want. For example, these changes give me (along with my current theme) the following look:

italic self and cls

You should be able to enter any combination of values (separated by a space) in place of italic. For instance, you can enter bold underline if you want self and cls to be bolded and underlined instead.

Related