131

I use Visual Studio Code to write Python code with Pylint.

When I press Ctrl + S (save), the editor wraps a long line into multiple short lines. How do I disable the action or configure wrap column count to 120 (default is 80)?

I have tried "python.linting.pylintArgs": ["--max-line-length=120"] and "editor.wordWrapColumn": 120, but it didn't work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kymo Wang
  • 1,413
  • 2
  • 10
  • 9
  • 2
    Do you have automatic formatting turned on? VS Code itself won't reformat anything, so `editor.wordWrapColumn` won't affect that as that -- along with `editor.wordWrap` -- only affect how to display long lines, not rewrite them. – Brett Cannon Nov 27 '17 at 22:00
  • 4
    If you use `autopep8`, do the following: open "Settings"-> search "python formatting autopep8 args" -> click "Add Item" -> type `--max-line-length=120` (change 120 to you preferred length). – Divelix Sep 29 '21 at 09:17
  • I want to enable this setting but my editor wont wrap lines at all – Aseem Jul 21 '23 at 00:48

5 Answers5

280

Check your Python formatting provider.

"python.formatting.provider": "autopep8"

I guess in your case it is not Pylint which keeps wrapping the long lines, but autopep8. Try setting --max-line-length for autopep8 instead.

"python.formatting.autopep8Args": [
    "--max-line-length=200"
]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chayapol
  • 3,718
  • 1
  • 21
  • 12
  • 12
    It also helps if the linter behaves similarly to the formatter: "python.linting.pep8Args": ["--max-line-length=120"] – Lian Apr 05 '18 at 15:15
  • 3
    For information about other available arguments, see https://pypi.org/project/autopep8/ – Jerren Saunders Feb 04 '19 at 15:51
  • 1
    TLDR: PEP8 indicates that the ideal line wrapping limit for Python is 80, so this is the default for the autopep8. – ProfDFrancis Oct 28 '19 at 06:52
  • 34
    with pure point & click achieve this with: "Code -> Preferences -> Settings". Then select "Extensions -> Python". Browse through to "Formatting: Autopep8 Args" and its "Add item", in the proposed box, add "--max-line-length=200" – loic.jaouen Apr 23 '20 at 06:23
  • 16
    `"--ignore=E501"` to completely ignore max-line-length. – Jeppe Jun 23 '20 at 07:07
  • To get this to work you may also have to change the **Editor: Word Wrap Column** setting. – Ryan Lundy Sep 30 '21 at 05:48
  • @Jeppe How can we that kind of setting exists? It works perfectly thank! – Say OL Mar 25 '22 at 04:02
15

When using custom arguments, each top-level element of an argument string that's separated by space on the command line must be a separate item in the arguments list. For example:

"python.formatting.autopep8Args": [ 
  "--max-line-length", "120", "--experimental" 
],
"python.formatting.yapfArgs": [
  "--style", "{based_on_style: chromium, indent_width: 20}"
],
"python.formatting.blackArgs": [
  "--line-length", "100"
]

For proper formatting of these Python settings you can check Formatter-specific settings:

Also check the answers here:

Allow statements before imports with Visual Studio Code and autopep8

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
Meetai.com
  • 6,622
  • 3
  • 31
  • 38
1

If you're using yapf as your formatter then the option is column_limit. For example, from settings.json:

"python.formatting.provider": "yapf",
"python.formatting.yapfArgs": [
    "--style={based_on_style: google, indent_width: 4, column_limit: 150}"
],
Andy Brown
  • 11,766
  • 2
  • 42
  • 61
0

I tried the solution with different way. Search in VSC settings for "Autopep8". And as you can see there is an "Add Item" button in here. You can simply click it and paste the code there. The code will add in your "settings.json" file.

screenshot

thedemons
  • 1,139
  • 2
  • 9
  • 25
Gaaartch
  • 1
  • 1
-2

Autopep8 requires --aggressive in order to recommend non-whitespace changes:

"python.linting.pylintArgs": ["--max-line-length", "120", "--aggressive"]

This will wrap the long lines for you.

Rob Haswell
  • 87
  • 1
  • 3