Vim basics

Learn the Vim text editor for efficient file editing on DAIC.

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:

  1. Press Esc several times (ensures you’re in the right mode)
  2. Type :q! and press Enter

This quits without saving. If you want to save your changes first, use :wq instead.

CommandWhat it does
:qQuit (only works if no unsaved changes)
:q!Quit and discard changes
:wSave the file
:wqSave and quit
ZZShortcut 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:

  • j moves down (not typing the letter “j”)
  • dd deletes a line
  • w jumps 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:

KeyWhere you start typing
iBefore the cursor
aAfter the cursor
IAt the beginning of the line
AAt the end of the line
oOn a new line below
OOn 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 d to delete it
  • Press y to 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!

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 - left
  • j - down (think: “j” hangs down below the line)
  • k - up
  • l - 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:

KeyMovement
wForward to start of next word
bBackward to start of previous word
eForward 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

KeyMovement
0Beginning 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

KeyMovement
ggFirst line of file
GLast line of file
42GLine 42 (any number works)
Ctrl+dDown half a page
Ctrl+uUp half a page
Ctrl+fForward one page
Ctrl+bBackward 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:

  1. Press G to go to the last line
  2. Press gg to go to the first line
  3. Press 10G to go to line 10
  4. Press $ to go to the end of the line
  5. Press 0 to go to the beginning
  6. Press w several times to move by words
  7. Press :q to 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:

CommandWhat it deletes
xCharacter under cursor
ddEntire line
dwFrom cursor to start of next word
deFrom cursor to end of word
d$From cursor to end of line
d0From cursor to beginning of line
dGFrom current line to end of file
dggFrom 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:

CommandAction
uUndo last change
Ctrl+rRedo (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).

CommandAction
yyYank (copy) the current line
ywYank from cursor to start of next word
y$Yank from cursor to end of line
pPut (paste) after cursor
PPut 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:

CommandAction
cwChange word (delete word, enter Insert mode)
ccChange 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:

  1. Find a line you want to delete: /TODO
  2. Delete it: dd
  3. Find the next one: n
  4. Repeat the deletion: .
  5. 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 match
  • N - 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:

CommandWhat it does
:s/old/new/gReplace all occurrences on current line
:%s/old/new/gReplace all occurrences in entire file
:%s/old/new/gcReplace 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:

  1. Move to the line def old_function():
  2. Press V - the entire line highlights
  3. Press j three times (or 3j) to extend selection through return x + y
  4. Press d to 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:

  1. Move to #!/bin/bash
  2. Press V to start line selection
  3. Press 5j to select down through --gres=gpu:1
  4. Press y to yank (copy)
  5. Open your new file: :e new_script.sh
  6. Press p to 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:

  1. Move to x = load_data()
  2. Press V to select the line
  3. Press 2j to extend selection to all three lines
  4. Press > to indent one level
  5. 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:

  1. Select the lines with V and movement
  2. Type : - you’ll see :'<,'> appear (means “selected range”)
  3. 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:

  1. Move cursor to the s in some
  2. Press v to start character selection
  3. Press e repeatedly or f( to extend to the (
  4. Press c to change (delete and enter Insert mode)
  5. Type your new function name
  6. 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:

  1. Move to the /
  2. Press v
  3. Press f" to select up to (and including) the closing quote - or use t" to stop before it
  4. Press y to yank
  5. Navigate elsewhere and press p to 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:

  1. Move to the p of the first print
  2. Press Ctrl+v to start block selection
  3. Press 2j to extend down (you’ll see a vertical bar of selection)
  4. Press I (capital i) to insert before the block
  5. Type #
  6. 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:

  1. Move to the r in red
  2. Press Ctrl+v
  3. Press 2j to extend down
  4. Press e to extend to end of word
  5. Press d to delete

Result:

apple  5
banana 3
grape  8

Quick reference

KeyWhen to use
VDeleting, copying, or indenting whole lines (most common)
vSelecting part of a line
Ctrl+vEditing columns or multiple lines at once

After selecting, these actions work on your selection:

  • d - delete
  • y - 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
  1. Search for the time directive: /time
  2. Press n until you find #SBATCH --time=1:00:00
  3. Move to the “1”: f1 (find the character “1”)
  4. Change the number: cw then type 4 then Esc
  5. Save and quit: :wq

Adding a line to a script

You need to add a new SBATCH directive:

$ vim submit.sh
  1. Navigate to the SBATCH section: /SBATCH
  2. Open a new line below: o
  3. Type: #SBATCH --gres=gpu:1
  4. Exit insert mode: Esc
  5. Save and quit: :wq

Viewing a log file

Check the output of a completed job:

$ vim slurm_12345.out
  1. Go to the end (most recent output): G
  2. Search backward for errors: ?error
  3. 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
  1. Find the function: /def train
  2. Start Visual line selection: V
  3. Select the entire function (move down): } (jumps to next blank line)
  4. Yank (copy): y
  5. Open the other file: :e utils.py
  6. Navigate to where you want the function
  7. Paste: p
  8. Save: :w
  9. Go back: :e model.py or Ctrl+^

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:

  1. i to insert, Esc to stop
  2. :wq to save and quit
  3. dd to delete lines, u to 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

KeyMode
EscNormal (command) mode
i, a, oInsert mode
v, VVisual mode
:Command mode

Essential commands

CommandAction
:wSave
:qQuit
:wqSave and quit
:q!Quit without saving
uUndo
Ctrl+rRedo

Movement

KeyMovement
h j k lLeft, down, up, right
w, bForward, backward by word
0, $Beginning, end of line
gg, GBeginning, end of file
/patternSearch forward

Editing

CommandAction
iInsert before cursor
aInsert after cursor
oInsert on new line below
ddDelete line
yyCopy line
pPaste
cwChange word
.Repeat last change

Summary

You’ve learned the essential Vim workflow:

TaskCommands
Open a filevim filename
Enter insert modei, a, o
Return to normal modeEsc
Save:w
Quit:q or :wq
Navigatehjkl, w, b, gg, G
Deletex, dd, dw
Copy/pasteyy, p
Undo/redou, Ctrl+r
Search/pattern, n, N
Replace:%s/old/new/g
Select linesV + 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.

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).

Exercise 3: Delete and undo

Open a file, delete a line (dd), undo (u), delete a word (dw), undo again.

Exercise 4: Copy and paste

Copy a line (yy), move to a new location, paste it (p). Then try with multiple lines using V.

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).

Exercise 6: Real task

Edit a SLURM batch script: change the time limit, add a new #SBATCH directive, and save.

Keep learning

  • Run vimtutor for 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