Week 2 Homework - Terminal Text Editors
Time estimate: 1.5 hours maximum
Submission: Create a directory week2-[netid] with your solutions and add it as a submodule to the class repo.
Important Note
This week will feel slower than Week 1. That’s expected. You’re learning a new way to edit text, and your muscle memory needs time to develop.
If any exercise takes significantly longer than the time estimate, stop and ask for help. The goal is learning, not frustration.
Setup
Create your homework directory:
mkdir week2-[netid]
cd week2-[netid]
Exercise 1: Complete vimtutor (30 minutes)
Required
Run the built-in Vim tutorial:
vimtutor
Complete lessons 1 through 4 minimum. If you have time, do all 7 lessons.
Deliverable: Create a file vimtutor_notes.txt with:
- Which lessons you completed (1-4 minimum, 1-7 ideal)
- Three things you learned that surprised you
- One thing you found confusing or difficult
Save this file using Vim:
vim vimtutor_notes.txt- Press
ito enter insert mode - Type your notes
- Press
<Esc>to return to normal mode - Type
:wqto save and quit
Exercise 2: Set Up Your .vimrc (15 minutes)
Required
Create a basic Vim configuration file.
Steps:
-
Create
~/.vimrcif it doesn’t exist:vim ~/.vimrc -
Copy the basic config from the Week 2 notes (the “Starter .vimrc” section)
-
Save and test:
vim test.txtVerify you see:
- Line numbers
- Relative line numbers
- Syntax highlighting
- Your cursor line highlighted
Deliverable: Create a file vimrc_setup.txt answering:
- What settings did you add to your .vimrc?
- Which setting do you think will be most useful?
- Did you customize anything beyond the starter config? If so, what?
Exercise 3: Install a Plugin Manager and Plugin (20 minutes)
Required
Modern Vim users manage plugins with a plugin manager. We’ll use vim-plug.
Part A: Install vim-plug
For Vim:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
For Neovim: (or whatever plugin manager you’re using)
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
Part B: Configure plugins in .vimrc
Add this section to your ~/.vimrc (above your other settings):
" Plugins
call plug#begin('~/.vim/plugged')
" Add plugins here
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
call plug#end()
Part C: Install the plugins
- Open Vim:
vim - Run the command:
:PlugInstall - Wait for installation to complete
- Quit and reopen Vim
Part D: Test fzf
fzf is a fuzzy file finder (like Ctrl-P in VS Code).
- Navigate to a directory with multiple files
- Open Vim:
vim - Type
:Filesand press Enter - Start typing a filename - see it filter in real-time
- Press Enter to open a file
Deliverable: Create plugin_setup.txt with:
- Screenshot or description of fzf working
- What command did you use to install plugins? (
:PlugInstall) - What does fzf do and why is it useful?
Bonus: Add a keybinding to open fzf with Ctrl-P:
" Add to your .vimrc
nnoremap <C-p> :Files<CR>
Exercise 4: Vim Movement Practice (15 minutes)
Required
Download this practice file:
curl -o movement_practice.txt https://raw.githubusercontent.com/iggredible/Learn-Vim/master/ch05_moving_in_file.md
Open it in Vim and complete these tasks using ONLY Vim motions (no arrow keys, no mouse):
- Jump to the beginning of the file
- Jump to the end of the file
- Find the word “vertical” (search forward)
- Jump to line 50
- Delete the word under your cursor
- Delete from cursor to end of line
- Undo your last change
- Yank (copy) a line
- Paste it below
Deliverable: Create movement_log.txt documenting:
- The command you used for each task above
- Which motion felt most natural?
- Which motion felt most awkward?
Example format:
1. Jump to beginning: gg
2. Jump to end: G
...
Exercise 5: Edit a Shell Script with Vim (20 minutes)
Required
Create a script using only Vim and proper Vim workflows.
Task: Write a script word_counter.sh that:
- Takes a filename as an argument
- Counts total words
- Counts unique words
- Shows the 5 most common words
Requirements:
- Use ONLY Vim to create and edit the script
- Practice using
oto open new lines - Use
dwto delete words when fixing mistakes - Use
cwto change words - Use
yyandpto copy/paste lines
Starter code (type this in Vim, don’t copy-paste):
#!/usr/bin/env bash
set -euo pipefail
# TODO: Add argument validation
# TODO: Count total words
# TODO: Count unique words
# TODO: Show top 5 most common words
Expected behavior:
$ ./word_counter.sh sample.txt
Total words: 342
Unique words: 156
Top 5 words:
45 the
32 and
28 to
22 a
19 of
Hints:
- Use
trto split words - Use
sortanduniq -c - Use
head -5for top 5
Deliverable: The completed word_counter.sh script
Exercise 6: Visual Mode Practice (15 minutes)
Required
Create a file visual_practice.py with this messy Python code:
def calculate_total(items):
x = 0
for item in items:
x = x + item
return x
def calculate_average(items):
total = calculate_total(items)
count = len(items)
return total / count
def main():
numbers = [1, 2, 3, 4, 5]
print(calculate_total(numbers))
print(calculate_average(numbers))
Tasks (using Vim):
-
Fix the indentation in
calculate_totalfunction- Use Visual Line mode (
V) to select the poorly indented lines - Use
>to indent
- Use Visual Line mode (
-
Add a docstring to
calculate_total:"""Calculate the sum of all items."""- Use
oto open a new line below the function definition - Type the docstring
- Use
-
Comment out the
printstatements inmain- Use Visual Block mode (
Ctrl-v) to select the start of both lines - Use
Ito insert at the beginning - Type
# - Press
<Esc>
- Use Visual Block mode (
-
Duplicate the entire
calculate_averagefunction- Use
Vto select the entire function - Use
yto yank (copy) - Navigate below it
- Use
pto paste - Change the duplicate to
calculate_medianusingcw
- Use
Deliverable: The corrected visual_practice.py file
Exercise 7: Search and Replace (10 minutes)
Required
Create replace_practice.txt with:
TODO: Fix the login bug
TODO: Update documentation
DONE: Add tests
TODO: Refactor database code
DONE: Deploy to staging
TODO: Review pull request
Tasks (using Vim search and replace):
- Replace all “TODO” with “IN_PROGRESS” globally
- Replace only the first “DONE” with “COMPLETED”
- Delete all lines containing “database”
Commands you’ll need:
:%s/old/new/g " Replace all in file
:s/old/new/ " Replace first on line
:g/pattern/d " Delete lines matching pattern
Deliverable:
- The modified
replace_practice.txt - A file
replace_commands.txtlisting the exact commands you used
Exercise 8 (Bonus): Macros (15 minutes)
Optional - Extra Credit
Given this CSV data in grades.csv:
Alice,85,92,88
Bob,78,85,90
Charlie,92,88,95
David,88,90,87
Eve,95,92,98
Task: Use a Vim macro to transform it to:
Student: Alice, Average: 88.33
Student: Bob, Average: 84.33
Student: Charlie, Average: 91.67
Student: David, Average: 88.33
Student: Eve, Average: 95.00
Approach:
- Record a macro that:
- Extracts the student name
- Calculates the average (you can use Python or bc)
- Formats the output line
- Replay it for all lines
Hint: You might need to use external commands from Vim:
:.!python3 -c "print(sum([85, 92, 88])/3)"
Deliverable:
- The transformed
grades_formatted.txt - A file
macro_explanation.txtexplaining your macro
Exercise 9 (Bonus): Vim Golf (10 minutes)
Optional - Extra Credit
Vim Golf is about solving editing challenges in minimum keystrokes.
Challenge: Given this text in golf.txt:
function_name
Transform it to:
def function_name():
pass
Your score: Number of keystrokes (lower is better)
Deliverable: A file golf_solution.txt with:
- Your keystroke sequence
- Your total count
- Explanation of what each keystroke does
Example solution format:
Keystrokes: Idef <Esc>A():<Esc>opass<Esc>
Count: 23
Explanation:
I - Insert at beginning of line
def - Type "def "
<Esc> - Return to normal mode
...
Submission Checklist
Your week2-[netid] directory should contain:
Required Files
week2-netid/
├── vimtutor_notes.txt
├── vimrc_setup.txt
├── plugin_setup.txt
├── movement_log.txt
├── word_counter.sh
├── visual_practice.py
├── replace_practice.txt
└── replace_commands.txt
Bonus Files
├── grades_formatted.txt
├── macro_explanation.txt
├── golf_solution.txt
└── golf_explanation.txt
Before submitting:
- Make all scripts executable:
chmod +x *.sh - Test that your .vimrc works:
vim --clean -u ~/.vimrc - Verify plugins load: Open Vim and run
:PlugStatus - Commit and push to your repo
Grading
- Exercises 1-7: Required
- Exercises 8-9: Bonus
Completion-based grading: If you attempted the exercise honestly and followed the requirements, you get full credit for that exercise.
Tips for Success
Start Small
Don’t try to memorize everything. Focus on:
hjklfor movementifor insert,<Esc>to exitdd,yy,pfor delete/copy/paste:w,:q,:wqfor file operations
Use the Help
Vim has amazing built-in help:
:help motion
:help operator
:help visual-mode
:help :substitute
Check Your Progress
After each exercise, verify you can:
- Open a file in Vim
- Navigate without arrow keys
- Make edits efficiently
- Save and quit without panic
Common Mistakes to Avoid
- Using arrow keys: Force yourself to use
hjkl - Staying in Insert mode: Spend most time in Normal mode
- Not using text objects: Learn
diw,ci(,da" - Ignoring the dot command:
.repeats your last change
When You’re Stuck
- Press
<Esc>several times (get to Normal mode) - Type
:helpfollowed by what you’re trying to do - Check the Week 2 notes
- Google: “vim how to [what you want]”
- Come to office hours
Practice Strategy
Week 2: Force yourself to use Vim for all homework
Week 3: Use Vim exclusively (no VS Code fallback)
Week 4+: Enjoy being faster than before
Resources
- Built-in help:
:help(from within Vim) - Interactive practice: https://www.openvim.com/
- Vim Adventures: https://vim-adventures.com/ (game)
- Cheat sheet: https://vim.rtorr.com/
- Our notes: Week 2 notes on course website
Final Thoughts
Learning Vim is like learning to touch type. The first week is painful. The second week is frustrating. The third week you start to see it. By the fourth week, you wonder how you ever lived without it.
Stick with it.
By Week 4, you’ll be editing at the speed you think, and every other editor will feel slow.
Remember:
- Vim is a language for editing
- Operators + motions = powerful combinations
- Modal editing feels weird until it doesn’t
- The best way to learn is to force yourself to use it
Good luck, and see you in Week 3!
Submit by: Next Wednesday via git submodule
Stuck? Ask for help early, don’t suffer in silence