Vim basics
17 minute read
What you’ll learn
By the end of this tutorial, you’ll be able to:
- Open, edit, save, and quit files in Vim
- Navigate efficiently without touching the mouse
- Delete, copy, and paste text
- Search and replace
- Edit SLURM scripts and Python code on the cluster
Time: About 30 minutes
Prerequisites: Basic familiarity with command line. Complete Bash Basics first if you’re new to Linux.
Why learn Vim?
When working on DAIC, you’ll often need to edit files directly on the cluster - tweaking a batch script, fixing a bug in your code, or checking a configuration file. Since DAIC is accessed via SSH (no graphical interface), you need a terminal-based text editor.
Vim is the most powerful and ubiquitous terminal editor. It’s installed on every Linux system, so the skills you learn transfer everywhere. While Vim has a steeper learning curve than simpler editors like nano, investing time to learn it pays off:
- Speed: Once fluent, you can edit text faster than with any other editor
- Availability: Always there, no installation needed
- Efficiency: Designed to minimize hand movement and keystrokes
- Ubiquity: Same editor on your laptop, on DAIC, on any server
This tutorial teaches you enough Vim to be comfortable editing files on DAIC. You don’t need to master everything - even basic Vim skills will serve you well.
The most important thing: how to quit
Before anything else, let’s address the most common Vim problem: getting stuck. If you accidentally open Vim and don’t know how to exit, here’s what to do:
- Press
Escseveral times (ensures you’re in the right mode) - Type
:q!and pressEnter
This quits without saving. If you want to save your changes first, use :wq instead.
| Command | What it does |
|---|---|
:q | Quit (only works if no unsaved changes) |
:q! | Quit and discard changes |
:w | Save the file |
:wq | Save and quit |
ZZ | Shortcut for save and quit |
Now that you know how to escape, let’s learn how to actually use Vim.
Understanding Vim’s philosophy
Vim works differently from editors you may be used to (like Word, VS Code, or even Notepad). The key insight is:
You spend more time navigating and editing text than typing new text.
Think about it: when you edit code, most of your time is spent reading, moving around, deleting lines, copying blocks, and making small changes. Typing fresh text is a small fraction of editing.
Vim is optimized for this reality. Instead of always being ready to type (like most editors), Vim has different modes for different tasks:
- Normal mode: Navigate and manipulate text (where you spend most time)
- Insert mode: Type new text
- Visual mode: Select text
- Command mode: Run commands
This might feel awkward at first, but it’s what makes Vim so efficient.
Modes explained
Normal mode: your home base
When you open Vim, you’re in Normal mode. This is your home base - you’ll return here constantly.
In Normal mode, every key is a command:
jmoves down (not typing the letter “j”)dddeletes a linewjumps to the next word
You cannot type text in Normal mode. This is intentional - it lets every key be a powerful command instead of just inserting a character.
To return to Normal mode from anywhere, press Esc. If you’re ever confused about what mode you’re in, press Esc a few times. You’ll always end up in Normal mode.
Insert mode: typing text
When you need to type new text, you enter Insert mode. The most common way is pressing i (for “insert”).
In Insert mode:
- You can type normally, like any other editor
- The bottom of the screen shows
-- INSERT -- - Backspace, arrow keys, and Enter work as expected
When done typing, press Esc to return to Normal mode.
There are several ways to enter Insert mode, each starting you in a different position:
| Key | Where you start typing |
|---|---|
i | Before the cursor |
a | After the cursor |
I | At the beginning of the line |
A | At the end of the line |
o | On a new line below |
O | On a new line above |
The most common are i (insert here), A (append to line), and o (open new line).
Visual mode: selecting text
Visual mode lets you select text, similar to clicking and dragging in other editors. Press v to enter Visual mode, then move the cursor to extend the selection.
Once you’ve selected text, you can:
- Press
dto delete it - Press
yto copy (“yank”) it - Press
>to indent it
Press Esc to cancel the selection and return to Normal mode.
Command mode: running commands
Press : to enter Command mode. You’ll see a colon appear at the bottom of the screen, where you can type commands like:
:w- save (write) the file:q- quit:set number- show line numbers:%s/old/new/g- find and replace
Press Enter to execute the command, or Esc to cancel.
Your first Vim session
Let’s put this together with a hands-on exercise. We’ll create a simple Python script.
Step 1: Open Vim
$ vim hello.py
You’re now in Vim, looking at an empty file. Notice:
- The cursor is at the top left
- Tildes (
~) mark empty lines beyond the file - The bottom shows the filename
You’re in Normal mode. If you try typing, nothing will appear (or unexpected things will happen).
Step 2: Enter Insert mode and type
Press i. The bottom of the screen now shows -- INSERT --.
Type this code:
#!/usr/bin/env python3
print("Hello from DAIC!")
Step 3: Return to Normal mode
Press Esc. The -- INSERT -- message disappears. You’re back in Normal mode.
Step 4: Save and quit
Type :wq and press Enter.
You’ve saved the file and exited Vim. Verify it worked:
$ cat hello.py
#!/usr/bin/env python3
print("Hello from DAIC!")
$ python hello.py
Hello from DAIC!
Congratulations - you’ve completed your first Vim edit!
Navigation: moving around efficiently
One of Vim’s superpowers is fast navigation. In Normal mode, you can move around without touching the mouse or arrow keys.
Basic movement: hjkl
The home row keys h, j, k, l move the cursor:
k
↑
h ← → l
↓
j
h- leftj- down (think: “j” hangs down below the line)k- upl- right
Arrow keys also work, but hjkl keeps your hands on the home row. It feels strange at first but becomes natural with practice.
Moving by words
Character-by-character movement is slow. Jump by words instead:
| Key | Movement |
|---|---|
w | Forward to start of next word |
b | Backward to start of previous word |
e | Forward to end of current/next word |
Try it: open a file and press w repeatedly. Watch the cursor hop from word to word.
Moving within a line
| Key | Movement |
|---|---|
0 | Beginning of line (column zero) |
^ | First non-blank character |
$ | End of line |
The ^ and $ symbols come from regular expressions, where they mean start and end.
Moving through the file
| Key | Movement |
|---|---|
gg | First line of file |
G | Last line of file |
42G | Line 42 (any number works) |
Ctrl+d | Down half a page |
Ctrl+u | Up half a page |
Ctrl+f | Forward one page |
Ctrl+b | Backward one page |
When reviewing a log file, G takes you straight to the end (most recent output), and gg takes you back to the beginning.
Practice exercise
Open any file:
$ vim /etc/passwd
Now practice:
- Press
Gto go to the last line - Press
ggto go to the first line - Press
10Gto go to line 10 - Press
$to go to the end of the line - Press
0to go to the beginning - Press
wseveral times to move by words - Press
:qto quit (no need to save - you shouldn’t modify this file)
Editing text
Now that you can navigate, let’s learn to edit.
Deleting text
In Normal mode, d is the delete command. It combines with movement:
| Command | What it deletes |
|---|---|
x | Character under cursor |
dd | Entire line |
dw | From cursor to start of next word |
de | From cursor to end of word |
d$ | From cursor to end of line |
d0 | From cursor to beginning of line |
dG | From current line to end of file |
dgg | From current line to beginning of file |
The pattern is: d + movement. The dd (delete line) is used so often it gets a shortcut.
Undo and redo
Made a mistake? No problem:
| Command | Action |
|---|---|
u | Undo last change |
Ctrl+r | Redo (undo the undo) |
Vim remembers many levels of undo, so you can press u repeatedly to go back through history.
Copying and pasting
In Vim, copying is called “yanking” (the y key). Pasting is “putting” (the p key).
| Command | Action |
|---|---|
yy | Yank (copy) the current line |
yw | Yank from cursor to start of next word |
y$ | Yank from cursor to end of line |
p | Put (paste) after cursor |
P | Put before cursor |
The pattern is similar to delete: y + movement.
Here’s a useful trick: when you delete with d, the deleted text is saved (like “cut” in other editors). So dd followed by p moves a line - delete it, then paste it elsewhere.
Changing text
The c command deletes and puts you in Insert mode - useful for replacing text:
| Command | Action |
|---|---|
cw | Change word (delete word, enter Insert mode) |
cc | Change entire line |
c$ | Change to end of line |
This is faster than deleting and then inserting separately.
Repeating actions
One of Vim’s best features: press . to repeat the last change.
Example workflow:
- Find a line you want to delete:
/TODO - Delete it:
dd - Find the next one:
n - Repeat the deletion:
. - Continue:
n,.,n,., …
Searching
Finding text
To search forward, press /, type your search term, and press Enter:
/error
Vim jumps to the first match. Then:
n- next matchN- previous match
To search backward, use ? instead of /.
To search for the word under your cursor, press * (forward) or # (backward).
Find and replace
To replace text, use the substitute command:
:s/old/new/
This replaces the first occurrence of “old” with “new” on the current line.
Add flags for more control:
| Command | What it does |
|---|---|
:s/old/new/g | Replace all occurrences on current line |
:%s/old/new/g | Replace all occurrences in entire file |
:%s/old/new/gc | Replace all, but ask for confirmation each time |
The % means “entire file” and g means “global” (all occurrences, not just the first).
Example - update a variable name throughout your code:
:%s/learning_rate/lr/g
Visual mode: selecting text
Sometimes you need to select a region of text before acting on it. Visual mode lets you see exactly what you’re selecting before you delete, copy, or modify it.
Three types of selection
Vim offers three selection styles for different situations:
Character selection (v) - Select specific characters, like highlighting with a mouse. Use when you need part of a line.
Line selection (V) - Select entire lines at once. Use when working with whole lines of code - which is most of the time.
Block selection (Ctrl+v) - Select a rectangular region. Use for columnar data or adding text to multiple lines.
Line selection (V) - the most useful
Line selection is what you’ll use most often when editing code. It selects complete lines, which is usually what you want.
Example: Delete a function
You have a Python file and want to delete an entire function:
def old_function():
x = 1
y = 2
return x + y
def keep_this():
pass
Steps:
- Move to the line
def old_function(): - Press
V- the entire line highlights - Press
jthree times (or3j) to extend selection throughreturn x + y - Press
dto delete all selected lines
The function is gone. If you made a mistake, press u to undo.
Example: Copy a code block to reuse it
You want to copy your SBATCH header to a new script:
#!/bin/bash
#SBATCH --account=ewi-insy
#SBATCH --partition=all
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
python train.py
Steps:
- Move to
#!/bin/bash - Press
Vto start line selection - Press
5jto select down through--gres=gpu:1 - Press
yto yank (copy) - Open your new file:
:e new_script.sh - Press
pto paste
Example: Indent code inside a loop
You’ve written code and need to wrap it in a loop, so you need to indent it:
x = load_data()
y = process(x)
save(y)
Steps:
- Move to
x = load_data() - Press
Vto select the line - Press
2jto extend selection to all three lines - Press
>to indent one level - Press
.to indent again if needed
Result:
x = load_data()
y = process(x)
save(y)
Now you can add your loop above and it’s properly indented.
Example: Comment out multiple lines
You want to temporarily disable some code. With line selection, you can add # to each line:
- Select the lines with
Vand movement - Type
:- you’ll see:'<,'>appear (means “selected range”) - Type
s/^/# /and press Enter
This adds # at the beginning (^) of each selected line.
Character selection (v) - for precision
Use character selection when you need part of a line, not the whole thing.
Example: Delete part of a line
You have:
result = some_very_long_function_name(arg1, arg2, arg3)
You want to delete just some_very_long_function_name and replace it:
- Move cursor to the
sinsome - Press
vto start character selection - Press
erepeatedly orf(to extend to the( - Press
cto change (delete and enter Insert mode) - Type your new function name
- Press
Esc
Example: Copy a specific phrase
You want to copy just the path from this line:
data = load("/tudelft.net/staff-umbrella/project/data.csv")
Steps:
- Move to the
/ - Press
v - Press
f"to select up to (and including) the closing quote - or uset"to stop before it - Press
yto yank - Navigate elsewhere and press
pto paste
Block selection (Ctrl+v) - for columns
Block selection creates a rectangular selection. This is powerful for:
- Editing columnar data
- Adding the same text to multiple lines
- Deleting a column
Example: Add # to comment multiple lines
print("debug 1")
print("debug 2")
print("debug 3")
Steps:
- Move to the
pof the firstprint - Press
Ctrl+vto start block selection - Press
2jto extend down (you’ll see a vertical bar of selection) - Press
I(capital i) to insert before the block - Type
# - Press
Esc- the text appears on all lines
Result:
# print("debug 1")
# print("debug 2")
# print("debug 3")
Example: Delete a column from data
You have space-separated data and want to remove the second column:
apple red 5
banana yellow 3
grape purple 8
Steps:
- Move to the
rinred - Press
Ctrl+v - Press
2jto extend down - Press
eto extend to end of word - Press
dto delete
Result:
apple 5
banana 3
grape 8
Quick reference
| Key | When to use |
|---|---|
V | Deleting, copying, or indenting whole lines (most common) |
v | Selecting part of a line |
Ctrl+v | Editing columns or multiple lines at once |
After selecting, these actions work on your selection:
d- deletey- yank (copy)c- change (delete and start typing)>- indent<- unindent:- run a command on selected lines
Practical workflows for DAIC
Editing a batch script
You need to change the time limit in your SLURM script:
$ vim submit.sh
- Search for the time directive:
/time - Press
nuntil you find#SBATCH --time=1:00:00 - Move to the “1”:
f1(find the character “1”) - Change the number:
cwthen type4thenEsc - Save and quit:
:wq
Adding a line to a script
You need to add a new SBATCH directive:
$ vim submit.sh
- Navigate to the SBATCH section:
/SBATCH - Open a new line below:
o - Type:
#SBATCH --gres=gpu:1 - Exit insert mode:
Esc - Save and quit:
:wq
Viewing a log file
Check the output of a completed job:
$ vim slurm_12345.out
- Go to the end (most recent output):
G - Search backward for errors:
?error - Quit without saving:
:q
For just viewing, you could also use less slurm_12345.out, but Vim’s search is more powerful.
Copying code between files
You need to copy a function from one file to another:
$ vim model.py
- Find the function:
/def train - Start Visual line selection:
V - Select the entire function (move down):
}(jumps to next blank line) - Yank (copy):
y - Open the other file:
:e utils.py - Navigate to where you want the function
- Paste:
p - Save:
:w - Go back:
:e model.pyorCtrl+^
Configuring Vim
Vim reads settings from ~/.vimrc when it starts. Here’s a good starting configuration:
$ vim ~/.vimrc
Enter Insert mode (i) and add:
" Line numbers
set number
" Syntax highlighting
syntax on
" Indentation
set tabstop=4 " Tab width
set shiftwidth=4 " Indent width
set expandtab " Use spaces, not tabs
set autoindent " Copy indent from previous line
" Search
set ignorecase " Case-insensitive search
set smartcase " ...unless you use capitals
set hlsearch " Highlight matches
set incsearch " Search as you type
" Usability
set showmatch " Highlight matching brackets
set mouse=a " Enable mouse
set ruler " Show cursor position
set wildmenu " Better command completion
" Colors
set background=dark
colorscheme desert
Lines starting with " are comments. Save with :wq and the settings apply next time you open Vim.
Learning more
This tutorial covers the essentials. To go further:
Built-in tutorial: Run vimtutor in your terminal for an interactive 30-minute lesson:
$ vimtutor
Gradual learning: Don’t try to learn everything at once. Start with:
ito insert,Escto stop:wqto save and quitddto delete lines,uto undo
Then gradually add new commands as the basic ones become automatic.
Practice: The only way to get comfortable with Vim is to use it. Force yourself to use it for small edits, and the commands will become muscle memory.
Cheat sheet
Modes
| Key | Mode |
|---|---|
Esc | Normal (command) mode |
i, a, o | Insert mode |
v, V | Visual mode |
: | Command mode |
Essential commands
| Command | Action |
|---|---|
:w | Save |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
u | Undo |
Ctrl+r | Redo |
Movement
| Key | Movement |
|---|---|
h j k l | Left, down, up, right |
w, b | Forward, backward by word |
0, $ | Beginning, end of line |
gg, G | Beginning, end of file |
/pattern | Search forward |
Editing
| Command | Action |
|---|---|
i | Insert before cursor |
a | Insert after cursor |
o | Insert on new line below |
dd | Delete line |
yy | Copy line |
p | Paste |
cw | Change word |
. | Repeat last change |
Summary
You’ve learned the essential Vim workflow:
| Task | Commands |
|---|---|
| Open a file | vim filename |
| Enter insert mode | i, a, o |
| Return to normal mode | Esc |
| Save | :w |
| Quit | :q or :wq |
| Navigate | hjkl, w, b, gg, G |
| Delete | x, dd, dw |
| Copy/paste | yy, p |
| Undo/redo | u, Ctrl+r |
| Search | /pattern, n, N |
| Replace | :%s/old/new/g |
| Select lines | V + movement |
Exercises
Practice these tasks to build muscle memory:
Exercise 1: Basic editing
Create a new file, add three lines of text, save and quit. Then reopen it and verify your changes.
Check your work
After :wq, verify with:
$ cat myfile.txt
line one
line two
line three
If the file is empty, you may have quit without saving (:q! instead of :wq).
Exercise 2: Navigation
Open a Python file and practice: go to end (G), go to beginning (gg), jump by words (w, b), go to specific line (10G).
Check your work
Check your position with:set number to show line numbers. After G, you should be on the last line. After gg, you should be on line 1. After 10G, you should be on line 10.Exercise 3: Delete and undo
Open a file, delete a line (dd), undo (u), delete a word (dw), undo again.
Check your work
After eachu, the deleted content should reappear. If undo doesn’t work, make sure you’re in Normal mode (press Esc first).Exercise 4: Copy and paste
Copy a line (yy), move to a new location, paste it (p). Then try with multiple lines using V.
Check your work
Afteryy and p, you should see the same line duplicated. With V, select multiple lines (they highlight), then y to copy and p to paste them elsewhere.Exercise 5: Search and replace
Open a file and search for a word (/word). Then replace all occurrences of one word with another (:%s/old/new/g).
Check your work
After/word and pressing Enter, the cursor jumps to the first match. Press n to see subsequent matches. After :%s/old/new/g, Vim reports how many substitutions were made (e.g., “5 substitutions on 3 lines”).Exercise 6: Real task
Edit a SLURM batch script: change the time limit, add a new #SBATCH directive, and save.
Check your work
After saving, verify your changes:
$ grep -E "time|gres" submit.sh
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
Keep learning
- Run
vimtutorfor a 30-minute interactive tutorial - Practice daily - even small edits help build muscle memory
- Add one new command to your repertoire each week
Next steps
- Slurm Tutorial - Submit jobs to the cluster
- Apptainer Tutorial - Package your environment