Перейти к содержимому

Scratch file pycharm что это

  • автор:

Scratch files

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 and buffers are not related to a specific project. They are available for any project that you open with the specific IDE instance.

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

Scratch files and buffers are stored in the IDE configuration directory under scratches .

Create a scratch file

  1. Go to File | New | Scratch File or press Control+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.

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 . WebStorm 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, WebStorm will create the file with the same type and extension as the original file.

WebStorm numbers scratch files sequentially starting from 1. If you close a tab with an empty scratch file, WebStorm deletes it.

Create a scratch buffer

Find the New Scratch Buffer action

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

View available scratches

  • To see the list of created scratch files and buffers, open the Project tool window and select Scratches and Consoles | Scratches : Scratches in the Project window
  • By default, scratch files and buffers are sorted alphabetically by their names and next by their extensions (types), which automatically means that buffer files always come first. To sort scratch files by types, click or right-click the Project tab and select Sort by Type : The Sort by Type actionWebStorm sorts scratches alphabetically based on their extensions : Scratches sorted by type

Rename scratches

  1. In the Project tool window, click Scratches under Scratches and Consoles , select the file to rename, and press Shift+F6 .
  2. In the dialog that opens, type the new name. Note that specifying another extension does not affect the file type, use the Change Language action for that.

For more information, refer to Renaming files and folders.

Copy scratches

  1. In the Project tool window, in the Scratches and Consoles | Scratches directory, select the scratch file to copy.
  2. Press F5 . In the dialog that opens, type the new filename. Here you can also specify another extension to change the file type. If necessary, select the folder where you want to save the copy.

For more information, refer to Copying files and folders.

Change the language of scratches

  1. In the Project tool window, click Scratches under Scratches and Consoles , and choose Change Language () from the context menu of the scratch file to change the language for.
  2. Select the desired language. Note the following:
    • Four last used items are on top of the list, above the separator.
    • To narrow down the list, start typing the language name.
    • The Change Language action keeps extension in sync, if it exists.

You can assign a shortcut to the Change Language action as described in Configuring keyboard shortcuts.

Run and debug scratches

  1. Install the required packages and add the corresponding import statements to the scratch file. For example, to use JQuery:
    1. Open package.json and add the following packages to the dependencies property:

    «dependencies»: < "jquery": "^3.5.0", "jsdom": "^16.2.2", "core-js": "^3.6.5", "nuxt": "^2.14.5" >
    const = require(«jsdom»); const = new JSDOM(«»); const $ = require(‘jquery’)(window);

    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. For more information, refer to Moving files and folders.
    • 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 Control+X , then select the target directory in your project and press Control+V .

    View documentation for scratches

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

    Scratch files

    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 and buffers are not related to a specific project. They are available for any project that you open with the specific IDE instance.

    • 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 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 Control+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.

    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 . PyCharm 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, PyCharm will create the file with the same type and extension as the original file.

    PyCharm numbers scratch files sequentially starting from 1. If you close a tab with an empty scratch file, PyCharm deletes it.

    Create a scratch buffer

    The New Scratch Buffer action

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

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

    PyCharm 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 PyCharm reaches that limit, it will recreate buffer1.txt and suggest clearing the content for it. If you want to make sure PyCharm 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 . Scratches in the Project windowConsoles are used to compose and execute SQL statements for databases defined in PyCharm as data sources. This functionality is available only when the Database Tools and SQL plugin is enabled. For more information, refer to Query consoles.
    • Use the Find Action popup Control+Shift+A to invoke the Show Scratch Files action and view all available scratch files in a popup: The Scratch Files popup

    Location of scratch files and buffers

    By default, PyCharm 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.

    Scratch files

    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 and buffers are not related to a specific project. They are available for any project that you open with the specific IDE instance.

    • 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 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 Control+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.

    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 . PhpStorm 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, PhpStorm will create the file with the same type and extension as the original file.

    PhpStorm numbers scratch files sequentially starting from 1. If you close a tab with an empty scratch file, PhpStorm deletes it.

    Create a scratch buffer

    The New Scratch Buffer action

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

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

    PhpStorm 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 PhpStorm reaches that limit, it will recreate buffer1.txt and suggest clearing the content for it. If you want to make sure PhpStorm 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 . Scratches in the Project windowConsoles are used to compose and execute SQL statements for databases defined in PhpStorm as data sources. This functionality is available only when the Database Tools and SQL plugin is enabled. For more information, refer to Query consoles.
    • Use the Find Action popup Control+Shift+A to invoke the Show Scratch Files action and view all available scratch files in a popup: The Scratch Files popup

    Running scratch files in multiple PHP versions

    PhpStorm integrates with 3v4l.org, which allows you to run code from a PHP scratch file using multiple PHP versions simultaneously or switch between versions on the fly. Under the hood, PhpStorm uploads the code snippet from the scratch file to the 3v4l.org service and brings the output back to the IDE. No additional steps to install or configure the service are required.

    Run PHP code on 3v4l.org from the IDE

    1. Open the code snippet in the scratch file editor by either of the following:
      • From the Project view, expand Scratches and Consoles | Scratches and open an existing scratch file.
      • Create a new scratch file from a project file. To do so, select the code snippet in the editor, press Alt+Enter , and then select Create new scratch file from selection .

    The editor window for PHP scratch files comes with an 3v4l.org toolbar at the top.

    3v4l.org toolbar

  2. Select the specific PHP version from the list on the toolbar (or leave the default All versions option) and click . When prompted, confirm if you want to upload content of the scratch file to 3v4l.org and make it public. 3v4l.org runs your code (refer to the 3v4l.org link on the toolbar), and a preview window with the execution result is displayed in the editor. 3v4l.org editor and preview windowsTo toggle between the editor and the preview, click , , or on the toolbar.

Location of scratch files and buffers

By default, PhpStorm 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.

Scratch Files and Scratch Buffers

In this blog, we’re going to take a look at both scratch files and scratch buffers and identify their key uses.

This blog post covers the same material as the video with some additional tips and tricks. This provides an easy way for people to skim the content quickly if they prefer reading to watching, and to give the reader/watcher code samples and links to additional information.

What Are Scratch Files and Scratch Buffers?

Scratch files and buffers are temporary files that are helpful for a range of uses. Scratch files are fully functional, runnable and debuggable files. They’re useful for drafting up code or running code in isolation. Alternatively, scratch buffers are plain text files. They’re useful for making quick notes, a todo list or frequently used strings.

We can view our scratch files and buffers in the Project Window in the Scratches folder under Scratches and Consoles.

Scratches and Consoles View

Scratch files and buffers are available across your projects in IntelliJ IDEA. If you create them in one project, they will appear in your other projects in the same location (provided you’re using the same version of IntelliJ IDEA). Scratch files and buffers are temporary files but they will persist when you restart IntelliJ IDEA even if you invalidate your caches. However, they will be removed if you restore your default settings or reinstall IntelliJ IDEA.

Tip: Scratch files and buffers are stored in the IDE configuration directory under scratches. They are available from any IDE and project that uses this configuration directory.

Scratch Files

Scratch files can be of various types including Java, Kotlin, HTML, etc. Scratch files are all IDE aware with syntax highlighting, code completion and all other features for the corresponding files type. We can create a scratch file by using Ctrl+Alt+Shift+Insert on Windows/Linux or ⌘⇧N on macOS. We can start typing in the file type that we’re looking for, for example Kotlin, and then create our code using the normal IDE functionality we’d expect, including running it.

Kotlin Scratch File

We can also change the language of a scratch file, for example, we can change a text file to HTML by right-clicking on it and selecting Change Language. Then, start typing in HTML to filter the list and once we press return, IntelliJ IDEA will render the contents correctly.

Scratch files can be moved to a project by dragging and dropping it into our project or pressing F6 to refactor the file. Once a scratch file is part of a specific project, it will no longer be visible in the scratch files directory in the project, or any other.

We recommend you rename your scratch files to something that’s more meaningful for you.

Tip: To run an SQL scratch file, you need to have a connection to a data source. In IntelliJ IDEA, you need to attach a scratch file to the query console.

Scratch Buffers

We can create a scratch buffer by using Ctrl+Shift+A on Windows/Linux or ⇧⌘A on macOS then typing in scratch and selecting New Scratch Buffer. Scratch buffers in IntelliJ IDEA use a default naming convention. The first scratch buffer that you create is called buffer1.txt. The second will be called buffer2.txt and so forth up to buffer5.txt. This is the maximum number of scratch buffers that we can have with the default name. If we try and create more, IntelliJ IDEA will prompt us and overwrite the contents of the first scratch buffer. We can rename our scratch buffers to something more meaningful and then it won’t count towards our allowance of 5. For example, we can rename our buffer2.text to database-connection.txt.

Scratch Buffer

If we create another scratch buffer this time around, we get a blank buffer2.txt because we renamed the previous one.

We can also change the language of scratch buffers to benefit from the IDE-aware functionality. For example, we can type some Groovy code into our buffer2.txt file but we won’t get any IDE help because it’s initially a text file. We can fix that by right-clicking on buffer2.txt and selecting Change Language. When we select Groovy and click Return, IntelliJ IDEA will render the file accordingly.

Tip: You can create a shortcut for the New Scratch Buffer action as described in Configuring keyboard shortcuts.

Tips for Use

Both scratch files and buffers are treated in a similar way to other files in IntelliJ IDEA. We can use Ctrl+Shift+E on Windows/Linux or ⇧⌘E on macOS to see them in our Recent Locations, and we can filter this dialog by changed areas too.

Recently Changed Locations

Even though scratch files and buffers are not in source control, IntelliJ IDEA still tracks their changes locally. You can view this in the local history. However, they are temporary files so please treat them as such:

Check anything important into version control if you want to keep hold of it!

Summary

Scratch files and scratch buffers help you to reduce clutter in your main project while keeping your focus on your current task. Scratch files are fully functional and are ideal for code snippets while scratch buffers are plain text files that are ideal for quick notes.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *