Mu2e Home
What to do About Tabs?
Search
Mu2e@Work

I would like to propose the following as a standard practice for Mu2e source code files.

Please configure your editor(s) so that they do not write tab characters to source code files. Please do configure your editor so that, when you hit the tab key, it inserts the appropriate number of spaces to achieve the desired formatting.

The reason for this request is that different editors, or different configurations of the same editor, render tab characters differently. To be clear we are discussing what the editor does when it encounters a tab character in the text of the file; we are not (yet) discussing what the editor does when you hit the tab key.

Most editors can be configured to interpret a tab character in the file text to mean "move the cursor to the right to the next tab stop", where tab stops might be defined differently from one editor to another or even among various configurations for the same editor. Different printers also have different interpretations of where the tab stops are. If we are not careful about the usage of tabs, then code that displays in a nicely lined up format in my editor may not display nicely in your editor and may not print nicely on some printers; similarly, code that lines up nicely in your editor may not line up nicely in my editor.

What happens when you press the tab key? Most editors can be configured that hitting the tab either inserts a tab character into the text or it inserts an appropriate number of spaces. When editing C++ source code, most editors are aware of the structure of the language and have the ability to do context sensitive indentation. If the line is already properly indented, the editor will do nothing.

This does not address the issue of indentation policy, how many spaces to indent the body of an if block, and so on. I think we can let authors decide on the details of their own policy, 2 spaces, 4 spaces, whatever. But please do choose such a policy and implement your policy in a way that does not put tab characters in the files.

As I learn how to do so, I will add the information for other editors.


Instructions for emacs users

If you are using emacs , add the followieng line to the end of your .emacs file

(setq-default indent-tabs-mode nil)
If you are using xemacs, add the same line to your ~/.xemacs/init.el file.

Checking if a File Has Tabs

There are two ways, using emacs, to check if a file contains tab characters. You can pull down the "Edit" menu from the menu bar near the top of the window; select "Find ..." this will pop up a dialog box. Move the mouse to the input box, press the tab key and then click on "Find next". If there are tab characters in the file, this will repaint the screen and move the cursor to the first tab character. If there are no tab characters in the file the message "Search failed" will appear in the box at the bottom of the screen.

You can also do this from command mode:

  1. ctrl-s
  2. type the tab key.

Changing Existing Tabs

This is the recommended procedure for removing tabs from a file.
  1. Select the full file by setting the mark at the top of the file and moving the cursor to the bottom of the file.
  2. ESC-x untabify
    Here ESC-x means to hit the escape key, followed by the letter x. This will open a mini-buffer at the bottom of the window into which you type the command untabify. This command will remove tabs from the selected region and replace them with the appropriate number of spaces.

Inserting a Tab Character on Purpose

If you really want to insert a tab character into a file, then quote it using ctrl-q; that is, simultaneously hold down ctrl key and the q key, release both and type the tab key. You should rarely need to do this. In C++ mode, emacs will usually accept a tab as a tab if it is typed within a character string; moreover one can always use the sequence \t to insert a tab into a string; this is preferred to typing an actual tab character.


Instructions for vim Users

If you are using vim,


Fermilab at Work ]  [ Mu2e Home ]  [ Mu2e @ Work ]  [ Mu2e DocDB ]  [ Mu2e Search ]

For web related questions: Mu2eWebMaster@fnal.gov.
For content related questions: kutschke@fnal.gov
This file last modified Thursday, 26-Jan-2012 17:51:14 CST
Security, Privacy, Legal Fermi National Accelerator Laboratory