Setting tabs for code formatting

So, I like to use tabs for code formatting. I searched and found the hidden. cmd-shift-p thing that can be used to allow this. I was able to even set 3 spaces for tabs as I prefer.

But then, the next time I open the file, it has switched back to the nonsense it used before.

How do I get it to save my 3 space for tab setting?

Thanks!

-jim lee

I asked this about 2 years ago. And I was said that tabs are nonsense, all modern programmers use spaces :))))))))).

2 posts were split to a new topic: Arduino IDE is not the best choice

There is also a setting in preferences.txt in the Arduino15 folder:

editor.tabs.size=2

I shut down the IDE and tried changing this to 4, but it made no difference. After re-starting the IDE, the tab size was still 2, so the setting is not being read and applied. In the Cmd+Shift+P menu, the Change Tab Display Size option only seems to apply within the current session and does not change the setting in preferences.txt.

Not sure whether this is by design or a bug?

The spaces vs tab thing is an old problem whereby some editors inserted tabs and others spaces. If you used different editors to edit the same piece of code you ended up with a mixture of both tabs and spaces which eventually would end up looking a bit messy. Current wisdom seems to be to find and enable the setting in your editor that converts tabs to spaces and set your preferred tab size in each editor you use. That way, only spaces get inserted, but the editor still interprets indents as "tabs". I have found this to be a more consistent approach.

Check the .clang-format file under home .arduinoIDE on linux.

A typical change might be
BreakBeforeBraces: Allman
to
BreakBeforeBraces: Whitesmiths

There's many options, you can see what's available and the resultant effect on formatting here:

Choose version 18 and based on LLVM for a starting point:

There is numerous discussions on using clang format here on the forum.

Hi @jimLee. Please try this:

  1. Press the Command+Shift+P keyboard shortcut to open the "Command Palette".
  2. Select the "Preferences: Open Settings (UI)" command from the menu.
    You can scroll down through the list of commands to find it, or type the command name in the field.
    A "Settings" tab will open in the Arduino IDE main panel.
  3. Type editor.detectIndentation in the "Search Settings" field of the "Settings" tab.
  4. If the checkbox next to the "Editor: Detect Indentation" setting is checked, uncheck it, then restart Arduino IDE.

Does that resolve the problem?

Sadly no. Did the steps. reset the file to 3 spaces / tab. made a change, saved the file. Closed it all up. Restarted opened the file and it’s back to it’s two spaces per tab.

ROFL, programmers are not know for their open mindedness. I know, been one forever.:grinning_face_with_smiling_eyes:

A post was merged into an existing topic: Arduino IDE is not the best choice

You can influence the behaviour of the auto format. You will need to create a file called .clang-format (note the dot) in the configuration directory. You can find where the configuration directory for your operating system is at https://support.arduino.cc/hc/en-us/articles/4415103213714-Find-sketches-libraries-board-cores-and-other-files-on-your-computer#boards (search for Configuration folder).

You can find the .clang-format that is used by default at arduino-examples/.clang-format at main · arduino/arduino-examples · GitHub.
I've attached my .clang-format file; it contains some more changes like "Allman" to get braces on new lines for conditional statements. My changes are commented with WS.
It should be safe but run a virus scanner over it to be sure.

Search the .clang-format file for IndentWidth: 2 and change the value. Save the file; you do not need to restart the IDE when making changes to that file.

.clang-format.zip (2.0 KB)

Notes

  1. There are a few more indent settings.
  2. You can test at http://clang-format-configurator.site/
  3. As a guide for the various settings you can consult Clang-Format Style Options — Clang 23.0.0git documentation; the IDE does not use that version, I think it's version 13 (not sure).

I'm not sure I understand what you mean by this. Please provide a detailed description so that I can understand:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?

When you say "back to it’s two spaces per tab", are you referring to the content of the file when you open it, the indentation Arduino IDE uses when you press the Tab key, the indentation that results from using the Tools > Auto Format feature, or something else?

You may also need to adjust the value of TabWidth. There is a detailed explanation here:

https://github.com/arduino/arduino-ide/issues/1133#issuecomment-1182645911