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
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.
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:
- Press down the Ctrl key and simultaneously the s key. Release both keys.
- You will get the following prompt at the bottom of the window: "I-Search: "
- type the tab key.
- If there is a tab character in the file, the screen will repaint with the cursor
moved to the first tab character.
- If there is no tab character in the file, then following message will appear
at the bottom of the screen: "Failing I-search: ".
Changing Existing Tabs
This is the recommended procedure for removing tabs from a file.
- Select the full file by setting the mark at the top of the file
and moving the cursor to the bottom of the file.
- 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.
If you are using vim,
- To change all existing tab characters into spaces so as to match the current tab stop
settings, use the 'retab' command. The default tab stops are very 8 columns,
but that can be changed by setting the 'tabstop' option to something other than 8.
- Set the 'expandtab' (abbreviated to 'et') option to convert each tab automatically as
you type it into a number of spaces. You can set this option in your ~/.vimrc file.)
Once this option is set, use Ctrl-V if you want to enter a real tab character.
This file last modified Thursday, 26-Jan-2012 17:51:14 CST