Что такое scratch file в pycharm
Перейти к содержимому

Что такое scratch file в pycharm

  • автор:

What is a Scratch File?

enter image description here

I am currently trying to figure out all the functions that my IDE has, and I have come across a predefined folder when I create a project that is called ‘scratches and consoles’. The IDE only allows me to create Scratch Files but I don´t know what that kind of file is and what Is its purpose. Could someone give me a hand with this?

asked Sep 21, 2020 at 15:43
Luis Orbaiceta Luis Orbaiceta
453 4 4 silver badges 15 15 bronze badges

1 Answer 1

It’s something like temporary notes with ability to highlight/format your code.

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

You can read more about it at JetBrains — Scratch files.

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 файлы в IDE JetBrains

Scratch-файлы — это временные файлы для экспериментов или прототипирования в редакторе без создания файлов внутри проекта.

Ctrl + Alt + Shift + Insert — открыть панель выбора шаблона/языка для создания скретч-файла.

Scratch-файл храниться до его закрытия.

Создается и открывается редакторе временный файл в каталоге:

ls -la ~/.config/JetBrains/PhpStorm2022.1/scratches/

Вы можете открыть scratch-каталог во встроенном терминале: Ctrl + Shift + A » Open in Terminal.

И запустить выполнение файла. Или запустить встроенный в PHP веб-сервер:

php -S --port 8001

#ide #phpstorm #scratch files

Can I change the directory for scratch files in PyCharm?

The default directory is %HOMEPATH%\.\config\scratches .
It’s not so easy to use.
So can I change it to other directories as C:\scratches ? If so, I can open a REPL and easily add ‘c:\scratches’ to the sys.path. Then I can load modules in scratches.

asked Oct 24, 2019 at 3:04
31 3 3 bronze badges
Why not just create a shortcut to %HOMEPATH%\config\scratches and put it on your desktop?
Oct 24, 2019 at 7:28
If I want to import a module from scratch folder in REPL, it’s still inconvenient.
Oct 29, 2019 at 3:05
Perhaps it’d help to update your question with your use case(s).
Oct 29, 2019 at 9:26
You are right, I should make the question more specific.
Oct 31, 2019 at 0:51

2 Answers 2

Just necroing this in case anyone stumbles upon it from searching. But, I guess you can now change the location to save scratch files from the default.

In PyCharm, click the «help» menu and then click «Edit Custom Properties«

This will open an idea.properties file with the first line shown below as a comment:

custom PyCharm properties (expand/override 'bin\idea.properties') 

Now just add the following code to this file and of course, change to whatever folder you want. And yes, the slashes should be forward slashes as shown:

idea.scratch.path/scratches=C:/Users/Johnny/Documents/MyCodeFiles/PyCharm/MyScratchesFolder 

Now just close out of the properties file (no saving needed) and then restart PyCharm. Your scratches folder should now be showing the new location on the left. And you can also copy/paste your previous scratch files from the default location to your new folder. Hope that helps!

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

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