How do you keep vscode from inserting extra spaces - visual-studio-code

I have made a pull request to a library on github. I am using vscode as the editor. It seems that vscode is removing a space after each comma created unnecessary changes in the diff. How can I stop this from happening.
I have a .vscode/settings.json with
"editor.formatOnSave": false,
"editor.insertSpaces": false,
This is what I am getting in my diff. Notice the extra space after the comma on line 55.
Thank you.

Turns out the above workspace config was not being respected. I had `editor.insertSpace:true" in the vscode global config. Taking it out solved.

Related

Prevent deleting dead code during Auto Format on Save?

What is the magic configuration for allowing a file to be auto-formatted upon a Save operation, but stop / disable / prevent VSCode from deleting dead code?
Sometimes I want to deliberately throw an exception in the middle of a function, for debugging purposes but am forced to comment out all following code in order not to get it deleted.
What's worse is that sometime I save while the editor didn't yet recover from some error in the code, thinks the code still contains an error, and causes code deletions which should not happen in the first place. I found myself too many times pulling up git in order to restore good code which was wrongly deleted.
Is there a clear "do not delete dead code" option to switch on?
UPDATE:
Running Prettier (the file's formatter) from command line did not delete dead code.
Trying to disable all extensions didn't help either. The dead code is still deleted upon save.
Case solved.
I can't pinpoint the exact package that's causing it but it seems to be related to either ESLint or Prettier.
Turns out that the project's Github repo contains a .vscode directory with a settings.json that contains the following configuration:
{
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
I already saw posts saying to add "source.fixAll": false to VSCode's general settings.json file, but it had no effect when I did.
Setting the flag to false did the trick
{
"editor.codeActionsOnSave": {
"source.fixAll": false
}
}
Note: Adding the above block to the general settings.json had no effect as well. I had to modify the local .vscode/settings.json file to get it to work.
source.fixAll activates also TS fixes, which are somehow too aggressive to use on save. Only enabling eslint fixes with source.fixAll.eslint is a good compromise - it will still report dead code (no-unreachable) but not delete it.
https://github.com/microsoft/vscode/issues/109530

YAML file formatting in VSCODE

I just started using VSCODE and faced with an annoyance every time I paste in YAML code in an existing YML file.
Basically the editor seems to auto format the document and in doing so messes up the significant spaces in the document. This causes builds in Azure Devops to break.
Although VS code formats the document nicely into collapsible regions, the formatting annoyance makes it hard to use.
Any help would be appreciated.
BEFORE:
AFTER:
I fixed this by changing editor.autoIndent settings for yaml and dockercompose language
"[yaml]": {
"editor.autoIndent": "advanced"
},
"[dockercompose]": {
"editor.autoIndent": "advanced"
}
In VsCode, press ctrl+shift+p (cmd+shift+p in Mac), and search for Preferences: Open User Settings (JSON). There I added this line:
"[yaml]": {
"editor.defaultFormatter": "redhat.vscode-yaml"
},
I picked it because it contains the word yaml, so I figured it must be yaml specific.
Anyhow, it seems to do a pretty good job for me.
Turn off the setting format on paste. This is a global setting, but plugins sometimes have their own, so if you're running a formatter like prettier, you'll need to see whether this is even an option with that plugin.
Looks like the problem is in the first line. Maybe when you copy the code, you're not copying the indentation on the first line.
One trick I use, is copying from the end of the preceding line, so the copied code starts with a newline, and then the paste is perfect.
Or just add the indentation on that first line, after pasting.

Is there a way to stop VS code from trimming spaces at end of lines?

I'm working in a project that has wonky whitespaces at the end of lines and don't want to remove them to keep the git diff down.
In .vscode/settings.json I have:
{
"editor.formatOnSave": false,
"files.trimTrailingWhitespace": false
}
But whitespaces are still trimmed. Is there any setting I can turn on to stop VS code from trimming on save?
Try disabling any plugins that might format on save.

VS Code automatically deleting internal spaces on save

I'm trying to make some statements visually line-up on the equal signs (per company style), which requires me putting extra spaces inside the line.
However, when I press save, VS Code deletes the spaces: even though I have all extensions off, and have it set to show all whitespace.
clip of problem, jump is on save
Any clue what might be causing this? I've tried restarting, toggling all the whitespace settings, disabling all extensions; no luck.
ctrl+comma
add line:
"editor.formatOnSave": false
You can also exclude some file types from format on save if you don't want to turn format on save off:
"editor.formatOnSave": true,
"[javascript]": {
"editor.formatOnSave": false
}

Visual Studio Code: How to show line endings

How can I display lineendings (CR,LF) in Visual Studio Code (not in Visual Studio)?
At the moment there is only the little statusbar menu which display/change the line ending if the actual file. But sometimes it would be great to see the line endings directly in every line especially when there are mixed line endings (not good, but this happens from time to time).
I use the following settings, but none of them show the line endings.
"editor.renderWhitespace": true,
"editor.renderControlCharacters": true,
"editor.renderIndentGuides": true
Is there a setting for lineendings?
I've opened a issue on GitHub: Possibility to display line endings in text area #12223
Soham Kamani made an extensions for this: code-eol
AFAIK there is no way to visually see line endings in the editor space, but in the bottom-right corner of the window there is an indicator that says "CLRF" or "LF" which will let you set the line endings for a particular file. Clicking on the text will allow you to change the line endings as well.
If you want to set it to LF as default, you can paste this line in your editor settings (F1 menu; > Preferences: Open Settings (JSON))
"files.eol": "\n"
Example:
{
"git.confirmSync": false,
"window.zoomLevel": -1,
"workbench.activityBar.visible": true,
"editor.wordWrap": true,
"workbench.iconTheme": "vscode-icons",
"window.menuBarVisibility": "default",
"vsicons.projectDetection.autoReload": true,
"files.eol": "\n"
}
Please note that this will change the default line ending for new files only. This will not edit your files.
Render Line Endings is a Visual Studio Code extension that is still actively maintained (as of September 2021):
https://marketplace.visualstudio.com/items?itemName=medo64.render-crlf
https://github.com/medo64/render-crlf/
It can be configured like this:
{
"editor.renderWhitespace": "all",
"code-eol.newlineCharacter": "¬",
"code-eol.returnCharacter" : "¤",
"code-eol.crlfCharacter" : "¤¬",
}
and looks like this:
You can install an extension to show line endings.
There are several available at the VS Marketplace.
Or if their search moves, try this relevant Google search
In the original answer, I had provided a link to a specific extension by Johnny Härtell After two years, this extension and the author are mysteriously missing from the VS Marketplace.
To provide a better experience and hopefully future proof this answer, I've updated it with search results that should keep us pretty close to a relevant extension.
I used the "find" and simply did a Regex search for "\n". Which seems to show the new lines in a simplistic but useful manner. Hope this helps.
Another way to set the default end of line value in Visual Studio Code:
Navigate to the Visual Studio Code settings tab (e.g., by Ctrl + , (comma))
Search for end of line in the search bar
Set the desired value in the Files: Eol dropdown menu
Screenshot:
Having the opposite problem? How to hide line endings:
Oddly I have the opposite problem! I just ended up with the following highlighting of each newline - which I've never seen before. In all open files and without a selection. Assumed I'd hit a shortcut by mistake, which is how I ended up on this question. Given that the feature doesn't seem to actually exist I resorted to closing and reopening and they went away!