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

### Automatic Indent

The auto indent feature inserts the appropriate number of tabs or spaces at the beginning of a line by looking at program structure.

In the default configuration, pressing ENTER will create a new line with the appropriate amount of indent automatically, and pressing TAB at the beginning of, or inside the leading whitespace of a line will insert the appropriate amount of indentation. Pressing it again will insert a tab character.

The behavior of the ENTER and TAB keys can be configured in the Shortcuts pane of the Utilities>Global 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.

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.

- Indent Selected Lines.

See for details.

Auto indent behavior 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.

Edit>Indent>Indent Selected Lines (shortcut: C+i) indents all selected lines, or the current line if there is no selection.

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.

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