ReSharper 2016.3 Help

Code Formatting Rules

ReSharper | Edit | Reformat Code
Ctrl+Alt+L
ReSharper_ReformatCode

An important aspect of code style is how to format the code, i.e. how to use whitespaces and blank lines to arrange and separate code blocks, whether and how to use tabs for indents, whether and how to wrap long lines, etc.

The extensive set of ReSharper code formatting rules has a default configuration that takes into account default Visual Studio formatting options as well as numerous best practices.

You can configure every detail of formatting rules and enforce the rules in your code. These rules are applied when ReSharper produces new code with code completion and code generation features, code templates and refactorings. The formatting rules can be also applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.

In this topic:

Reformatting code

When you type code in the editor, ReSharper reformats expressions as soon as you type a semicolon and reformats blocks of code as soon as you type a closing brace. If necessary, you can disable these auto-formatting options on the Environment | Editor | Editor Behavior page of ReSharper options.

To reformat existing code

  1. Select the scope where you want to reformat code:
    • Make a selection in the editor to reformat code in the selection.
    • Set the caret anywhere in the file to reformat code in the file.
    • Select one or more items in the Solution Explorer to reformat code in files under these items and their child items.
  2. Press Ctrl+Alt+L or choose ReSharper | Edit | Reformat Code in the main menu .

You can reformat code in the current selection with the corresponding context action.

To reformat code in the current selection

  1. In the editor, select a block of code that you want to reformat.
  2. Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
  3. Select /help/img/dotnet/2016.3/ThemedIcon.CodeCleanupOptionPage.Screen.[Gray].png Format selection in the action list.
    Reformatting currently selected code

Alternatively, you can use code cleanup to reformat code in any scope. Code cleanup may be helpful if you want to combine reformatting code with applying other code styles.

To reformat code with Code Cleanup

  1. Select the scope where you want to reformat code:
    • Make a selection in the editor to reformat code in the selection.
    • Set the caret anywhere in the file to reformat code in the file.
    • Select one or more items in the Solution Explorer to reformat code in files under these items and their child items.
  2. Do one of the following:
    • Press Ctrl+Alt+F or choose ReSharper | Edit | Cleanup Code in the main menu .
    • Right-click anywhere in the text editor or right-click the selection and choose Cleanup Code in the context menu.
  3. In the Code Cleanup dialog that opens, select the Default: Reformat Code profile in the Available Profiles area.
  4. Click Run. ReSharper will reformat code in the selected scope according to your formatting preferences.

If you want to reformat code without opening the Code Cleanup dialog, you can bind the default Reformat Code profile to the silent cleanup and run it simply by pressing Ctrl+Shift+Alt+F. You can also create a custom cleanup profile that would combine reformatting code with other code style tasks.

Configuring code formatting rules

Formatting rules can be configured to a very detailed level. For example, you can define whether whitespaces should be placed around a specific operator or whether to indent nested using statements.

Your code formatting preferences are saved using the mechanism of shared settings. Among other things, this mechanism allows you to maintain different preferences for different solutions as well as to keep these preferences under a VCS and automatically share them with your team members.

To configure code formatting rules

  1. Choose ReSharper | Options in the main menu.
  2. Use the Code Editing | General Formatting Style page to check and configure the options that are applied in all languages.
  3. Use the Code Editing | [Language] | Formatting Style pages to configure language-specific formatting preferences. On these pages, you can use the preview area in the bottom to view how exactly ReSharper applies the specific preference to the code.
  4. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see Managing and Sharing ReSharper Settings.

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts Protobuf JSON
/help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg /help/img/dotnet/2016.3/check.svg

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

See Also

Last modified: 23 January 2017