VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.
As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom!
In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F. To format a range, in an already opened project, open the document that you want to modify, select the specific range to format, right-click, and select Format Selection.
Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.
Use the extension called ”Format Files”. Here are the steps:
Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files
This works for me
Install prettier:
npm init
npm i prettier
Add following script in package.json:
"pretty": "prettier --write \"./**/*.{js,jsx,json}\""
In this case only, i need to format my .js .jsx and .json files.
Run script:
npm run pretty
The simplest solution that I have found is as below.
npx prettier --write "**/*.ts"
(Add the file type regex as per the need)
I was out of luck finding an extension that was doing this the way I was expecting it so I made one. I suggest you take a look at the extension I just made :
https://marketplace.visualstudio.com/items?itemName=lacroixdavid1.vscode-format-context-menu#overview
It might still have some issues, feel free to report them or to contribute.
With npm
, just run npx prettier --write .
in the terminal if your language is supported
You can use the extension Command on All Files
Add this to your settings.json
"commandOnAllFiles.commands": {
"Format File": {
"command": "editor.action.formatDocument",
"includeFileExtensions": [".js",".ts",".css"]
}
}
From the command palette select command Apply 1 or more commands to all files in the Workspace and select Format File from the Quickpick list. Wait till you see an information message (lower right) that all files have been processed.
I do a simply trick:
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With