Scratch files | IntelliJ IDEA (2024)

Sometimes you may need to create temporary notes or draft up some code outside the project context. Instead of switching to a different application, you can use scratch files and scratch buffers.

  • Scratch files are fully functional, runnable, and debuggable files that support syntax highlighting, code completion, and all other features for the corresponding file type. For example, while working on one project, you may come up with an idea for a method that you could later use in another project. You can create a scratch file with a draft of the method, which is not stored in your project directory but can be opened when you're working on any other project. You can use scratch files to draft Java code constructs, HTTP requests, JSON documents, and so on.

  • Scratch buffers are simple text files without any coding assistance features. Scratch buffers can be used for simple task lists and notes to yourself. They are also not stored in the project directory and can be opened from any other project. You can create up to five scratch buffers with default names, which are rotated and reused by clearing the content.

Create a scratch file

  1. Go to File | New | Scratch File or press Ctrl+Alt+Shift+Insert. Alternatively, in the Project tool window , right-click any area and navigate to New | Scratch File.

  2. Select the language of the scratch file.

    Scratch files of the same type are automatically numbered and added to the Scratches and Consoles directory of the Project view.

    When you create a Java scratch file, IntelliJIDEA automatically adds a class declaration and a main() method. You can run it the same way you would run an ordinary Java file: either click Scratch files | IntelliJIDEA (1) in the gutter or press Shift+F10. For more information, refer to Java Scratch run configuration.

Alternatively, you can create a new scratch file with the contents of the current selection in the editor. Select some text or code, press Alt+Enter and then select Create new scratch file from selection. IntelliJIDEA will attempt to detect the language of the selected fragment and use the appropriate type and extension. If it can't detect the language of the selection, IntelliJIDEA will create the file with the same type and extension as the original file.

Create a scratch buffer

  • There is no dedicated menu item for the action to create a new scratch buffer, but you can use the Find Action popup Ctrl+Shift+A and run the New Scratch Buffer action.

Scratch files | IntelliJIDEA (2)

You can also add a shortcut for the New Scratch Buffer action as described in Configure keyboard shortcuts.

IntelliJIDEA creates a text file named buffer1.txt.

The next scratch buffer you create is named buffer2.txt, and so on up to buffer5.txt. When IntelliJIDEA reaches that limit, it will recreate buffer1.txt and suggest clearing the content for it. If you want to make sure IntelliJIDEA does not clear the scratch buffer after you have five, you can rename it.

View all scratch files and buffers

  • Open the Project view and expand Scratches and Consoles | Scratches.

    Scratch files | IntelliJIDEA (3)
  • Use the Find Action popup Ctrl+Shift+A to invoke the Show Scratch Files action and view all available scratch files in a popup:

    Scratch files | IntelliJIDEA (4)

Use dependencies in a scratch file

  1. Add a dependency to a module.

  2. In the Project tool window, right-click the scratch file and select More Run/Debug | Modify Run Configuration.

  3. From the Use classpath of module list, select the module with the required dependency.

  4. Apply the changes and close the dialog.

Location of scratch files and buffers

By default, IntelliJIDEA stores scratch files and buffers in the IDE configuration directory under scratches. They are available from any IDE and project that uses this configuration directory.

Syntax

%APPDATA%\JetBrains\<product><version>

Example

C:\Users\JohnS\AppData\Roaming\JetBrains\IntelliJIdea2024.1

Syntax

~/Library/Application Support/JetBrains/<product><version>

Example

~/Library/Application Support/JetBrains/IntelliJIdea2024.1

Syntax

~/.config/JetBrains/<product><version>

Example

~/.config/JetBrains/IntelliJIdea2024.1

To change the location of the Scratches and Consoles directory, use the idea.scratch.path platform property. To change the location of just the Scratches directory, use the idea.scratch.path/scratches platform property. For more information, refer to Advanced configuration.

Include a scratch file into your project

If a scratch file grows into something that you want to use in your project, move it into the desired directory of your project structure.

  • Open a scratch file in the editor or select it under the Scratches and Consoles | Scratches directory in the Project view, press F6, and select the target directory in your project.

  • Drag a scratch file from the Scratches and Consoles | Scratches directory in the Project view to the target directory in your project.

  • Select a scratch file in the Scratches and Consoles | Scratches directory in the Project view and press Ctrl+X, then select the target directory in your project and press Ctrl+V.

Change the language of a scratch file or buffer

  1. In the Project tool window, open the Scratches and Consoles | Scratches.

  2. Right-click a scratch file or buffer and then click Change Language.

  3. Select the desired language. You can start typing the name of the language to narrow down the list.

View documentation for scratches

  • Select any scratch file or buffer in the Project tool window and press Ctrl+Q to view quick documentation with the location, type, size, creation and modification date of the file.

Last modified: 26 May 2024

Language injectionsJava Scratch run configuration

Scratch files | IntelliJ IDEA (2024)
Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5951

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.