sublime对应语言模板使用技巧
# 前言
sublime 作为一个比较便捷的文本工具,颜值非常高,编写基本工具时候如果不需要打开IDE,可以将这个工具作为第一个选择 !!!
不要跟我说 devCPP,vscode, editplus...
这部分内容是纪录sublime
工具破解以及编写对应语言自定义编译命令
# 使用步骤
基本步骤
- tools->build system->new build system-> xxx.sublime-build (根据自己定义命令,后面会用到)-> save->
- 使用 tools->build system->找到刚刚保存的选择
# 模板
# Java
{
// Windows
// "shell_cmd": "javac -d ${file_path}/../bin/ -sourcepath ${file} ",
"shell_cmd": "javac ${file} -d bin -encoding utf-8 && cd bin && java ${file_base_name} ",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "GBK",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only java files can use this sublime-build file
"selector": "source.java",
"variants": [
{
// build and run in Terminus (sublime plugin)
// [Recommanded]''
"name": "Run In Terminus",
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "start cmd /c IF NOT EXIST bin MKDIR bin && javac -d bin ${file} && cd bin && start cmd /K java ${file_base_name}"
},
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "start cmd /c IF NOT EXIST bin MKDIR bin && javac -d bin ${file} && cd bin && start cmd /K java ${file_base_name}"
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "javac -d bin ${file} && cd bin && java ${file_base_name} "
},
{
// 仅仅测试输入
"name": "Run in Sublime and input",
"shell_cmd": "javac -d bin ${file} && cd bin && java ${file_base_name} < \"../in/${file_base_name}/in.txt\""
},
{
// 输入 输出 以及期望结果 使用 GIT 比较输入输出文件
"name": "Run in Sublime and input and output use Git ",
"shell_cmd": "javac -d bin ${file} && cd bin && java ${file_base_name} < \"../in/${file_base_name}/in.txt\" > \"../in/${file_base_name}/out.txt\" && cmd /c git diff \"../in/${file_base_name}/temp.txt\" \"../in/${file_base_name}/out.txt\" "
},
{
// build and run in sublime
// 输入 输出 以及期望结果 使用 CMD 命令 比较输入输出文件
"name": "Run in Sublime and input and output ",
"shell_cmd": "javac -d bin ${file} && cd bin && java ${file_base_name} < \"../in/${file_base_name}/in.txt\" > \"../in/${file_base_name}/out.txt\" && cmd /c comp /L /M \"${file_path}/../in/${file_base_name}/temp.txt\" \"${file_path}/../in/${file_base_name}/out.txt\" > nul && echo AC || echo NO"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
文件结构
project_dir 项目目录
- bin 存放可执行.class 文件
- Code01.class 自动生成的.class文件
- in 存放输入流
- Code01 按照文件名分类目录
- in.txt 输入文件 如果需要
- out.txt 输出文件 如果需要
- temp.txt 期望结果 如果需要 最后会同 temp.txt 和 out.txt 比较
Code00.java 模板文件 通过执行这个文件自动创建模板
Code01.java 源文件 注意这个文件同 bin 、 in 目录同级
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
注意 如果使用输入输出 但是 没有安装 git 请使用最后一条命令 不然报错……
当然配套的也可以自定义了,使用我下面这个模板可以快速创建类coding了……
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
public class Code00 {
private static String root_dir = null;
private static final File file = new File("");
private static final String compileDir = "bin";
static {
String path = file.getAbsolutePath();
if(path.contains(compileDir)){
path = path.replace(compileDir,"");
}
root_dir = path;
checkCode00();
}
public static void main(String[] args) {
int cnt = countJavaFile();
String fileName ="Code"+(cnt < 10 ? ("0" + cnt):String.valueOf(cnt)) + ".java";
String javaFilePath = root_dir + fileName;
String template = content(fileName);
createInputAndOutputFile(javaFilePath);
readContent(template,javaFilePath);
}
public static int countJavaFile(){
int ans = 0;
File f = new File(root_dir);
File[] fs = f.listFiles();
for(File temp : fs ){
if(temp.getAbsolutePath().endsWith(".java")){
ans++;
}
}
return ans;
}
public static void readContent(String content,String javaFilePath){
try{
File file = new File(javaFilePath);
if(!file.exists()){
file.createNewFile();
}
BufferedWriter w = new BufferedWriter(new FileWriter(file));
w.write(content,0,content.length());
w.flush();
w.close();
System.out.println(javaFilePath + " create success !");
}catch(Exception e){
e.printStackTrace();
}
}
public static void printMethodInfo(Class<?> c){
Method[] mts = c.getDeclaredMethods();
for(Method m : mts){
System.out.println("\n"+m+"\n");
}
}
public static String content(String className){
if(className == null || className.length() == 0){
throw new NullPointerException("className is null");
}
if(className.endsWith(".java")){
className = className.replace(".java","");
}
StringBuilder sb = new StringBuilder();
sb.append("import java.util.*; \n");
sb.append("import java.io.*; \n\n");
sb.append("public class ").append(className).append(" {");
sb.append("\n public static void main(String[] args) {\n\n");
sb.append(" ");
sb.append(className);
sb.append(" code = new ");
sb.append(className);
sb.append("(); \n\n\n");
sb.append(" }\n");
sb.append("}");
return sb.toString();
}
public static void createInputAndOutputFile(String javaFilePath) {
try{
File file = new File(javaFilePath);
String fileName = file.getName().replace(".java","");
String path = root_dir + File.separator + "in" + File.separator + fileName + File.separator;
createNewFile(path + "in.txt");
createNewFile(path + "out.txt");
createNewFile(path + "temp.txt");
}catch(Exception e) {
// ignore
}
}
public static void createNewFile(String path) {
try{
File f = new File(path);
File p = new File(f.getParent());
if(!p.exists()) {
p.mkdirs();
}
if(!f.exists()){
f.createNewFile();
}
}catch(Exception e) {
// ignore
}
}
// check code00
public static void checkCode00() {
try{
String path = root_dir + "in" + File.separator + Code00.class.getSimpleName() + File.separator;
createNewFile(path + "in.txt");
createNewFile(path + "out.txt");
createNewFile(path + "temp.txt");
}catch(Exception e) {
// ignore
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
因为 Java 的输入输出同 C 、C++ 之类 的输入比较麻烦 为此简单封装了一个模板
IO 输入输出流模板可以根据自己喜好封装一个也行……
import java.util.*;
import java.io.*;
public class Code {
public static void main(String[] args) {
Code code = new Code();
IO io = new IO();
int a = io.readInt();
int b = io.readInt();
io.println(a * b);
io.close();
}
// 这个为输入输出模板
// 你可以根据自己喜好封装一个自己想要
public static class IO {
BufferedReader in;
PrintWriter out;
public IO() {
this(System.in,System.out);
}
public IO(InputStream in,PrintStream out) {
try{
this.in = new BufferedReader(new InputStreamReader(in));
this.out = new PrintWriter(new OutputStreamWriter(out));
}catch(Exception e) {
e.printStackTrace();
}
}
public String read() {
try{
return in.readLine();
}catch (Exception e) {
return "";
}
}
public int readInt() {
return Integer.parseInt(read());
}
public char readChar(){
return read().charAt(0);
}
public boolean readBoolean() {
return Boolean.parseBoolean(read());
}
public long readLong() {
return Long.parseLong(read());
}
public double readDouble() {
return Double.parseDouble(read());
}
public float readFloat() {
return Float.parseFloat(read());
}
public void println(Object obj){
out.println(obj);
}
public void print(Object obj){
out.print(obj);
}
public void close() {
try{
if(in != null){
in.close();
in = null;
}
if(out != null ) {
out.flush();
out.close();
out = null;
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# C++
{
// Windows
// build only
"shell_cmd": "g++ -std=c++11 -g -Wall ${file} -o ${file_path}/../bin/${file_base_name} && ${file_path}/../bin/${file_base_name} ",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only C and Cpp files can use this sublime-build file
"selector": "source.c, source.c++",
"variants": [
{
// build and run in Terminus (sublime plugin)
// [Recommanded]
"name": "Run In Terminus",
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && start cmd /c \"\"${file_path}/../bin/${file_base_name}\" & pause \""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime only input ",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime and input and output compare use git ",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\" > \"${file_path}/../in/${file_base_name}/out.txt\" && cmd /c git diff \"${file_path}/../in/${file_base_name}/temp.txt\" \"${file_path}/../in/${file_base_name}/out.txt\" "
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime and input and output ",
"shell_cmd": "g++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\" > \"${file_path}/../in/${file_base_name}/out.txt\" && cmd /c comp /L /M \"${file_path}/../in/${file_base_name}/temp.txt\" \"${file_path}/../in/${file_base_name}/out.txt\" > nul && echo AC || echo NO"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
注意目录结构
project_dir 项目目录
- bin 存放可执行文件
- a.exe 自动生成的 a.exe 文件
- in 存放输入流
- a 按照文件名分类目录
- in.txt 输入文件 如果需要
- out.txt 输出文件 如果需要
- temp.txt 期望结果 如果需要 最后会同 temp.txt 和 out.txt 比较
- project 存放源文件目录 注意 bin 、in 、 project 三个目录同级
- a.cpp 源代码
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# C
{
// Windows
// build only
"shell_cmd": "gcc -std=c99 -g -Wall ${file} -o ${file_path}/../bin/${file_base_name}",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only C and Cpp files can use this sublime-build file
"selector": "source.c",
"variants": [
{
// build and run in Terminus (sublime plugin)
// [Recommanded]
"name": "Run In Terminus",
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && start cmd /c \"\"${file_path}/../bin/${file_base_name}\" & pause \""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime only input ",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime and input and output compare use git ",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\" > \"${file_path}/../in/${file_base_name}/out.txt\" && cmd /c git diff \"${file_path}/../in/${file_base_name}/temp.txt\" \"${file_path}/../in/${file_base_name}/out.txt\" "
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime and input and output ",
"shell_cmd": "gcc -std=c99 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\"< \"${file_path}/../in/${file_base_name}/in.txt\" > \"${file_path}/../in/${file_base_name}/out.txt\" && cmd /c comp /L /M \"${file_path}/../in/${file_base_name}/temp.txt\" \"${file_path}/../in/${file_base_name}/out.txt\" > nul && echo AC || echo NO"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
目录结构同C++
# Python
{
// Windows
// build only
"shell_cmd": "python ${file}",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only C and Cpp files can use this sublime-build file
"selector": "source.py",
"variants": [
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "start cmd /K python ${file}"
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "python ${file}"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Go
{
// Windows
// "shell_cmd": "javac -d ${file_path}/../bin/ -sourcepath ${file} ",
"shell_cmd": "go run ${file}",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only java files can use this sublime-build file
"selector": "source.go",
"variants": [
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "start cmd /K go run ${file_name}"
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "go run ${file_name}"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
补充
如果上面有了上面模板不能满足需求,可根据上面模板自己调整,应该都能调试出自己想要的模板 另外输入输出 可以按照上面 C++
或者 Java
模板创建自己需要 输入输出流模板 以及比较结果 这里就不创建了
如果不想使用模板可以使用shell脚本
example
#!/bin/bash
filename=$1
workdir=$(pwd)/bin
if [ ! -d "bin" ]; then
mkdir bin
fi
javac -d bin -encoding GBK $(pwd)/${filename}.java && cd bin && java ${filename} 2>>err.txt
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
使用
sh xx.sh 类名
1
# 激活教程
# 字体管理
如果不喜欢默认字体,推荐几个好的字体
- jetbrains mono (jetbranisn 全家桶的)
- fira code
- roboto mono
- monaco
字体下载
更换系统字体
如果更换系统字体 ? 点我下载 (opens new window)
更换sublime编辑器字体
preferences->foot->choose
注意
下载的字体不要忘记安装了哦!
# 推荐链接
编辑 (opens new window)
上次更新: 2024-05-21, 09:50:09