Remove trailing spaces automatically in Visual Code Studio

Being developers writing hundred of lines is not difficult as finding the errors. Trailing spaces while writing codes in Visual Code Studio might annoy some, hence, here is a way to remove them automatically. 

What is trailing space?

A trailing space while coding is the last but empty character that appears in a text entry field. For example, you have pasted some lines in Visual Code studio editor and it automatically added one empty character at the end of that, then while compiling it will give an error. However, to make things easy for developers we can enable the option to automatically remove the trailing spaces in the Visual Code Studio development environment platform.

What is trailing space


Enable automatic Trim Trailing WhiteSpace in VSCode

To remove the trailing space automatically while saving your code file in Visual Code studio, we can enable the in-built option available in this Microsoft IDE.

  • Open Visual Code Studio
  • Click on the File menu option.
  • Select Preferences and then Settings 
  • On the Settings Window select User Tab
  • Expand the Text Editor to select the Files option
  • Scroll to find Trim Trailing WhiteSpace
  • Click to check the box given in front of it to enable the automatic removal of Trailing WhiteSpace from the code as we save the file.

Enable automatic Trim Trailing WhiteSpace in VSCode


Command palette Shortcut to trim trailing whitespace

Now we can use the Keyboard shortcut or command palette to remove the whitespace.

If you want to use the Command palette to quickly access the option for trimming unnecessary white space at the end of the character, then press Ctrl+Shift+P and type Trim.

Soon you will have the option – “Trim Trailing WhiteSpace“, select that using the mouse.

Alternatively, you can also use the VSCode keyboard shortcut to trim trailing space:

For Windows, Linux press: Ctrl+K, Ctrl+X

For Mac: ⌘ + k, ⌘ + x

Shortcut to trim trailing whitespace


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.