Tabbing and Indentation

A tab is a special character that moves the caret to the next tab stop. Many programming languages rely on tabs to indent lines of source depending on how deeply they are nested in the code. jEdit has several features that make tabs and indentation easier to deal width.

Changing the Tab Width

Many believe that the One True Tab Size is 8, others think that all code should be indented with a tab width of 4, and the GNU Project refuses to accept code with a tab width of anything but 2.

There are three ways to change the tab size:

Soft Tabs

Sometimes, it is preferable to use multiple spaces instead of "real" tabs to indent code. The main advantage of doing so is that the file will look the same if viewed with a different default tab size. jEdit supports this with its "Soft Tabs" feature. If the Tab key is pressed with soft tabs enabled, the number of spaces necessary to move the caret to the next tab stop will be inserted, instead of a "real" tab.

There are three ways to use soft tabs:

Indentation-Related Commands

Edit>Lines>Shift Indent Left (keyboard equivalent: Alt-Left Arrow) and Edit>Lines>Shift Indent Right (keyboard equivalent: Alt-Right Arrow) will add or remove, respectively, one level of indent (a tab, or the number of spaces equivalent to a tab) to every line in the selection, or the current line if nothing is selected.

Edit>Selection>Spaces to Tabs will convert consecutive spaces to tabs in the selection. Edit>Selection>Tabs to Spaces will convert tabs to soft tabs (multiple spaces) in the selection.

Automatic Indent

Pressing Tab or Space at the start of every line to create the appropriate indent is very tedious, so jEdit has an auto indent feature.

Edit>Lines>Indent Selected Lines (keyboard equivalent: Control-I) will indent all selected lines, or the current line if there is no selection. If indent on enter is enabled, pressing Enter will create a new line with the appropriate amount of indent automatically. If indent on tab is enabled, pressing Tab on an unindented line will insert the appropriate amount of indentation. Pressing it again will insert an actual tab.

By default, indent on enter is enabled and indent on tab is disabled. There are three ways to specify when indentation should be performed:

Auto indent rules are mode-specific. In most edit modes, the indent of the previous line is simply copied over. However, in C-like languages (C, C++, Java, JavaScript), if the previous line contains an open curly bracket ("{"), an extra tab (or soft tab) is inserted. Similarly, when you type a closing curly bracket ("}"), a level of indentation is removed. Also, "if", "else", "while" and so on create an extra level of indent on the next line.

If you want to insert a literal tab or newline without performing indentation, prefix the tab or newline with Control-E Control-V. For example, to create a new line without any indentation, type Control-E Control-V Enter.