PyCharm 2024.3 Help

Copyright

Your project may contain files that you want to protect by copyright. Instead of adding a copyright notice to each file separately, you can use copyright profiles.

A profile defines copyright notice text and a set of files (a scope) in which this text will be incorporated.

You may need several different copyright notices for files within the same project. In this case, you can configure several profiles and associate them with different scopes.

In PyCharm, copyright profiles can be local and shared.

Shared profiles are stored in the .idea/copyright in .xml files and are available to other members of your team through VCS.

Local copyright profiles are stored on the IDE level and are available in all projects that you open in the current IDE instance.

A profile allows you to define a copyright text that you can later insert across the project or add to specific files only.

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright | Copyright Profiles.

    To configure the default profile for all newly created projects, select File | New Projects Setup | Settings for New Projects and go to Editor | Copyright | Copyright Profiles.

  2. Click Add and name the new profile.

  3. Select Share through VCS to place this profile in the .idea folder and share it with your team or deselect the checkbox to keep this profile locally and make it available in all projects.

  4. Enter the copyright notice text.

    You can enter plain text or configure a Velocity template. For the template, use variables, and click Validate to make sure that it is configured correctly.

    Configuring a copyright profile

Once the profile is configured, you can select the scope of files to which you want to add the text or set this profile as the default profile for all files in the project that are not included in any scope.

Copyright variables

Currently, the following variables are available in the Velocity context:

Name

Type

Comment

$today

DateInfo

The current date and time.

$file.fileName

String

The name of the currently opened file where the notice is to be generated.

$file.pathName

String

The complete path and name of the currently opened file where the notice is to be generated.

$file.qualifiedClassName

String

The fully qualified name of the currently opened file where the notice is to be generated.

$file.lastModified

DateInfo

The date and time when the current file was last changed.

$project.name

String

The name of the current project.

$username

String

The name of the current user.

DateInfo has the following properties:

year

int

The current year.

month

int

The current month (1-12).

day

int

The current date of month (1-31).

hour

int

The current hour (0-11).

hour24

int

The current hour (0-23).

minute

int

The current minute of the hour (0-59).

second

int

The current second of the minute (0-59).

DateInfo has the following method:

format(String format)

String

Date and time formats that are specified by date and time pattern strings.

By default, the copyright template contains two years: the original year that and the current year.

If you insert a new copyright notice, the original year is going to match the current year, so only one year is going to be added (for example, 2021). The next year, when you update your copyright, you will be able to regenerate the notice, so that the original year is going to appear alongside the current year (2021 – 2022). This way you don't have to add the year manually every time you update the copyright.

In the template, the original year is pulled with the $originalComment.match("Copyright \(c\) (\d+)", 1, "-") code, and the current year is inserted with $today.year.

Copyright template in settings

Note that the year is going to update correctly if the format of the new copyright matches the format of the previous copyright in the template. So, make sure to edit the part of the template that pulls the year from files so that it matches the format of the original copyright.

For example, if your original notice was Copyright 2019-2021 MongoDB, Inc., remove the copyright sign (c) from the template: $originalComment.match("Copyright (\d+)", 1, "-").

Assign a profile to a scope of files

Select the scope of files to which you want to add the configured copyright text:

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright.

  2. Click Add (or press Alt+Insert), and select an existing shared scope from the list.

    You can define a new scope if necessary. Click the Select Scopes to add scopes or modify existing ones link in the lower part of the page.

  3. From the Copyright list, select the profile that you want to link with the scope.

  4. Apply the changes and close the dialog.

    Associating a profile with a scope

After that, you can add the copyright to the necessary files.

Set the default copyright profile

The settings of the default profile will be applied to files that are not explicitly included in any scope covered by a profile.

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright.

  2. From the Default project copyright list, select the profile you want to use as the default profile.

  3. Apply the changes and close the dialog.

    After that, you can add the copyright to the necessary files.

By default, the IDE pastes a block comment before other comments with a prefix in each line and adds a blank line after the block.

You can change the default format for all files or for each file type separately:

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright | Formatting.

  2. On this page, you can configure the formatting for all types of files.

    To change the formatting for a specific file type, select it under the Formatting node.

  3. Configure the formatting options. Use the preview section to make sure that the new formatting looks as intended.

    Copyright notice in the default format

If you want to use a profile in another project, copy the .xml file with the profile settings to a different location and then import it:

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright | Copyright Profiles.

  2. Click Import Profile and in the dialog that opens, select the .xml file containing the profile that you want to use.

  3. Change the name of the profile if necessary and click OK.

    If the settings are imported successfully, you will see a confirmation popup.

    Popup confirming that copyright profile is imported
  • To insert the text in a single file, open it in the editor, press Alt+Insert, and select Copyright from the popup.

  • To insert the text into a group of files, right-click a node in the Project tool window, and select Update Copyright. You will be prompted to select in which scope you want to update the notice.

    A node may include files that belong to different scopes. In this case, copyright notices will be generated according to the assigned profiles.

    If a node contains a file that doesn't belong to any scope, the IDE will assign the default profile to it.

  1. Press Ctrl+Alt+S to open settings and then select Editor | Copyright | Copyright Profiles.

  2. Select the necessary profile and edit the copyright text.

  3. In the Regex to detect copyright in comments field, specify a regular expression that will be used to find existing copyright notices in files.

    The IDE will use this expression to identify the copyright comment among other comments. For example, such comments normally contain the word Copyright.

  4. In the Allow replacing copyright if old copyright matches field, specify a regular expression that the IDE will use to search for the files in which the copyright text should be updated.

    For example, if you want to update the copyright text in all files that contain years 2000-2006 and 2010-2016 in their copyright, specify 20[0-1][0-6].

  5. Apply the changes and close the dialog.

  6. In the Project (Alt+1) tool window, right-click the node for which you want to update the text, and select Update Copyright.

You can configure the IDE to update copyright notices every time the IDE saves your changes.

  1. Press Ctrl+Alt+S to open settings and then select Tools | Actions on Save.

  2. Select the Update copyright notice checkbox.

    The checkbox is available if you have configured at least one copyright profile and specified a scope of files to which this profile is applied.

    If you haven't configured a profile yet, click the Configure link.

    When the profile is configured, return to Tools | Actions on Save and enable the Update copyright notice checkbox.

  3. Apply the changes and close the dialog.

For more information about actions that you can enable on save, refer to Trigger actions when saving changes.

Last modified: 11 October 2024