这是IDEA快捷键拆解系列的第十八篇。
本文将介绍一下IDEA强大的Live Template功能。
首先,我们要知道Live Template是在哪里定义的,先按Ctrl + Shift + S
进入设置,接着在输入框输入“Live Template”就可以定位到了,如下图所示。
Live Template
从上图可以看到,IDEA官方已经帮我们定义好了一些常用的Live Template,而且针对不同的文件类型也划分了不同的Live Template Group。查看Live Template的快捷键是Ctrl + J
,这里列几个比较常用的。
//1.定义一个数组 String[] strings = new String[]; //2.输入itar后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键快速进行光标切换 for (int i = 0; i < strings.length; i++) { String string = strings[i]; }
String[] strings = new String[]; //输入ritar后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键进行光标快速切换 for (int i = strings.length - 1; i >= 0; i--) { String string = strings[i]; }
List<String> stringList = new ArrayList<>(); //输入iter后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键进行光标快速切换 for (String s : stringList) { }
//输入fori后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键进行光标快速切换 for (int i = 0; i < ; i++) { }
List<String> stringList = new ArrayList<>(); //输入itli后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键进行光标快速切换 for (int i = 0; i < stringList.size(); i++) { String s = stringList.get(i); }
List<String> stringList = new ArrayList<>(); //输入itco后会有提示,按`Enter`确认后会自动输出以下内容,默认是按`Tab`键进行光标快速切换 for (Iterator<String> iterator = stringList.iterator(); iterator.hasNext(); ) { String next = iterator.next(); }
当然了,Live Templat强大的地方不仅在于官方定义好的这部分,更重要是还支持自定义的Live Template。在学习工作中,我们可以尝试把一些重复的代码抽出来变成模板,接下来我们就来看看如何自定义Live Template。
配置自定义Live Template
修改作用范围
修改作用范围
Enter
键选择后光标将停留在VAR1的位置,默认按Tab
键即可快速切换到VAR2的位置。测试Live Template
测试Live Template
由于个人疏忽,所以截图部分Live Template存在部分错误,下面是更正后的Live Template。
/**
* $VAR1$
*/
private String $VAR2$;
$END$