Code faster with Intellij IDEA live templates

简介: Intellij IDEA – in my opinion the most productive IDE for Java – comes with bunch of features helping getting rid of writing repetitive code – which i...

Intellij IDEA – in my opinion the most productive IDE for Java – comes with bunch of features helping getting rid of writing repetitive code – which is a everyday business no matter what language you use. One of them is live templates.

Live templates contain predefined code fragments. You can use them to insert frequently-used or custom code constructs into your source code file quickly, efficiently, and accurately.

IDEA comes with a set of live templates for Java, Maven, HTML, CSS and more and also enables to create custom templates.

Inserting live template

There are 2 ways to insert live template:

  1. Type live template abbreviation (for example dep) and next press TAB key
  2. Use shortcut Command + J (Mac) / Ctrl + J (PC) to popup list of available live templates in current context

At the beginning it’s quite difficult to remember all you wish to use and going back and forth from code to settings or browsing list is not very efficient – especially when you don’t know what you are looking for. That’s why I created gallery of (in my opinion) most useful built-in live templates for Java and Maven that can be used as a cheat sheet:

Maven

  • dep – Inserts <dependency/>

  • pl – Inserts <plugin/>

  • repo – Inserts <repo/>

Iterations

  • fori – creates iteration loop

  • itar – iterates elements of array

  • itco – iterates elements of java.util.Collection

  • iter – iterates Iterable

  • itit – iterates java.util.Iterator

  • itli – iterates elements of java.util.List

Other

  • ifn – Inserts “if null” statement

  • inn – Inserts “if not null” statement

  • inst – Checks object type with instanceof and down-casts it

  • lazy – Performs lazy initialization

Plain

  • psf – public static final

  • psfi – public static final int

  • psfs – public static final String

  • thr – throw new

Summary

Using predefined Java templates is just a beginning. You can find ready to use templates for particular frameworks on Github and I really encourage you to create your custom, project specific ones.

If you created interesting common use live templates for Java or related frameworks feel invited to post them in comments.

相关文章
|
XML 搜索推荐 JavaScript
Easy Code,IntelliJ IDEA中代码一键生成
Easy Code,IntelliJ IDEA中代码一键生成
729 0
Easy Code,IntelliJ IDEA中代码一键生成
解决IDEA提交代码时提示“Performing Code Analysis...”
解决IDEA提交代码时提示“Performing Code Analysis...”
1728 0
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
1145 1
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
C++ Windows
Windows、IDEA、VS Code常用快捷键
Windows、IDEA、VS Code常用快捷键
633 0
|
Java Linux 开发工具
IDEA中git提交前如何关闭code analysis以及开启格式化代码
【10月更文挑战第12天】本文介绍了在 IntelliJ IDEA 中关闭代码分析和开启代码格式化的步骤。关闭代码分析可通过取消默认启用检查或针对特定规则进行调整实现,同时可通过设置 VCS 静默模式在提交时跳过检查。开启代码格式化则需在 `Settings` 中配置 `Code Style` 规则,并通过创建 Git 钩子实现提交前自动格式化。
6720 3
|
前端开发 JavaScript 数据安全/隐私保护
idea代码review工具Code Review Helper使用介绍
CodeReview IDEA 插件是一款用于代码审查的工具,旨在解决在GitLab中查看整体业务逻辑的不便。该插件提供快速添加注释、行号旁的评审意见标记、双击跳转到代码、意见删除和修改、内容导出为Excel以及导入等功能。特别地,它支持离线和在线模式,离线模式下,审核者和开发者通过Excel文件交换评审意见;在线模式则通过服务端实现评审内容的上传和下载,简化文件传输。此外,该插件允许定制评审字段,并能与团队协作工具集成。通过这些特性,CodeReview IDEA 提高了代码审查的效率和便捷性。
2567 2
|
SQL 监控 JavaScript
用了这么久 IDEA,竟然不知道 Live Templates ?
Live Templates 是什么,听上去感觉挺玄乎的。有的同学用过之后觉得简直太好用了,不能说大大提高了开发效率吧,至少也是小小的提高一下,节省了很多敲重复代码的时间。有的同学用过之后说:没什么用,奇技淫巧罢了。
|
IDE Java 开发工具
Idea 启动报错 failed to create jvmjvm path url或failed to create jvmerror code -1 jvm path
Idea 启动报错 failed to create jvmjvm path url或failed to create jvmerror code -1 jvm path
927 0
IDEA的Duplicated code fragment (14 lines long)提示如何关闭
IDEA的Duplicated code fragment (14 lines long)提示如何关闭
2564 0
IDEA的Duplicated code fragment (14 lines long)提示如何关闭

热门文章

最新文章