ReSharper Help

Code Formatting Rules

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.

ReSharper allows you to configure an extensive set of formatting rules and enforce them 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:

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.

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

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.

Reformatting existing code

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 Format | Selection icon_reformat in the action list.

Alternatively, you can use code cleanup to reformat code in any scope.

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 these items and their child items.
  2. Do one of the following:
    • Choose ReSharper | Tools | Cleanup Code on the main menu.
    • Press Ctrl+Alt+F.
    • 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 box that opens, select the Default: Reformat Code 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 box, 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.

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_availablefeature_availablefeature_availablefeature_availablefeature_availablefeature_availablefeature_availablefeature_availablefeature_availablefeature_available

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: 21 September 2015