# Editing Source Code

## Edit Modes

An edit mode specifies syntax highlighting rules, auto indent behavior, and various other customizations for editing a certain file type. This section only covers using existing edit modes; information about writing your own can be found in .

When a file is opened, jEdit first checks the file name against a list of known patterns. For example, files whose names end with `.c` are opened with C mode, and files named `Makefile` are opened with Makefile mode. If a suitable match based on file name cannot be found, jEdit checks the first line of the file. For example, files whose first line is `#!/bin/sh` are opened with shell script mode.

### Mode Selection

File name and first line matching is done using glob patterns similar to those used in Unix shells. Glob patterns associated with edit modes can be changed in the Editing pane of the Utilities> Options dialog box. Note that the glob patterns must match the file name or first line exactly; so to match files whose first line contains `begin`, you must use a first line glob of `*begin*`. See for a description of glob pattern syntax.

The default edit mode for files which do not match any pattern can be set in the Editing pane as well.

The edit mode can be specified manually as well. The current buffer's edit mode can be set on a one-time basis in the Utilities>Buffer Options dialog box; see . To set a buffer's edit mode for future editing sessions, place the following in one of the first or last 10 lines of the buffer, where edit mode is the name of the desired edit mode:

    :mode=edit mode:

### Syntax Highlighting

Syntax highlighting is the display of programming language tokens using different fonts and colors. This makes code easier to follow and errors such as misplaced quotes easier to spot. All edit modes except for the plain text mode perform some kind of syntax highlighting.

The colors and styles used to highlight syntax tokens can be changed in the Syntax Highlighting pane of the Utilities> Options dialog box; see .

## 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:

- On a global or mode-specific basis in the Editing pane of the the Utilities> Options dialog box. See .

- In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See .

- 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 n is the desired tab width, and m is the desired indent width:

      :tabSize=n:indentSize=m:

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 .

- In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See .

- 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 .

- In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See .

- 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 )

#### 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 ), or when causing a hard wrap (see ).

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 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.

## Commenting Out Code

Most programming and markup languages support the notion of comments, or regions of code which are ignored by the compiler/interpreter. jEdit has commands which make inserting comments more convenient.

Comment strings are mode-specific, and some in some modes such as HTML different parts of a buffer can have different comment strings. For example, in HTML files, different comment strings are used for HTML text and inline JavaScript.

Edit>Source Code>Range Comment (shortcut: C+e C+c) encloses the selection with comment start and end strings, for example `/*` and `*/` in Java mode.

Edit>Source Code>Line Comment (shortcut: C+e C+k) inserts the line comment string, for example `//` in Java mode, at the start of each selected line.

Toggling Comments

You might be looking for the actions Toggle Line Comment or Toggle Range Comment. They are available from the TextTools plugin, not jEdit core.

## Bracket Matching

Misplaced and unmatched brackets are one of the most common syntax errors encountered when writing code. jEdit has several features to make brackets easier to deal with.

Positioning the caret immediately after a bracket will highlight the corresponding closing or opening bracket (assuming it is visible), and draw a scope indicator in the gutter. If the highlighted bracket is not visible, the text of the matching line will be shown in the status bar. If the matching line consists of only whitespace and the bracket itself, the previous line is shown instead. This feature is very useful when your code is indented as follows, with braces on their own lines:

    public void someMethod()
    {
        if(isOK)
        {
            doSomething();
        }
    }

Invoking Edit>Source>Go to Matching Bracket (shortcut: C+]) or clicking the scope indicator in the gutter moves the caret to the matching bracket.

Edit>Source>Select Code Block (shortcut: C+[) selects all text between the closest two brackets surrounding the caret.

Holding down Control while clicking the scope indicator in the gutter or a bracket in the text area will select all text between the two matching brackets.

Edit>Source>Go to Previous Bracket (shortcut: C+e C+[) moves the caret to the previous opening bracket.

Edit>Source>Go to Next Bracket (shortcut: C+e C+]) moves the caret to the next closing bracket.

Bracket highlighting in the text area and bracket scope display in the gutter can be customized in the Text Area and Gutter panes of the Utilities>Options dialog box; see .

jEdit's bracket matching algorithm only checks syntax tokens with the same type as the original bracket, so for example unmatched brackets inside string literals and comments will be skipped when matching brackets that are part of program syntax.

## Abbreviations

Abbreviations are invoked by typing a couple of letters and then issuing the Edit>Expand Abbreviation (keyboard shortcut: C+;), which takes the word before the caret as the abbreviation name. If that particular abbreviation was not yet set, a dialog will pop up, and you can enter the text to insert before and after the caret. After the abbreviation is created, it can be viewed or edited from the Abbreviations pane of the Utilities>Options dialog box; see .

Using abbreviations reduces the time spent typing long but commonly used strings. For example, in Java mode, the abbreviation sout is defined to expand to System.out.println(), so to insert System.out.println() in a Java buffer, you only need to type sout followed by C+;. An abbreviation can either be global, in which case it can be used in all edit modes, or specific to a single mode.

The Java, VHDL. XML and XSL edit modes include some pre-defined abbreviations you might find useful. Other modes do not have any abbreviations defined by default.

Automatic abbreviation expansion can be enabled in the Abbreviations pane of the Utilities>Options dialog box. If enabled, pressing the space bar after entering an abbreviation will automatically expand it.

If automatic expansion is enabled, a space can be inserted without expanding the word before the caret by pressing Control E V Space.

### Positional Parameters

Positional parameters are an advanced feature that make abbreviations much more useful. The best way to describe them is with an example.

Java mode defines an abbreviation F that is set to expand to the following:

    for(int $1 = 0; $1 < $2; $1++)

Expanding `F#j#array.length#` will insert the following text into the buffer:

    for(int j = 0; j < array.length; j++)

Expansions can contain up to nine positional parameters. Note that a trailing hash character (#) must be entered when expanding an abbreviation with parameters.

If you do not specify the correct number of positional parameters when expanding an abbreviation, any missing parameters will be blank in the expansion, and extra parameters will be ignored. A status bar message will be shown stating the required number of parameters.

## Folding

Program source code and other structured text files can be thought of as containing a hierarchy of sections, which themselves might contain sub-sections. The folding feature lets you selectively hide and show these sections, replacing hidden ones with a single line that serves as an overview of that section. Folding is disabled by default. To enable it, you must choose one of the available folding modes.

Indent mode creates folds based on a line's leading whitespace; the more leading whitespace a block of text has, the further down it is in the hierarchy. For example:

    This is a section
      This is a sub-section
      This is another sub-section
        This is a sub-sub-section
    Another top-level section

Explicit mode folds away blocks of text surrounded with {{{ and }}}. For example:

    {{{ The first line of a fold.
    When this fold is collapsed, only the above line will be visible.

    {{{ A sub-section.
    With text inside it.
    }}}

    {{{ Another sub-section.
    }}}

    }}}

Both modes have distinct advantages and disadvantages; indent folding requires no changes to be made to a buffer's text and does a decent job with most program source. Explicit folding requires fold markers to be inserted into the text, but is more flexible in exactly what to fold away.

Some plugins might add additional folding modes; see for information about plugins.

Folding can be enabled in one of several ways:

- On a global or mode-specific basis in the Editing pane of the Utilities> Options dialog box. See .

- In the current buffer for the duration of the editing session in the Utilities>Buffer Options dialog box. See .

- In the current buffer for future editing sessions by placing the following in the first or last 10 lines of a buffer, where mode is either indent, explicit, or the name of a plugin folding mode:

      :folding=mode:

When using indent folding, portions of the buffer may become inaccessible if you change the leading indent of the first line of a collapsed fold. If you experience this, you can use the Expand All Folds command to make the text visible again.

### Collapsing and Expanding Folds

The first line of each fold has a triangle drawn next to it in the gutter (see for more information about the gutter). The triangle points toward the line when the fold is collapsed, and downward when the fold is expanded. Clicking the triangle collapses and expands the fold. To expand all sub-folds as well, hold down the Shift while clicking.

The first line of a collapsed fold is drawn with a background color that depends on the fold level, and the number of lines in the fold is shown to the right of the line's text.

Folds can also be collapsed and expanded using menu item commands and keyboard shortcuts.

Folding>Collapse Fold (shortcut: A+BACK_SPACE) collapses the fold containing the caret.

Folding>Expand Fold One Level (shortcut: A+ENTER) expands the fold containing the caret. Nested folds will remain collapsed, and the caret will be positioned on the first nested fold (if any).

Folding>Expand Fold Fully (shortcut: AS+ENTER) expands the fold containing the caret, also expanding any nested folds.

Folding>Collapse All Folds (shortcut: C+e c) collapses all folds in the buffer.

Folding>Expand All Folds (shortcut: C+e x) expands all folds in the buffer.

### Navigating Around With Folds

Folding>Go to Parent Fold (shortcut: C+e u) moves the caret to the fold containing the one at the caret position.

Folding>Go to Previous Fold (shortcut: A+UP) moves the caret to the fold immediately before the caret position.

Folding>Go to Next Fold (shortcut: A+DOWN) moves the caret to the fold immediately after the caret position.

### Miscellaneous Folding Commands

Folding>Add Explicit Fold (shortcut: C+e a) surrounds the selection with {{{ and }}}. If the current buffer's edit mode defines comment strings (see ) the explicit fold markers will automatically be commented out as well.

Folding>Select Fold (shortcut: C+e s) selects all lines within the fold containing the caret. Control-clicking a fold expansion triangle in the gutter has the same effect.

Folding>Expand Folds With Level (shortcut: C+e ENTER key) reads the next character entered at the keyboard, and expands folds in the buffer with a fold level less than that specified, while collapsing all others.

Sometimes it is desirable to have files open with folds initially collapsed. This can be configured as follows:

- On a global or mode-specific basis in the Editing pane of the Utilities> Options dialog box. See .

- In the current buffer for future editing sessions by placing the following in the first or last 10 lines of a buffer, where level is the desired fold level:

      :collapseFolds=level:

### Narrowing

The narrowing feature temporarily narrows the display of a buffer to a specified region. Text outside the region is not shown, but is still present in the buffer.

Holding down Alt while clicking a fold expansion triangle in the gutter will hide all lines the buffer except those contained in the clicked fold.

Folding>Narrow Buffer to Fold (shortcut: C+e n n) hides all lines the buffer except those in the fold containing the caret.

Folding>Narrow Buffer to Selection (shortcut: C+e n s) hides all lines the buffer except those in the selection.

Folding>Expand All Folds (shortcut: C+e x) shows lines that were hidden as a result of narrowing.