Boost Java Productivity with Postfix Completion, Live Templates & Hidden IDE Shortcuts
This guide explores how to dramatically improve Java development efficiency in IntelliJ IDEA by mastering Postfix Completion, customizing Live and File Templates, and leveraging a collection of low‑frequency yet powerful shortcuts, providing step‑by‑step instructions, code examples, and advanced techniques for both beginners and seasoned developers.
Background
Effective use of an IDE is crucial for developers, as it directly impacts coding speed and quality. This article aims to systematically summarize IDE shortcuts and techniques, helping both newcomers and experienced engineers boost productivity.
Purpose and Positioning
The article aggregates scattered tips from various blogs into a unified knowledge system, categorizing them for easy reference and progressive learning.
Universality
Although illustrated with JetBrains IntelliJ IDEA, most techniques apply to other JetBrains IDEs and even Android Studio, because they share the same core.
Postfix Completion
Introduction
Postfix Completion lets you append a template key (e.g., .var) after an already‑typed expression to generate common code patterns.
Example and Efficiency Gains
Consider a null‑check for name:
if (name != null) {
// ...
}Without Postfix, typing this in a plain editor requires 23 keystrokes. In IDEA, using Postfix reduces it to 8 keystrokes, automatically formatting the code.
Common Postfix Templates
var : Define a local variable with type inference.
notnull : Add a null‑check guard.
nn : Shortcut for notnull.
try : Wrap the current statement in a try‑catch block.
cast : Perform a type cast with inferred type.
if : Generate an if statement.
throw : Throw an exception.
for : Iterate over a collection or array.
fori : Iterate with an index.
sout / soutv : Print a string or variable.
return : Return a value from a method.
format : Apply String.format syntax.
Advanced Usage – Custom Postfix
You can create custom postfixes via Editor → General → Postfix Completion → + → Java . For example, a custom isempty postfix checks collection emptiness. After configuration, typing list.isempty expands to the desired guard code.
Live Template
Introduction
Live Templates are code snippets triggered by a template key, without needing a preceding expression.
Basic Templates
psfs: Insert a string constant. main: Generate a public static void main method. sout: Print to console.
Unlike Postfix, Live Templates are invoked solely by the key.
Advanced Usage – Groovy Script Integration
Live Templates can execute Groovy scripts, enabling complex actions such as cross‑device code sharing. A minimal Flask server provides /push and /pull endpoints. Two Groovy scripts implement push and pull templates that send/receive code via HTTP, allowing one IDE instance to push code and another to pull it directly into the editor.
from flask import Flask, request
DEFAULT = 'nothing'
code = DEFAULT
app = Flask(__name__)
@app.route('/push')
def push():
global code
code = request.args.get('code', DEFAULT)
return 'Success'
@app.route('/pull')
def pull():
return code
app.run() def url = new URL('http://127.0.0.1:5000/pull')
def conn = url.openConnection() as HttpURLConnection
def result = conn.inputStream.text
return result def code = _1
def url = new URL('http://127.0.0.1:5000/push?code=' + new URLEncoder().encode(code))
def conn = url.openConnection() as HttpURLConnection
def result = conn.inputStream.text
return resultThis demonstrates how IDE templates can become programmable tools.
File Template
Introduction
File Templates generate entire files (e.g., class headers) based on predefined patterns.
Custom File Header
Configure the header format in IDEA settings; new files automatically include the header.
Generic Controller Template
Using Velocity syntax, a template abstracts common CRUD controller code. By supplying the domain name ( ${Subject}), the template produces a fully functional Spring REST controller for any entity.
#set($SubjectOfLowerFirst = ${Subject.substring(0,1).toLowerCase()} + $Subject.substring(1))
package ${PACKAGE_NAME};
import com.alibaba.ide.code.entity.Result;
import com.alibaba.ide.code.entity.${Subject};
import com.alibaba.ide.code.service.Condition;
import com.alibaba.ide.code.service.${Subject}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
#parse("File Header.java")
@RestController
@RequestMapping("api/${SubjectOfLowerFirst}")
public class ${Subject}Controller {
@Resource
private ${Subject}Service ${SubjectOfLowerFirst}Service;
@PostMapping
public Result<${Subject}> create(@RequestBody ${Subject} record) {
${Subject} ${SubjectOfLowerFirst} = ${SubjectOfLowerFirst}Service.insert(record);
return Result.success(${SubjectOfLowerFirst});
}
// ... other CRUD methods omitted for brevity
}Applying the template with Goods instantly generates a complete controller identical in structure to the UserController example.
Low‑Frequency High‑Efficiency Shortcuts
Select next occurrence : Control + G Batch selection : Option + drag mouse Move line up/down : Option + Shift + ↑/↓ Duplicate line/block : Command + D Expand/Collapse code : Command + . or Command + Shift + +/- Change method signature : Command + F6 Show clipboard history : Command + Shift + V Extract variable : Command + Option + V Extract field : Command + Option + F Extract constant : Command + Option + C Extract method parameter : Command + Option + P Extract method :
Command + Option + MDebugging Trick
Using conditional breakpoints, you can modify variable values at runtime without restarting the application. This is especially useful for rapid UI tweaks in Android development or for adjusting server‑side logic on the fly.
Conclusion
By mastering Postfix Completion, Live and File Templates, and a curated set of low‑frequency shortcuts, developers can transform repetitive coding tasks into swift, automated actions, freeing more time for solving core business problems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
