Search code examples
pythonvisual-studio-codepython-black

Format code in vscode setting for line break


We can use Ctrl+Shift+i for auto-formatting codes in vs code. vscode break lines longer that 80 character. Changing linewidth does not change it. I want to put that 120 in my python code. What is the solution?

I did not find similar questions digging previous ones.

This is my setting.json :

{
    "workbench.panel.defaultLocation": "right",
    "workbench.startupEditor": "none",
    "workbench.sideBar.location": "right",
    "python.pythonPath": "/usr/bin/python3",
    "editor.minimap.enabled": false,
    "workbench.colorTheme": "Monokai",
    "C_Cpp.updateChannel": "Insiders",
    "update.showReleaseNotes": false,
    "update.mode": "manual",
    "workbench.editorAssociations": [
        {
            "viewType": "jupyter.notebook.ipynb",
            "filenamePattern": "*.ipynb"
        }
    ],
    "files.associations": {
        "*.rmd": "markdown"
    },
    "window.zoomLevel": 1,
    "prettier.printWidth": 120,
    "editor.wordWrap": "wordWrapColumn",
    "editor.wrappingIndent": "same",
    "editor.wordWrapColumn": 120

}

as @Subrato suggested this worked for me:

"editor.defaultFormatter": "esbenp.prettier-vscode",
  "[python]": {
    "editor.defaultFormatter": null
  },
  "python.formatting.blackArgs": ["--line-length", "120"],
  "python.formatting.provider": "black",

Solution

  • Add this setting in your settings.json file in vs code.

    "editor.wordWrap": "wordWrapColumn",
    "editor.wrappingIndent": "same",
    "editor.wordWrapColumn": 120
    

    Remember editor.wordWrapColumn: 120 alone will not work you also need to add editor.wordWrap: 'wordWrapColumn'.

    @Updated Prettier doesn't work with Python. autopep8 format is required for formating python files.

    Use pip install pep8 for installing pep8 into your vs code editor

    "python.formatting.provider": "autopep8",
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
     }
    //custom config for python 
    "python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],