Replace tabs with whitespaces

PLCopen has released their coding guidelines for 61131-3 structured text (ST) some time ago. If you haven’t read that yet, I highly recommend you to do so! Even if I don’t agree with everything, it’s still a good read and a really good initiative to consolidate all the different coding guidelines that every vendor has for their 61131-3 ST environment.

Included in this is Rule L16 “Define the use of tabs”. Let me quote:

Use of tab character (ASCII code 9) should be avoided, and Programming Support Environment set to replace tabs with spaces

This is a common rule in many projects, as this simplifies copying of code into other contexts and makes the code less dependent on the user settings for display. Simply put, the visual interpretation of a tab character varies wildly. As the TwinCAT development environment integrates into visual studio, this is the place we need to do our change. I’m going to use visual studio 2015 community edition for this particular demonstration, but it should be more or less the same in the other versions of visual studio.

First off, you can quite easily enable visualization of all white spaces (including tabs). This is done by going to “Edit” -> “Advanced” -> “View White Space“.

When enabling this, you will immediately see the tabs as right-pointing arrows in the code.

What you may also notice is that all the standard blank spaces are shown as interpuncts.

Now we want the development environment to automatically start using white-spaces instead of tabs for us. This is done by going to “Tools” -> “Options“. Then in the list you will find “TwinCAT” -> “PLC Environment” -> “Text editor“. Here you want to de-select “Keep tabs”.

TwinCAT text editor settings

TwinCAT text editor settings

I usually keep the tab width & Indent width set at 4 spaces.

That’s it! Now all your future editing in the code will create spaces instead of tabs in the code. It’s also possible to do replacement of all the current tabs into spaces by a regular-expression search & replace.

Your email address will not be published. Required fields are marked *