Indentation

From Notepad++ Wiki
Jump to: navigation, search
Indentation


Proper indentation of code is a requirement in any sizable project and enhances code readability at an usually low cost.

Notepad++ has limited abilities for real-time line indentation. This is done by design in Scintilla, so as not to create any surprise nor delay to typists.However, it provides, through various plugins, ways to reformat the code after it was written.

The NppAutoIndent plugin adds more real-time capabilities.

How to indent?

Two concurrent methods exist:

  • Use hard tab characters, and then tell the editor how it should expand tabs visually;
  • Use a constant number of spaces.

Switching from one to another, as well as specifying the expansion width for a tab character, is done from the Settings -> Preferences -> Language Menu/Tab settings -> Tab settings frame. The checkbox toggles hard tab indenting on or off. The clickable link with a value gives the exchange rate of a tab against spaces. This is either a visual width or an actual number of spaces. These values can be set on a per language basis - click the language name in the list above the frame.

This setting governs what is going to be typed, but not what was typed before. Replacing a tab (\t) by some fixed number of spaces or back is a plain Find and Replace operation in Extended mode.

Keeping the indent

Notepad++, when you press the Enter key, can create the exact indent of the current line, or do nothing about it and leave cursor at the first column instead. Whether the indent from the line above is being duplicated is controlled by the Auto-indent checkbox on the Settings -> Preferences -> MISC tab. Apart from this, Notepad++ does not interfere with your typing.

The indent guide can be hidden or shown using the View -> Show Symbol -> Show indent guide toggle. It can be mapped to a key combination on the shortcut mapper.

Changing the indent

Manual indent changes

If one or more lines are selected, hitting the Tab key will increase the indent level one step, while hitting Shift-Tab will decrease it one step. Tab and Shift-Tab can be remapped at will using the Shortcut mapper: the bindings to look for are the SCI_TAB and SCI_BACKTAB listed under Scintilla commands.

Automated changes

The following helper functions are provided in the TextFX Edit submenu:

Indent and surround { text lines }
Just like it says. This is handy to turn contiguous lines of code into a block of code, in C/Java-like languages. And no one will prevent you from performing a Find/Replace to change { and } into something more useful, like begin and end in Pascal-like languages.
Leading space to tabs or tabs to spaces
Switches between tabs and spaces leading the selected lines, using the current exchange rate.
Leading space to tabs or tabs to spaces width=8
As above, but with a fixed exchange rate of 8 overriding the current one. This has/had been a common coding standard.
Indent text sticky left margin
Recomputes how lines should be indented by using standard rules, and leaving preprocessor directives alone - some compilers insist that they start at column 1 -.
Reindent C++ code
Uses a smarter algorithm:
  1. Indents for /* stream comments */ are not changed.
  2. goto labels and #preprocessor statements are always at the beginning of the line
  3. switch..case statements are indented in a special way that allows them to follow the bracket count exactly. I've always manually written them this way and was very glad when the easiest numeric technique was the one I had been using from the beginning.

You may wish to experiment in order to find which will suit some piece of code best. Use the Undo button as often as needed.

The NppAutoIndent plugin

It is better to turn it off if you use Notepad++ Auto-indent internal feature.

The plugin can either

  • be turned off
  • emulate Notepad++ internal auto-indent through the Previous line option
  • use standard rules through the Code block option
  • Perform more checks when using the Smart indent mode

The above options are mutually exclusive.

In addition, the plugin may be told to apply across all programming languages (using the Ignore language menu entry), or confine its attention to C/C++ files.

Overhang indent

This particular sort of indent applies to wrapped portions of long lines when word wrap mode is on. There is no overhang indent when word wrap mode is off.

By default, overhang indent is 0, so that wrapped pats start at column 1. The SCI_SETWRAPSTARTINDENT Scintilla message takes the indent amount (in spaces) as first parameter and 0 as the second.

To send this message in a non programmatic way, you can define the following macro:

<Macro name="some_name" Ctrl="yes" Alt="yes" Shift="no" Key="100">
   <Action type="0" message="2464" wParam=the_indent_in_chars lParam="0" sParam=""/>
</Macro>

and add it to Shortcuts.xml inside the <Macros>...</Macros> node. The key assignment may not be what you want (Ctrl+Alt+Numpad4), you can change it from the shortcut mapper. There doesn't seem to be an easier way to this setting.