Programming
How do I create a new class in IntelliJ without using the mouse
In the fast-paced world of software development, efficiency is paramount. Every second saved can translate into significant productivity gains, and for many developers, the mouse can often be an unexpected bottleneck. Learning to navigate your Integrated Development Environment (IDE) using only the keyboard is a powerful skill that can dramatically speed up your workflow. IntelliJ IDEA, a popular choice among Java developers and polyglot programmers alike, offers an extensive suite of keyboard shortcuts designed to keep your hands on the keys. This guide will walk you through the essential steps to master how do I create a new class in IntelliJ without using the mouse, transforming your coding experience from a series of clicks into a fluid, keyboard-driven symphony.
Mastering IntelliJ Navigation with Keyboard Shortcuts
The foundation of a mouse-free workflow in IntelliJ is a solid understanding of its powerful keyboard shortcuts. These shortcuts not only accelerate common tasks but also foster a deeper connection with your codebase, allowing you to move and manipulate files with precision and speed. Developers who commit to learning these shortcuts often report a significant boost in their daily output, making the initial learning curve well worth the investment.
Before diving into class creation, let’s briefly touch upon fundamental navigation. To open the Project tool window, which displays your project structure, simply press Alt+1 (Windows/Linux) or Cmd+1 (macOS). Once open, you can navigate through folders and files using the arrow keys. To expand or collapse directories, use the right and left arrow keys respectively. This immediate access to your project structure is crucial for quickly locating the desired package or directory where your new class will reside. Understanding these core navigation principles sets the stage for more advanced keyboard-only operations.
For instance, switching between open editor tabs is effortlessly done with Alt+Left/Right Arrow (Windows/Linux) or Cmd+Shift+[ / ] (macOS). If you need to jump directly to a specific file by name, Ctrl+Shift+N (Windows/Linux) or Cmd+Shift+O (macOS) opens the “Go to File” dialog, allowing you to type the file name and hit Enter to open it. These quick navigation techniques are indispensable for maintaining flow and reducing reliance on the mouse for basic file management and viewing, creating a seamless development experience. For a comprehensive list of shortcuts, JetBrains provides an excellent IntelliJ IDEA Keymap Reference.
The Core Process: Creating a New Class (Keyboard-Only)
Creating a new class without touching your mouse in IntelliJ IDEA is a straightforward process once you know the right keyboard commands. This method ensures consistency and efficiency, especially when you need to create multiple files or work within a specific package structure. The key is to leverage IntelliJ’s context-sensitive menus and quick-action shortcuts.
To create a new class in IntelliJ without using the mouse, first ensure your Project tool window is active (Alt+1 or Cmd+1). Navigate using the arrow keys to the specific package or directory where you want to add the new class. Once the target package is highlighted, press Alt+Insert (Windows/Linux) or Cmd+N (macOS). This action brings up the ‘New’ context menu, which typically appears when you right-click with a mouse. From this menu, use your arrow keys to select ‘Java Class’ (or ‘Kotlin Class’, ‘Scala Class’, etc., depending on your project type and installed plugins) and press Enter. A dialog will then appear prompting you to enter the name for your new class. Type the desired class name, for example, OrderService, and press Enter again. Your new class file will be created and opened in the editor, ready for you to start coding.
- Activate Project Tool Window: Press
Alt+1(Windows/Linux) orCmd+1(macOS) to bring focus to the Project tool window. - Navigate to Target Package: Use the arrow keys (up/down/left/right) to navigate through your project structure and highlight the specific package or folder where you want to create the new class.
- Open ‘New’ Menu: With the desired package highlighted, press
Alt+Insert(Windows/Linux) orCmd+N(macOS). This opens the ‘New’ context menu. - Select ‘Java Class’: Use the down arrow key to navigate through the options in the ‘New’ menu until ‘Java Class’ is highlighted. Press Enter.
- Name Your Class: A dialog box will appear. Type the desired name for your class (e.g.,
PaymentProcessor). - Confirm Creation: Press Enter. The new class file will be created within the selected package and automatically opened in the editor for immediate coding.
This sequence of keyboard commands is incredibly efficient. It bypasses the need to move your hand to the mouse, click, navigate a menu, and then click again. Instead, it transforms the entire operation into a fluid series of key presses, keeping your focus on the keyboard and your code. For complex projects, this efficiency compounds over time, making it a valuable skill for any developer.
Beyond Basic Creation: Enhancing Productivity
Once you’ve mastered creating new classes using only the keyboard, IntelliJ’s power truly shines when you integrate other productivity-enhancing shortcuts into your workflow. These features help you not just create, but also maintain, refactor, and generate code, all without lifting your fingers from the keyboard. Adopting these techniques elevates your coding from a series of individual actions to a seamless, integrated process.
One of the most powerful features is context-aware code generation. After creating a new class, you might immediately need to generate a constructor, getters, setters, or override methods. Instead of typing these out manually, you can use Alt+Insert (Windows/Linux) or Cmd+N (macOS) while your cursor is inside the class body. This brings up a menu of generation options. For example, selecting ‘Constructor’ allows you to choose which fields to include, and IntelliJ automatically generates the code. Similarly, choosing ‘Getters and Setters’ provides a quick way to encapsulate your class fields. This significantly reduces boilerplate code and ensures consistency across your project, a cornerstone of clean code practices emphasized by industry leaders like Robert C. Martin in his book “Clean Code.”
Refactoring is another area where keyboard shortcuts provide immense value. Renaming a class, method, or variable is a frequent task. Instead of manually changing every instance, place your cursor on the element you wish to rename and press Shift+F6. IntelliJ intelligently identifies all usages and allows you to rename them globally. This is part of IntelliJ’s robust suite of refactoring tools, which also includes extracting Question & Answer :
Is there a way to create a new class in a desired location without using the mouse in IntelliJ?
I understand there is no keyboard binding in the default keymap.
If you are already in the Project View, press Alt+Insert (New) | Class. Project View can be activated via Alt+1.
To create a new class in the same directory as the current one use Ctrl+Alt+Insert (New…).
You can also do it from the Navigation Bar, press Alt+Home, then choose package with arrow keys, then press Alt+Insert.
Another useful shortcut is View | Select In (Alt+F1), Project (1), then Alt+Insert to create a class near the existing one or use arrow keys to navigate through the packages.
And yet another way is to just type the class name in the existing code where you want to use it, IDEA will highlight it in red as it doesn’t exist yet, then press Alt+Enter for the Intention Actions pop-up, choose Create Class.