Basic Text Editing - Mastering Insert, Delete, Change, and Copy Operations

Learning Objectives: By the end of this lesson, you will be able to:

  • Insert text efficiently using various insertion commands
  • Delete text with precision using deletion operators
  • Change text using substitution and modification commands
  • Copy and paste text using yank and put operations
  • Understand and apply the operator-motion paradigm
  • Use text objects for more efficient editing operations
  • Undo and redo changes with confidence
  • Combine operators with counts and motions for powerful editing

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Imagine you’re a sculptor working with clay. You don’t just add clay—you also remove it, reshape it, and move pieces around. Text editing in Neovim is similar: you have precise tools for adding, removing, changing, and rearranging text. What if every editing operation could be as precise and intentional as a sculptor’s chisel?

Lesson Overview

  • Duration: 6-8 hours
  • Prerequisites: Comfortable with modal editing and basic navigation
  • Materials Needed: Various text files (code, prose, data), Neovim with navigation skills

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. Insertion Operations - Various ways to enter Insert mode and add text
  2. Deletion Mastery - Precise text removal with different operators
  3. Change Operations - Efficient text substitution and modification
  4. Copy and Paste - Yank, put, and register management
  5. Operator-Motion Paradigm - Combining operators with movements
  6. Text Objects - Working with words, sentences, and paragraphs as units
  7. Undo and Redo - Managing change history effectively
  8. Advanced Editing Patterns - Complex operations using command combinations

Success Criteria:

By the end of this session, you should perform common editing tasks faster than traditional editors, understand the grammar of Neovim commands, and handle complex text modifications with confidence.

3. Foundation Knowledge (15-20 minutes)

The Grammar of Editing

Neovim commands follow a consistent grammar structure that makes complex operations predictable and learnable:

Command Grammar: [count] operator [count] motion/text-object

Examples:

  • d5w - Delete 5 words
  • c2j - Change 2 lines down
  • y} - Yank until next paragraph

Real-World Analogy: Think of Neovim commands like spoken language. You have verbs (operators), nouns (text objects), and adjectives (counts). Just as “delete three words” is clear in English, d3w is clear in Neovim.

Why This Matters: This grammar system provides:

  • Predictability: Once you learn the pattern, all commands work similarly
  • Composability: Mix and match operators with any motion
  • Efficiency: Express complex edits with few keystrokes
  • Learnability: Master the grammar, not individual commands

Core Editing Operations

1. Insertion (Adding Text)

  • Multiple ways to enter Insert mode for different scenarios
  • Efficient text input without unnecessary cursor movement
  • Quick entry and exit patterns

2. Deletion (Removing Text)

  • Precise text removal with various granularities
  • Delete by character, word, line, or text object
  • Combination with navigation for targeted deletion

3. Change/Substitution (Modifying Text)

  • Replace existing text efficiently
  • Combines deletion and insertion in one operation
  • Powerful for text replacement workflows

4. Copy and Paste (Moving Text)

  • Yank (copy) text to registers
  • Put (paste) text at various positions
  • Multiple clipboards through register system

Key Terminology

  • Operator: A command that acts on text (d, c, y, etc.)
  • Motion: A movement command that defines range
  • Text Object: Structured text units (words, sentences, paragraphs)
  • Register: Named storage for copied/deleted text
  • Count: Number prefix that repeats operations
  • Range: The extent of text an operation affects

4. Detailed Explanation and Examples (25-30 minutes)

Insertion Operations

Efficient insertion is about choosing the right entry point into Insert mode based on where you want to add text.

Basic Insertion Commands

i          " Insert before cursor
a          " Insert after cursor (append)
I          " Insert at beginning of line
A          " Insert at end of line
o          " Open new line below cursor
O          " Open new line above cursor

Advanced Insertion Commands

s          " Substitute character (delete char and insert)
S          " Substitute line (delete line and insert)
C          " Change to end of line
gi         " Insert at last insertion point
gI         " Insert at column 1 (before any indentation)

Insertion Strategy

  • Use i when cursor is exactly where you want to insert
  • Use a when you need to insert after current character
  • Use I for inserting at line beginning (respects indentation)
  • Use A for adding to line end
  • Use o for new lines below
  • Use O for new lines above

Deletion Operations

Deletion in Neovim is precise and undoable. Every deletion is stored in registers for potential recovery.

Character and Word Deletion

x          " Delete character under cursor
X          " Delete character before cursor
dw         " Delete word
dW         " Delete WORD (whitespace delimited)
db         " Delete word backward

Line Deletion

dd         " Delete entire line
D          " Delete to end of line (same as d$)
d0         " Delete to beginning of line
d^         " Delete to first non-whitespace character

Advanced Deletion with Motions

dt,        " Delete until comma (not including comma)
df,        " Delete until comma (including comma)
d}         " Delete until next paragraph
d/pattern  " Delete until search pattern

Change Operations

Change operations combine deletion and insertion, providing efficient text replacement.

Basic Change Commands

cw         " Change word
cW         " Change WORD
cc         " Change entire line
C          " Change to end of line
c0         " Change to beginning of line

Advanced Change Operations

ct,        " Change until comma
cf,        " Change until and including comma
c}         " Change until next paragraph
c/pattern  " Change until search pattern

The Power of Change

Change operations are particularly powerful because they:

  • Delete the target text
  • Enter Insert mode automatically
  • Remember the change for repeat with .

Copy and Paste Operations

Neovim’s copy (yank) and paste (put) system is more powerful than standard clipboard operations.

Basic Yank and Put

yy         " Yank entire line
yw         " Yank word
y}         " Yank until next paragraph
p          " Put after cursor/line
P          " Put before cursor/line

Working with Registers

"ay5w      " Yank 5 words into register 'a'
"ap        " Paste from register 'a'
:reg       " Show all register contents

Special Registers

""         " Default register (last yank/delete)
"0         " Last yank (not affected by deletions)
"+         " System clipboard
"*         " Selection clipboard (Linux/Unix)
"%         " Current filename

5. Live Coding Demonstration (20-25 minutes)

Demonstration 1: Efficient Text Insertion

Starting with this incomplete function:

function calculateTotal() {
    let sum = 
    for (let i = 0; i < items.length; i++) {
        
    }
    return 
}

Task: Complete the function efficiently

  1. Add missing variable initialization:

    • Position: Navigate to end of “sum = " line
    • Command: A0; (append “0;”)
  2. Add loop body:

    • Position: Inside empty loop
    • Command: o (open new line)
    • Type: sum += items[i];
  3. Complete return statement:

    • Position: After “return”
    • Command: A sum; (append " sum;”)

Demonstration 2: Precise Text Deletion

Working with this CSS:

.header-navigation-menu-item {
    background-color: #3498db;
    border-radius: 5px 5px 5px 5px;
    padding: 10px 15px 20px 25px;
    margin: 0px 0px 0px 0px;
}

Task: Clean up redundant code

  1. Remove redundant border-radius values:

    • Position: On border-radius line
    • Find extra values: f5 (find first “5”)
    • Delete redundant: dt; (delete until semicolon)
    • Complete: A5px; (append correct value)
  2. Simplify margin to zero:

    • Position: On margin line
    • Change entire value: f0 then ct; then type 0
  3. Remove unnecessary background-color:

    • Position: On background-color line
    • Delete entire line: dd

Demonstration 3: Complex Change Operations

Working with this Python code:

user_data = {
    'first_name': 'John',
    'last_name': 'Doe', 
    'email_address': 'john.doe@email.com',
    'phone_number': '555-1234'
}

Task: Restructure the data

  1. Change ‘first_name’ to ‘firstName’:

    • Navigate: /first_name
    • Change: ci'firstName
  2. Change email domain:

    • Navigate: /@
    • Change after @: cf'company.com'
  3. Add new field:

    • Position: After last item
    • Add: o'status': 'active'

6. Hands-On Practice (30-40 minutes)

Exercise 1: Insertion Mastery (10 minutes)

Objective: Practice efficient text insertion using appropriate commands

Starting Code:

<div>
<h1></h1>
<p></p>
<ul>
<li></li>
<li></li>
</ul>
</div>

Tasks:

  1. Add class attribute to div: Use A to append class="container"
  2. Add heading text: Position in h1 tags, use i to insert “Welcome”
  3. Add paragraph text: Position in p tags, use i to insert content
  4. Add list items: Position in first li, use i to insert “Item 1”
  5. Duplicate list item: Use yy then p, modify with A for “Item 2”
  6. Add new list item: Use o to open new line, type <li>Item 3</li>

Expected Result:

<div class="container">
<h1>Welcome</h1>
<p>This is a paragraph</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>

Exercise 2: Deletion Precision (15 minutes)

Objective: Practice targeted deletion using various delete commands

Starting Text:

function processUserDataInformation(userData, validationRules, optionalSettings) {
    const userName = userData.firstName + " " + userData.lastName;
    const userEmail = userData.emailAddress;
    const userPhoneNumber = userData.phoneNumber || "N/A";
    
    // TODO: Add validation logic here
    // FIXME: Handle edge cases
    // NOTE: This needs optimization
    
    return {
        name: userName,
        email: userEmail,
        phone: userPhoneNumber,
        validated: false
    };
}

Tasks:

  1. Shorten function name: Delete “Information” from function name using dt(
  2. Remove optional parameter: Delete “, optionalSettings” using f, then dt)
  3. Simplify variable names: Change “userName” to “name” using cw
  4. Remove TODO comment: Delete entire line with dd
  5. Remove “user” prefix from variables:
    • Navigate to “userEmail”, delete “user” with dw
    • Navigate to “userPhoneNumber”, delete “user” with dw
  6. Clean up object: Remove redundant “name” from return object values

Exercise 3: Change and Copy Operations (15 minutes)

Objective: Master change operations and register usage

Starting Configuration:

database:
  host: localhost
  port: 5432
  username: admin
  password: secret123
  database: myapp_development

cache:
  host: localhost
  port: 6379
  timeout: 30

email:
  smtp_host: smtp.gmail.com
  smtp_port: 587
  username: admin
  password: secret123

Tasks:

  1. Change passwords: Use ci' to change both passwords to “new_secure_pass”
  2. Copy database config:
    • Yank entire database section: ya} (yank around paragraph)
    • Paste after email section: G then p
    • Change the copy to be “backup” database
  3. Standardize ports:
    • Change cache port to 6380: Navigate to 6379, use cw6380
    • Change email port to 465: Navigate to 587, use cw465
  4. Add new section: Copy cache section and modify it for “session” storage
  5. Use registers: Store database host in register ‘a’: "ayi', then paste it in other sections

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Accidentally deleting too much text Cause: Incorrect motion or text object selection Solution: Use undo (u) and practice motions in isolation Prevention: Use visual mode to preview selections: v then motion, then operator

  2. Problem: Forgetting to exit Insert mode Cause: Old habits from traditional editors Solution: Build automatic <Esc> habit after every insertion Practice: Set a timer to check mode every 30 seconds

  3. Problem: Lost text after deletion Cause: Not understanding register system Solution: Check registers with :reg and recover with "<register>p Knowledge: Deletions go to default register and can be restored

  4. Problem: Inefficient editing patterns Cause: Using character-by-character operations instead of text objects Solution: Learn and practice text objects

    caw        " Change a word (including surrounding whitespace)
    das        " Delete a sentence
    cap        " Change a paragraph
    

Recovery and Debugging

When Edits Go Wrong

u          " Undo last change
<C-r>      " Redo last undone change
U          " Undo all changes on current line

Register Recovery

:reg       " Show all registers
"1p        " Paste from register 1 (recent deletion)
"0p        " Paste from register 0 (last yank)
"+p        " Paste from system clipboard

Visual Confirmation

v{motion}  " Visually select before operating
V          " Line visual mode for line operations
<C-v>      " Block visual mode for column operations

Performance Tips

Efficient Command Patterns

" Instead of: iHello<Esc>
" Use: IHello<Esc> (if at beginning of line)

" Instead of: xxxxx (delete 5 characters)
" Use: 5x or dw (delete word)

" Instead of: dddddd (delete 6 lines)
" Use: 6dd or d6j

Operator Combinations

dit        " Delete inside HTML/XML tag
ci"        " Change inside quotes
ya(        " Yank around parentheses
c}         " Change until next paragraph

8. Best Practices and Professional Tips (5 minutes)

Command Selection Strategy

Choosing the Right Insertion Command

  • i - When cursor is exactly where you want to insert
  • a - When you need to insert after current character/punctuation
  • A - For adding to end of line (most common)
  • o - For new content that needs its own line
  • I - For adding to beginning of line (preserves indentation)

Efficient Deletion Patterns

  • Character level: x for single characters
  • Word level: dw for partial words, daw for complete words
  • Line level: dd for entire lines, D for rest of line
  • Text objects: dit, ci", da( for structured text

Workflow Optimization

The Edit-Repeat Pattern

Many Neovim operations can be repeated with the . command:

cw           " Change word
.            " Repeat the change on next word
n.           " Find next occurrence and repeat change

Building Complex Operations

Start simple and combine:

" Goal: Change function name in multiple places
1. /function_name<Enter>    " Find first occurrence
2. cw                       " Change word
3. new_name<Esc>           " Type new name
4. n.                      " Find next and repeat

Advanced Editing Techniques

Using Counts Effectively

3cw        " Change 3 words
c3w        " Change 3 words (same result)
5dd        " Delete 5 lines
y2}        " Yank 2 paragraphs

Register Management

"ay5w      " Store 5 words in register 'a'
"by}       " Store paragraph in register 'b'
"ap        " Paste from register 'a'
"bp        " Paste from register 'b'

9. Summary and Review (5 minutes)

Key Takeaways

  1. Master the grammar: [count] operator [count] motion/text-object
  2. Choose insertion wisely: Different insertion commands for different scenarios
  3. Delete precisely: Use appropriate granularity for deletion
  4. Change efficiently: Combine deletion and insertion in one operation
  5. Leverage registers: Multiple clipboards for complex editing workflows

Essential Command Categories

" Insertion: i, a, I, A, o, O
" Deletion: x, dw, dd, D, dt<char>
" Change: cw, cc, C, ct<char>, ci<object>
" Copy/Paste: yy, yw, y<motion>, p, P
" Undo/Redo: u, <C-r>

Quick Review Questions

  1. How do you change a word and immediately start typing the replacement?
  2. What’s the difference between dw and daw?
  3. How do you copy a line and paste it below the current line?
  4. How do you undo the last change?
  5. How do you change text inside quotes?

Answers:

  1. cw then type the replacement
  2. dw deletes from cursor to end of word, daw deletes the entire word plus whitespace
  3. yy then p
  4. u
  5. ci" (change inside quotes)

Building Editing Fluency

Practice Progression

  • Week 1: Master basic insertions and deletions
  • Week 2: Comfortable with change operations and simple text objects
  • Week 3: Efficient use of registers and complex operations
  • Week 4: Automatic command selection for any editing task

Daily Practice Routine

  1. Start session: Practice basic operations on sample text
  2. Work time: Use Neovim for actual editing tasks
  3. End session: Review difficult operations and practice alternatives

For Instructors

Teaching Tips

  • Show command building: Demonstrate how commands are constructed step by step
  • Emphasize reversibility: Stress that almost all operations can be undone
  • Practice variety: Use different text types (code, prose, data) for exercises
  • Build confidence: Start with simple operations and gradually increase complexity
  • Real-world focus: Use actual editing scenarios students encounter

Assessment Ideas

  • Speed tests: Time common editing operations before and after training
  • Command construction: Test understanding of operator-motion grammar
  • Problem solving: Present editing challenges requiring multiple operations
  • Efficiency analysis: Compare student solutions to identify optimization opportunities

Adaptation Notes

  • Programming languages: Adapt examples to languages students use most
  • Skill levels: Advanced students can learn advanced text objects and registers
  • Physical considerations: Some students may need alternative key mappings
  • Learning styles: Provide both visual and kinesthetic practice opportunities

Common Student Questions

  • “When should I use change vs delete+insert?” - Change is faster for replacements
  • “How do I remember all these commands?” - Focus on patterns and grammar, not memorization
  • “What if I delete something important?” - Explain undo system and register recovery
  • “Is it really faster than Ctrl+C/Ctrl+V?” - Demonstrate efficiency with complex operations

Remember: Text editing mastery comes from understanding the grammar and building muscle memory through consistent practice. Students should focus on command patterns rather than memorizing individual commands.