Learn the Vim text editor for efficient file editing on DAIC.
---
@@ -12,7 +12,7 @@ By the end of this tutorial, you'll be able to:
- Navigate efficiently without touching the mouse
- Delete, copy, and paste text
- Search and replace
- Edit SLURM scripts and Python code on the cluster
- Edit Slurm scripts and Python code on the cluster
**Time**: About 30 minutes
@@ -24,9 +24,9 @@ By the end of this tutorial, you'll be able to:
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:
Vim is a terminal editor that is installed on practically 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
-**Speed**: Once fluent, common edits take only a few keystrokes
-**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
@@ -179,9 +179,23 @@ Hello from DAIC!
Congratulations - you've completed your first Vim edit!
### Exercise 1: Basic editing
Create a new file, add three lines of text, save and quit. Then reopen it and verify your changes.
{{% alert title="Check your work" color="info" %}}
After `:wq`, verify with:
```shell-session
$cat myfile.txt
line one
line two
line three
```
If the file is empty, you may have quit without saving (`:q!` instead of `:wq`).
{{% /alert %}}
## 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.
In Normal mode, you can move around without touching the mouse or arrow keys.
### Basic movement: hjkl
@@ -238,7 +252,7 @@ The `^` and `$` symbols come from regular expressions, where they mean start and
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
### Exercise 2: Navigation
Open any file:
@@ -255,6 +269,10 @@ Now practice:
6. Press `w` several times to move by words
7. Press `:q` to quit (no need to save - you shouldn't modify this file)
{{% alert title="Check your work" color="info" %}}
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.
{{% /alert %}}
## Editing text
Now that you can navigate, let's learn to edit.
@@ -287,6 +305,13 @@ Made a mistake? No problem:
Vim remembers many levels of undo, so you can press `u` repeatedly to go back through history.
### Exercise 3: Delete and undo
Open a file, delete a line (`dd`), undo (`u`), delete a word (`dw`), undo again.
{{% alert title="Check your work" color="info" %}}
After each `u`, the deleted content should reappear. If undo doesn't work, make sure you're in Normal mode (press `Esc` first).
{{% /alert %}}
### Copying and pasting
In Vim, copying is called "yanking" (the `y` key). Pasting is "putting" (the `p` key).
@@ -303,6 +328,13 @@ 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.
### Exercise 4: Copy and paste
Copy a line (`yy`), move to a new location, paste it (`p`). Then try with multiple lines using `V`.
{{% alert title="Check your work" color="info" %}}
After `yy` 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.
{{% /alert %}}
### Changing text
The `c` command deletes and puts you in Insert mode - useful for replacing text:
@@ -370,7 +402,14 @@ Example - update a variable name throughout your code:
:%s/learning_rate/lr/g
```
## Visual mode: selecting text
### 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`).
{{% alert title="Check your work" color="info" %}}
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").
{{% /alert %}}
## Working with selections
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.
@@ -416,7 +455,7 @@ You want to copy your SBATCH header to a new script:
```bash
#!/bin/bash
#SBATCH --account=ewi-insy
#SBATCH --account=<your-account>
#SBATCH --partition=all
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
@@ -427,7 +466,7 @@ 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`
3. Press `4j` 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
@@ -483,7 +522,7 @@ 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 `(`
3. Press `e`to extend to the end of the function name (underscores count as part of a word);`t(`does the same by moving to just before the `(`
4. Press `c` to change (delete and enter Insert mode)
5. Type your new function name
6. Press `Esc`
@@ -575,7 +614,7 @@ After selecting, these actions work on your selection:
### Editing a batch script
You need to change the time limit in your SLURM script:
You need to change the time limit in your Slurm script:
```shell-session
$vim submit.sh
@@ -606,14 +645,14 @@ $ vim submit.sh
Check the output of a completed job:
```shell-session
$vim slurm_12345.out
$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.
For just viewing, `less slurm-12345.out` also works and cannot modify the file by accident.
### Copying code between files
@@ -633,6 +672,18 @@ $ vim model.py
8. Save: `:w`
9. Go back: `:e model.py` or `Ctrl+^`
### Exercise 6: Real task
Edit a Slurm batch script: change the time limit, add a new `#SBATCH` directive, and save.
{{% alert title="Check your work" color="info" %}}
After saving, verify your changes:
```shell-session
$grep-E"time|gres" submit.sh
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
```
{{% /alert %}}
## Configuring Vim
Vim reads settings from `~/.vimrc` when it starts. Here's a good starting configuration:
@@ -664,7 +715,6 @@ 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
@@ -739,89 +789,6 @@ Then gradually add new commands as the basic ones become automatic.
| `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.
{{% alert title="Check your work" color="info" %}}
After `:wq`, verify with:
```shell-session
$cat myfile.txt
line one
line two
line three
```
If the file is empty, you may have quit without saving (`:q!` instead of `:wq`).
{{% /alert %}}
### 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`).
{{% alert title="Check your work" color="info" %}}
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.
{{% /alert %}}
### Exercise 3: Delete and undo
Open a file, delete a line (`dd`), undo (`u`), delete a word (`dw`), undo again.
{{% alert title="Check your work" color="info" %}}
After each `u`, the deleted content should reappear. If undo doesn't work, make sure you're in Normal mode (press `Esc` first).
{{% /alert %}}
### Exercise 4: Copy and paste
Copy a line (`yy`), move to a new location, paste it (`p`). Then try with multiple lines using `V`.
{{% alert title="Check your work" color="info" %}}
After `yy` 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.
{{% /alert %}}
### 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`).
{{% alert title="Check your work" color="info" %}}
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").
{{% /alert %}}
### Exercise 6: Real task
Edit a SLURM batch script: change the time limit, add a new `#SBATCH` directive, and save.
{{% alert title="Check your work" color="info" %}}
After saving, verify your changes:
```shell-session
$grep-E"time|gres" submit.sh
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
```
{{% /alert %}}
## 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
-[Slurm Tutorial](/tutorials/slurm/) - Submit jobs to the cluster