Lesson 2: Hello World#
In this lesson, you’ll write your first Java program in Neovim and learn the basics of editing with Vim motions.
Learning Objectives#
By the end of this lesson, you’ll be able to:
- Write a simple Java program
- Understand Vim modes (Normal, Insert, Visual)
- Use basic editing commands
- Save and run your program
Vim Modes Quick Reference#
Neovim has different modes for different tasks:
| Mode | Purpose | How to Enter |
|---|---|---|
| Normal | Navigate and execute commands | Press Esc |
| Insert | Type and edit text | Press i, a, o, etc. |
| Visual | Select text | Press v, V, Ctrl-v |
| Command | Run commands | Press : |
Tip: You’ll spend most time in Normal mode, briefly entering Insert mode to type.
Creating Hello World#
Open your project:
cd ~/java-projects/maven-demo
nvim .Create or open Main.java:
- Press
<Space>neto open file explorer - Navigate to
src/main/java/com/example/ - Press
ato create new file - Type:
Main.java - Press Enter
Writing the Code#
Now you’re in Insert mode (if not, press i). Type the following:
package com.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello from Neovim!");
}
}Basic Vim Editing#
Entering Insert Mode:
i- Insert before cursora- Insert after cursoro- Open new line belowO- Open new line above
Leaving Insert Mode:
Esc- Return to Normal mode
Moving Around (Normal Mode):
h,j,k,l- Left, Down, Up, Rightw- Next wordb- Previous word0- Start of line$- End of linegg- Top of fileG- Bottom of file
Deleting (Normal Mode):
x- Delete characterdd- Delete linedw- Delete wordd$- Delete to end of line
Undo/Redo:
u- UndoCtrl-r- Redo
Saving Your File#
In Normal mode (press Esc first):
:wOr use the leader key shortcut:
<Space>wRunning Your Program#
Option 1: Using Maven#
Open a terminal (you can split Neovim or use a separate terminal):
mvn compile
mvn exec:java -Dexec.mainClass="com.example.Main"Option 2: Using Gradle#
gradle build
gradle runOption 3: Direct Compilation (for simple projects)#
# Compile
javac src/main/java/com/example/Main.java
# Run
java -cp src/main/java com.example.MainOption 4: Using Neovim Terminal#
You can run commands without leaving Neovim:
- Press
<C-7>(Ctrl+7) to toggle the terminal - Run your Maven/Gradle commands
- Press
<C-7>again to toggle back, orCtrl-\thenCtrl-nto exit terminal mode - Type
exitto close the terminal
Editing Practice#
Let’s practice editing your Hello World program:
Exercise 1: Add More Output#
- Move to the line with
System.out.println - Press
oto open a new line below - Type:
System.out.println("Welcome to Java development with Neovim!"); - Press
Escto return to Normal mode - Save with
:w
Exercise 2: Change the Message#
- Move cursor to “Hello” in the string
- Press
ciw(change inner word) - Type:
Greetings - Press
Esc - Save with
:w
Exercise 3: Duplicate a Line#
- Move to any
System.out.printlnline - Press
yyto yank (copy) the line - Press
pto paste below - Edit the new line as needed
- Save with
:w
Common Vim Commands Summary#
| Command | Action |
|---|---|
i | Enter insert mode |
Esc | Return to normal mode |
:w | Save file |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
u | Undo |
Ctrl-r | Redo |
dd | Delete line |
yy | Yank (copy) line |
p | Paste |
/text | Search for “text” |
n | Next search result |
N | Previous search result |
Troubleshooting#
LSP Not Working?#
Check if jdtls is running:
:LspInfoYou should see jdtls listed as attached.
Syntax Highlighting Issues?#
Make sure Treesitter is installed:
:checkhealth nvim-treesitterBuild Errors?#
Check your Java version:
java -version
javac -versionMake sure your pom.xml or build.gradle specifies the correct Java version.
Tips for Vim Beginners#
- Stay in Normal mode - Don’t keep Insert mode on all the time
- Use motions -
w,b,eare faster than arrow keys - Practice
hjkl- It feels awkward at first but becomes natural - Learn incrementally - Master a few commands, then add more
- Run
:Tutor- Neovim’s built-in tutorial is excellent
Practice Exercise#
Create a new class called Calculator.java:
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.add(5, 3);
System.out.println("5 + 3 = " + result);
}
}Use only Vim motions to:
- Create the file
- Write the code
- Save it
- Run it
What’s Next?#
In Lesson 3: Code Completion, you’ll learn to use intelligent auto-completion to code faster.