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.
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 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.
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.
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.
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.
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!
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.
Subscribe to Blog updates
Thanks, we've got you!
String Templates in Java – why should you care?
TLDR; The existing String concatenation options are difficult to work with and could be error prone. String Templates (a preview feature introduced in Java 21) greatly improves how we create strings in Java by merging constant strings with variable values. The name of this feature pretty much says …
Java Annotated Monthly – November 2023
Welcome to the Java Annotated Monthly – November 2023! As the Java world keeps on spinning, we've gathered a treasure trove of articles, talks, and insights to warm your Java developers’ hearts. This month's edition features a delightful blend of the latest technological advancements and the ever…
Java Annotated Monthly – October 2023
Welcome to the October edition of Java Annotated Monthly! In this issue, we're plunging into the world of Java and tech. We'll explore the latest and greatest in the recent release of Java 21, share exciting news about the new EAP for IntelliJ IDEA 2023.3, and bring you a handpicked selection of …