Code formatting Visual Studio Code - Line Length

Viewed 48328

I've been working with Visual Studio Code for a while. Since yesterday the code formatting is not working as I expect but I haven't changed any configuration.

This is the file without formatting

Code without formatting

Code without formatting

When it gets formatted:

Code formatted Code formatted

These are the settings modified by me

enter image description here

What can I do form improving this?

5 Answers

Open Extensions on the menu on the left or File menu.

Then click on the extension settings button.

Change the line length for the extension you are working with.

enter image description here

This is not TypeScript or VSCode doing this.

You have the prettier extension installed. Prettier will always do this, as prettier knows better

enter image description here

I had to create a .prettierrc file and set it up like this:

Prettier vscode configuration file line length

Here is a copiable version :)

{
  "tabWidth": 2,
  "useTabs": false,
  "printWidth": 120
}

If you are using Prettier extension, you can set the property printWidth from the settings.json file in VS Code:

"prettier.printWidth": 71

You can select the width according to the column selection, shown in the status bar.

enter image description here

I have a background in PyCharm and because i had been doing a lot of PHP programming, ended up using VS Code a lot.

So, when i returned to Python, i realised vs code has terrible formatting for Python code.


This is how i set up my vertical PEP8 79 character ruler in Vs Code.

  1. List item

Open Settings.json file Press Ctrl + Shift + P to open Command Palette and there type "Open Settings (JSON)" and select Preferences: Open Default Settings (JSON) from the list. This will open a Setings.json file.

  1. List item

Add the ruler's settings as shown below.

"editor.rulers": [ 79 ]

3 Close VS Code and Reopen it.

Vertical lines now appear at the 79th PEP8 character length.

If you want to increase characters like 80 or anything else then just replace 79 with any integer number.

Goodluck

Related