How to remove trailing spaces in Visual Studio Code

July 29, 2024 19:11 by Jesper Nielsen • 6 minutes to read

Why Trailing Spaces Matters

I really do not like trailing spaces. Trailing spaces are any extra spaces or tabs at the end of a line of code or text. They may seem harmless, but they can cause problems for developers, editors, and users - and they somehow burn my eyes :)

Trailing spaces can affect the efficiency of the code or file in several ways. First, they can increase the file size unnecessarily, which can slow down the loading and processing time. Second, they can interfere with the version control system, which tracks the changes made to the code or file. Trailing spaces can create false differences or conflicts, which can waste time and resources to resolve. Third, they can cause errors or bugs in some languages or tools, which can affect the functionality and performance of the code or file.

Trailing spaces can also affect the readability of the code or file, which is crucial for the understanding and maintenance of the code or file. Trailing spaces can make the code or file look messy and inconsistent, which can reduce the clarity and quality of the code or file. Trailing spaces can also make the code or file harder to edit or format, which can affect the productivity and collaboration of the developers or editors.

To find and remove trailing spaces in all files within your Visual Studio Code workspace, try using these steps:

  • Press Ctrl + Shift + H to open the search-and-replace side panel.
  • Enable Regular Expression Search by clicking the .* button next to the search field to enable regular expression search.
  • Search for Trailing Spaces, type \s+$ which matches one or more whitespace characters at the end of a line.
  • Leave the replace field empty to remove the trailing spaces.
  • Click on the Replace All button to remove all trailing spaces in the files within your workspace.

You can configure Visual Studio Code to automatically trim trailing whitespace on save:

Press Ctrl + , to open the settings screen.

  • In the search bar, type Files: Trim Trailing Whitespace.
  • Check the box next to: When enabled, will trim trailing whitespace when saving a file.

This way, every time you save a file, Visual Studio Code will automatically remove any trailing whitespace.

Additionally, you can configure how Visual Studio Code render whitespace characters:

Press Ctrl + , to open the settings screen.

  • In the search bar, type Editor: Render Whitespace.
  • In the drop down, select trailing.

Other controls for Visual Studio Code to render whitespace characters.

  • none
  • boundary: Render whitespace characters except for single spaces between words.
  • selection: Render whitespace characters only on selected text (Default).
  • trailing: Render only trailing whitespace characters.
  • all

For those utilizing Source Control such as Git, it is advisable to configure Visual Studio Code to recognize changes in leading or trailing whitespace, ensuring consistency with the output of git diff.

You can configure how Visual Studio Code handle changes in leading or trailing whitespace:

Press Ctrl + , to open the settings screen.

  • In the search bar, type Diff Editor: Ignore Trim Whitespace.
  • Uncheck the box next to: When enabled, the diff editor ignores changes in leading or trailing whitespace.

A final adjustment I would suggest is the Trim Final Newlines setting.

You have the option to set Visual Studio Code to trim any trailing new lines at the end of a file upon saving:

Press Ctrl + , to open the settings screen.

  • In the search bar, type Files: Trim Final Newlines.
  • Check the box next to: When enabled, will trim all new lines after the final new line at the end of the file when saving it.

This way, every time you save a file, Visual Studio Code will automatically trim all new lines after the final new line at the end of the file when saving.

The wrap

Removing trailing whitespace in VS Code is a straightforward yet effective way to enhance your coding practice. By following these simple steps, you can ensure your code remains clean and consistent, aligning with professional development standards. This not only improves the readability of your code but also facilitates a smoother collaboration process with others by avoiding unnecessary changes in version control systems.

Removing unnecessary trailing space in Visual Studio Code is an uncomplicated but valuable method to refine your coding habits. Adhering to these easy-to-follow procedures guarantees that your code stays tidy and uniform, meeting the criteria of professional coding practices. Not only does this sharpen the clarity of your code, but it also streamlines teamwork by preventing superfluous modifications within version control systems.

One last thing

Visual Studio Code is a powerful and customizable code editor that offers a variety of features and extensions to enhance your coding experience.

There are two ways to access and modify the settings in Visual Studio Code: through the Settings UI or through the JSON settings file.

If you prefer to use the JSON settings file, you can edit the file directly within Visual Studio Code.

While the Settings UI is convenient and easy to use, it has some limitations. For example, it does not show all the available settings, it does not allow you to add comments or custom properties, and it does not support conditional settings based on the workspace or the file type.

To open the Visual Studio Code User Settings JSON file, follow these steps:

  • Launch Visual Studio Code and click on the File menu.
  • Select Preferences > Settings.
  • In the Settings tab, click on the icon that looks like a page with an arrow on the top right corner. This will open the User Settings JSON file in a new tab.

Alternatively you can use the keyboard shortcut F1 or Ctrl + Shift + P to open the Command Palette, and type Open User Settings (JSON) and press Enter. If you want to see all available settings, type Open Default Settings (JSON) and press Enter.

Configure Visual Studio Code to automatically trim trailing whitespace on save

// When enabled, will trim trailing whitespace when saving a file.
"files.trimTrailingWhitespace": false,

Configure how Visual Studio Code render whitespace characters

// Controls how the editor should render whitespace characters.
//  - none
//  - boundary: Render whitespace characters except for single spaces between words.
//  - selection: Render whitespace characters only on selected text.
//  - trailing: Render only trailing whitespace characters.
//  - all
"editor.renderWhitespace": "trailing",

Configure how Visual Studio Code handle changes in leading or trailing whitespace

// When enabled, the diff editor ignores changes in leading or trailing whitespace.
"diffEditor.ignoreTrimWhitespace": false,

Configure Visual Studio Code to trim any trailing new lines at the end of a file upon saving

// When enabled, will trim all new lines after the final new line when saving it.
"files.trimFinalNewlines": true,

Happy coding.

–Jesper