Tabbing and Indentation

jEdit makes a distinction between the tab width, which is is used when displaying hard tab characters, and the indent width, which is used when a level of indent is to be added or removed, for example by mode-specific auto indent routines. Both can be changed in one of several ways:

Edit>Indent>Shift Indent Left (shortcut: S+TAB or A+LEFT) removes one level of indent from each selected line, or the current line if there is no selection.

Edit>Indent>Shift Indent Right (shortcut: A+RIGHT) adds one level of indent to each selected line, or the current line if there is no selection. Pressing Tab while a multi-line selection is active has the same effect.

Edit>Indent>Remove Trailing Whitespace (shortcut: C+e r) removes all whitespace from the end of each selected line, or the current line if there is no selection.

Soft Tabs

Files containing hard tab characters may look less than ideal if the default tab size is changed, so some people prefer using multiple space characters instead of hard tabs to indent code.

This feature is known as soft tabs. Soft tabs can be enabled or disabled in one of several ways:

  • On a global or mode-specific basis in the Editing pane of the Utilities> Options dialog box. See the section called “The Editing Pane”.

  • In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See the section called “The Buffer Options Dialog Box”.

  • In the current buffer for future editing sessions by placing the following in one of the first or last 10 lines of the buffer, where flag is either true or false:

    :noTabs=flag:

Changing the soft tabs setting has no effect on existing tab characters; it only affects subsequently-inserted tabs.

Edit>Indent>Spaces to Tabs converts soft tabs to hard tabs in the current selection, or the entire buffer if nothing is selected.

Edit>Indent>Tabs to Spaces converts hard tabs to soft tabs in the current selection, or the entire buffer if nothing is selected.

Elastic Tabstops

Elastic tabstops are an alternative way to handle tabstops. Elastic tabstops differ from traditional fixed tabstops because columns in lines above and below the "cell" that is being changed are always kept aligned. As the width of text before a tab character changes, the tabstops on adjacent lines are also changed to fit the widest piece of text in that column. It provides certain explicit benefits like it saves time spent on arranging the code and works seemlessly with variable width fonts.But at the same time it can make the code look unorganized on editors that do not support elastic tabstops.

This feature is known as elastic tabstops. Elastic tabstops can be enabled or disabled in one of several ways:

  • On a global or mode-specific basis in the Editing pane of the Utilities>Options dialog box. See the section called “The Editing Pane”.

  • In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See the section called “The Buffer Options Dialog Box”.

  • In the current buffer for future editing sessions by placing the following in one of the first or last 10 lines of the buffer, where flag is either true or false:

    :elasticTabstops=flag:

Note that this feature does not work with soft tabs. where tabs are emulated as spaces

Automatic Indent

The auto indent feature inserts the appropriate number of tabs or spaces at the beginning of a line. There are three different indentation schemes to choose from: full, simple, and none. The scheme can be chosen on a global or per-edit mode basis using the Editing pane of the Utilities>Options dialog. It can also be changed for a specific buffer using the Buffer Options dialog, or with a buffer-local property. (see the section called “Buffer-Local Properties”)

Automatic Indent Scheme: full

In this default scheme, the amount of indentation inserted is 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), curly brackets and language statements are taken into account and indent is added and removed as necessary.

The automatic indentation can be triggered by: pressing ENTER (this will by default only affect the indentation of the new line), pressing TAB at the beginning of, or inside the leading whitespace of a line, entering one the bracket characters defined in the edit mode, pressing one of the electricKeys for the current edit mode (more details in the section called “The PROPS Tag”), or when causing a hard wrap (see the section called “Wrapping Long Lines”).

No matter what automatic indentation scheme is currently active, Edit > Indent > Indent Selected Lines (shortcut: C+i) indents all selected lines, or the current line if there is no selection, as if in the full scheme.

Electric keys

Electric keys cause reapplying of the indentation rules to the current line. Thanks to the electric keys the following code fragments are indented properly on-line:

  • Java, C: brackets. If indenting brackets are defined for the language, they are implicitly considered electric keys. Thus a closing bracket is placed in its correct position immediately after being typed.

  • Java, C: labels. Labels end with a colon and the colon is included in electric keys for these languages. With pressing the colon, the line is reindented and the labels are indented a level to the left.

  • Basic: endif. Here f letter is an electric key, that makes the line indented to the left.

In jEdit 4 electric keys worked unconditionally. As of jEdit 5 they trigger reindentation only if the indentation of the line, before pressing a key, is the same as jEdit would indent it using its rules. This allows for specifying more electric keys in mode files, because they don't cause unwanted indentation like they did before. Electric keys including all letters seem to be good solution for basic-like languages.

Automatic Indent Scheme: simple

In this simplified automatic-indentation scheme, only two actions trigger an indentation: pressing ENTER, or causing a hard wrap. Only the new line will be indented, and the amount of indentation will be the same as the previously line.

Automatic Indent Scheme: none

In this automatic indentation scheme, no actions in the text area will trigger a reindentation, and all lines start completely unindented.

Further customization of automatic indentation

The behavior of the ENTER and TAB keys can be configured in the Shortcuts pane of the Utilities>Options dialog. box, just as with any other key. The ENTER key can be bound to one of the following, or indeed any other command or macro:

  • Insert Newline.

  • Insert Newline and Indent, which is the default. This is equivalent to Insert Newline when using the indentation scheme none.

The TAB can be bound to one of the following, or again, any other command or macro:

  • Insert Tab.

  • Insert Tab or Indent, which is the default. This is equivalent to Insert Tab when not using the full automatic indentation scheme.

  • Indent Selected Lines. This binding will not respect the selected auto indentation scheme.

See the section called “The Shortcuts Pane” for details.

To insert a literal tab or newline without performing indentation, prefix the tab or newline with C+e v. For example, to create a new line without any indentation, type C+e v ENTER.