Webからスケッチをコピーしてきた場合など、インデントが崩れてしまってスケッチが見辛い場合がある。量が少なければ手動でタブを追加して整形できるが、量が多いと整形するだけでも一苦労。
そんな時は、ツールメニューの「自動整形」を実行すると自動的に綺麗に整形しなおしてくれる。


スケッチを書く際if文やforループ、case等見易くするために行頭にインデントを付ける。Pythonの言語等ではインデントが重要で、正しく記述しないと正常に動作しない。
インデントの幅(スペースの数)は色々と議論があるが、2スペース幅か4スペース幅である事が多い様だ。Arduino IDEのデフォルトは2スペースになっている。筆者は5スペースが見易く好み。インデントの幅を増やしたい場合、preference.txtでスペース数を設定出来る。

環境設定を開き、ウィンドウ下にあるpreference.txtをクリックするとpreference.txtファイルが格納されているフォルダーが開くので、preference.txtを開く。
Arduino IDEが起動している状態でpreference.txtを編集してもArduino IDE終了時に上書きされ編集内容が消えてしまうので、preference.txtはArduino IDEを終了した状態で編集する。


preference.txtにあるeditor.tabs.sizeと言う項目がインデントのスペース数となっているので、好きな数字に編集する。

Arduino IDEでインデントをスペースではなくタブのまま使いたい場合、editor.tabs.expandの値をfalseにしておく事で、タブがタブのまま扱われ、スペースに変換されずにすむ。

これで編集中はタブ幅を好みに設定できるが、ツールの自動整形を実行した場合2スペース幅になってしまう。
自動整形で使われるタブ幅を変更したい場合、Arduino IDEのインストールフォルダーの下のlibフォルダーにあるformatter.confファイルをpreference.txtがあるフォルダーにコピー後、メモ帳等で編集する。

Indent=spaces=2の数字を変える事で、自動整形時のタブ幅を変更できる。

自動整形の設定は
http://astyle.sourceforge.net/astyle.html
の情報を元に好みに設定可能。自動整形時、インデントをスペースではなくタブで行いたい場合、indent=spacesをコメントアウトし、indent=tabに設定する事で、タブで自動整形される。
