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.

  • Share on:

5 Comments, RSS

  1. Yannick Asselin

    Hi,

    I have been looking for the “View White Spaces” since I started using TwinCAT3 a couple years ago.
    The problem is I still can’t find it in Visual Studio 2013 shell that is installed when installing TwinCAT3.
    Do you know if this option is available or not in this version? Maybe located somewhere else?
    We are not using the community edition as I don’t think it is legal to use inside our company.

    Thank you.

    • Jakob Sagatowski

      Hi Yannick! I don’t have VS2013Shell installed, but I think it should be available there as well. I’ve noticed that this option only appears in Visual studio if you have selected a window where you have code (so for instance, in a function block header definition). If you don’t do this (and for instance have the selection in the solution explorer) the option “Edit->Advanced->View white spaces” is not visible. So try again by making a selection in a window where you have code, and then go to the edit-menu.

  2. thierry

    Greetings Jakob, I was just testing Github Gist with some Twincat code and ‘found’ the annoying tab/space problem.
    So the solution is to avoid the tabs, but could you explain how I can do a ‘search and replace’ for the tabs? Thx

    • thierry

      Oops, I was to fast with my question. It seems you can just select a ‘tabbed space’ (no idea how to call this) and then put in in the find field. Then put 4 spaces in the replace field.

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

*

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