How to Setup Sublime Text 3 for Competitive Programming for Java

Image for post
Image for post

Welcome guys! If you are a programmer then you must have heard about Sublime Text “sophisticated text editor for coding”. It’s so popular because it is highly configurable, extremely lightweight, and has a sleek and beautiful design.

This setup will save your time and make you able to focus only on the problem rather than focusing on other things. It is preferred to have some kind of template to save time during online contests.

Hoping that you have already installed Sublime Text 3.

Now, lets get Started!

Setup Input and Output window

We all have to paste that same input to console again and again if code fails and this wastes a lot of time. In online contest, time matter a lot. To avoid wasting time, we are going to take input from a file and writing the output to another file. So when ever we have to give input to the program, we will just paste that into the input file.

To setup this:

  • Go to View -> Layout -> Column-3 (or Press Alt+shift+3).
Layout
Layout

Then View -> Groups -> Max Column 2

group
group

Now we have setup the input and output window.

Setup build system for Java

Now we have to setup the build system in order to compile and run our java program. When compilation of java program is done, .class files are create. So we are going to separate those files in “out” folder.

  • First create a folder called “java” then inside that folder create another folder called “out”.
  • Inside “out” folder create two text files called “input.txt” and “output.txt”.
  • Then note down its path because later we will need this.
  • Now inside sublime text, Go to Tools -> Build System -> New Build System.
build system
build system
  • Now a file is open. Then paste this code:
{
"shell_cmd": "javac -d <out folder path> $file && java -cp <out folder path> $file_base_name",
"file_regex": "^(...?):([0-9]):?([0-9]*)",
"selector": "source.java",
"shell": true
}

Wherever, I have specified “<out folder path>” just replace that with the out folder that you have noted.

  • Then save this file with the name “java.sublime-build”.

That all you have successfully setup the Build System for Java.

Code Snippet — Adding Boiler plate code

In Sublime Text or any editor or IDE, we can add our own code snippet. So when ever we trigger that, editor will place the code.

To add code snippet in Sublime-Text:

  • Go to Tools -> Developer -> New Snippet
code snippet
code snippet
  • Now a file is open. Then paste this code:
<snippet>
<content><![CDATA[
import java.io.*;
import java.util.*;
public class ${TM_FILENAME/(.+)\..+|.*/$1/:name} {static long TIME_START, TIME_END;public static class Task {
public void solve(Scanner sc, PrintWriter pw) throws IOException {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
}
}
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(new FileReader(new File("out/input.txt")));
Scanner sc = new Scanner(System.in);
// PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File("out/output.txt"))));
PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
Runtime runtime = Runtime.getRuntime();
long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
TIME_START = System.currentTimeMillis();
Task t = new Task();
t.solve(sc, pw);
TIME_END = System.currentTimeMillis();
long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
pw.close();
System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader s) throws FileNotFoundException {
br = new BufferedReader(s);
}
public String next() throws IOException {
while (st == null || ! st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
]]></content>
<tabTrigger>jmain</tabTrigger>
<source>source.java</source>
</snippet>
  • This code includes a Fast Scanner class which is fast than the native Scanner class — I have added this because in some cases, our code can give TLE (Time Limit Exceed).
  • You can replace the name which is inside the <tabTrigger> tag. So whenever you type that name, it will show suggest and when you press TAB or ENTER key, this code will be added.
code-snippet-trigger
code-snippet-trigger
code-snippet-code
code-snippet-code

That all you have successfully setup the code snippet for java boilerplate code.

Compile and Run a Java Program

We have setup the build system for java and by using that we will compile and our java program.

  • First create a file called “<file_name>.java” outside the “out” folder which you have created.
  • Inside that file type that code-snippet trigger which you have created and press TAB or ENTER key.
  • Then click on Upper Right window and open the “input.txt” file which is present inside the “out” folder.
  • Click on Bottom Right window and open the “output.txt” file which is also present inside the “out” folder.
  • Now uncomment these lines in order to read and write input and output from “input.txt” and “output.txt”:
1. Scanner sc = new Scanner(new FileReader(new File("out/input.txt")));
2 .PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File("out/output.txt"))));
  • Now you can write your logic inside the solve() method and run the program by pressing “CTRL + B”.

Example:

Output
Output

Now you are ready to go!

Extras — Plugins

Plugins make the editors or IDEs more powerful than before. They also save out time and make editors more attractive.

Here are the plugins which I recommend:

  • Package Control
  • Side Bar
  • Terminus
  • Kite
  • SublimeAStyleFormatter

That’s it guys! Hope you find this article useful. Thanks for reading.

Happy Coding :)

Student | Java Programmer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store