Issue #16 (Whitespace Characters)08/10/22
|
Get a weekly dose of tools, articles, and other resources covering remote work, productivity, career well-being, and lots more.
|
|
|
VS Code has a few different settings that allow you to control the visibility and even the existence of whitespace characters. It will depend on what your preference is and what type of code you're writing, but you may strongly prefer to change the defaults in this instance.
If you type "whitespace" in your Settings search box, you'll see a number of options show up. Probably the most useful of the whitespace-related settings is the Render Whitespace option, which has five possible values.
Setting this to "all" will cause your files to essentially look like the screenshot shown below where I've added some arrows to show where some of the characters are:
Notice the dot characters that appear everywhere that there's no physical character. There are some trailing dot characters, indicating trailing whitespace, and there are even dots to indicate single whitespace characters between words. This might be overkill so you can choose one of the other options:
- boundary – Renders all whitespace characters except single spaces between words
- selection – The default setting, rendering whitespace only on selected text.
- trailing – Only renders trailing whitespace
Another pair of useful whitespace-related settings have to do with
removing trailing whitespace. Firstly, if you're still looking at the search results for "whitespace", you'll see an option to "Trim Trailing Whitespace". This is off by default, but turning it on will remove any trailing whitespace when the file is saved.
This means all lines in the file that include any trailing whitespace characters (as shown in the image above) will be deleted. Such characters might creep in from copying and pasting from somewhere online, or even in an old inherited project.
The only thing this won't remove is trailing new lines, which can also creep in, sometimes just from naturally hitting enter/return multiple times at the end of a file. This setting doesn't show up using the "whitespace" keyword search. You'll have to search for "Trim Final Newlines" to find this one. This will automatically remove trailing newlines, as shown in the following image.
The setting will remove all but one of the trailing new lines, which is good. I personally like to have those trailing lines and characters removed from my files, so these are definitely settings I keep on. Along with the option to autosave files, trailing whitespace characters and lines will hardly even be noticed in most cases.
Now on to this week's hand-picked links!