skip to Main Content

How to remove the line breaks that’s added in the formatting when saving the code in VSCode

For example:

If I have an expression with multiple validations:

if True == True or False == True or False == False:

It is being formatted as:

if (True == True
or False == True
or False == False)

This complicates readability. How can I reformat it to look like the first example?

2

Answers


  1. First check to see which formatter VSCode is using to format your code. In most cases (I tried Black and Ruff), you can disable the formatting of a specific piece of code by wrapping it inside the below block:

    # fmt: off
    lst = [1, 2, 3,
           4, 5, 6,
           7, 8 ,9]
    # fmt: on
    

    Without that comment, it would put them in single line.

    I guess most formatters will respect that comment.

    Login or Signup to reply.
  2. You can use the shortcut Ctrl+, to open the settings. Input format on save and deselect the Editor:Format On Save option. This disables the use of code formatting when saving documents globally. This preserves the format of the code you wrote it.enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search