25 min to read
Copy, Insert, and Customize Like a Power User
Master Yanking, Advanced Insertion, Replace Mode, and Settings That Transform Your Editing Experience
You need to copy a function definition to another part of your script, open new lines efficiently without awkward key combinations, overwrite incorrect parameter values, and make your searches case-insensitive. Each of these tasks has a specific VIM command designed for maximum efficiency.
This lesson covers the final essential commands that separate casual VIM users from power users. You’ll learn to copy text without deleting it, insert lines and text with precision, replace entire sequences of characters, and customize VIM’s behavior to match your workflow.
Let’s complete your VIM education.
Creating Our Practice File
We’ll create a Python analysis script with various elements to practice on:
vim data_processing.py
Press i to enter Insert Mode, then type:
#!/usr/bin/env python3
# Data Processing Pipeline
# Version 1.0
import pandas as pd
import numpy as np
from pathlib import Path
# Configuration
INPUT_DIR = "/data/raw"
OUTPUT_DIR = "/results/processed"
REFERENCE_FILE = "reference_genes.txt"
# Load reference data
def load_reference(filename):
"""Load reference gene list"""
with open(filename, 'r') as f:
genes = [line.strip() for line in f]
return genes
# Process sample data
def process_sample(sample_id):
"""Process a single sample"""
input_file = f"{INPUT_DIR}/{sample_id}.csv"
data = pd.read_csv(input_file)
# Filter by quality
filtered = data[data['quality'] > 30]
# Calculate statistics
mean_value = filtered['expression'].mean()
std_value = filtered['expression'].std()
return mean_value, std_value
# Main analysis function
def analyze_samples(sample_list):
"""Analyze all samples in the list"""
results = []
for sample_id in sample_list:
mean, std = process_sample(sample_id)
results.append({
'sample': sample_id,
'mean': mean,
'std': std
})
return pd.DataFrame(results)
# Save results
def save_results(dataframe, output_filename):
"""Save analysis results to file"""
output_path = Path(OUTPUT_DIR) / output_filename
dataframe.to_csv(output_path, index=False)
print(f"Results saved to {output_path}")
# Entry point
if __name__ == "__main__":
samples = ["sample_001", "sample_002", "sample_003"]
print("Starting analysis...")
results = analyze_samples(samples)
save_results(results, "analysis_results.csv")
print("Analysis complete!")
Press ESC, type :wq, press ENTER.
Command 1: Open New Lines - o and O
You’ve already learned i (insert before cursor) and A (append at end of line). Now learn the most efficient way to create new lines.
Open Line Below: o (lowercase)
Press o (lowercase o) and VIM:
- Creates a new line below the current line
- Moves your cursor to that new line
- Enters Insert Mode
Try it:
vim data_processing.py
- Navigate to the line
import numpy as np - Press
o - A new blank line appears below, cursor is there, Insert Mode is active
- Type
import sys - Press
ESC
You’ve added an import statement in one smooth motion: o, type, ESC.
Compare to the old way:
- Press
Ato go to end of line - Press
ENTERto create new line - Type your text
- Press
ESC
With o, you skip the “go to end of line” step. Just press o anywhere on the line, and you’re on a new line below, ready to type.
Open Line Above: O (capital O - Shift-O)
Press O (capital O) and VIM:
- Creates a new line above the current line
- Moves your cursor to that new line
- Enters Insert Mode
Try it:
- Navigate to the line
# Configuration - Press
O - A new blank line appears above
- Type
# Global settings - Press
ESC
Real-world usage scenarios:
Add a comment above a function:
- Navigate to
def load_reference(filename): - Press
O - Type
# TODO: Add error handling - Press
ESC
Add import at top of import block:
- Navigate to first import line
- Press
O - Type new import
- Press
ESC
Insert a blank line for readability:
- Navigate to where you want space
- Press
o - Press
ESCimmediately (creates blank line without adding text)
Why o and O Are Essential
These commands handle one of the most common editing operations: adding new lines. Without o and O, you’re constantly navigating to line ends or beginnings before pressing ENTER. With them, you just position anywhere on the line and press o or O.
Pattern for adding lines:
- Need new line below? Press
o - Need new line above? Press
O - Type your content
- Press
ESC
You’ll use these commands dozens of times per editing session.
Command 2: Advanced Insertion - a and A
You already know A (append at end of line). Now let’s understand a (append after cursor) and see how it differs from i.
Append After Cursor: a
Press a (lowercase a) to enter Insert Mode with the cursor positioned after the current character.
The difference between i and a:
i- insert before the cursora- insert after the cursor (append)
Try it:
- Navigate to the line
INPUT_DIR = "/data/raw" - Position cursor on the last
"quote - Press
a - Type additional path:
/fastq - Press
ESC
The path now reads "/data/raw/fastq".
When to use a vs i:
Use a when you want to add text after the current character:
- Adding to end of a word: cursor on last letter, press
a, type - Continuing a string: cursor on closing quote, press
a, type before quote - Adding suffix to identifier: cursor on last character, press
a, type
Use i when you want to insert before the current character:
- Adding prefix: cursor on first letter, press
i, type - Inserting into middle of word: cursor where you want to insert, press
i, type
You Already Know A
We covered A (capital A) in lesson 1, but it’s worth reinforcing:
Press A to jump to end of line AND enter Insert Mode in one command.
Example:
- Navigate to
REFERENCE_FILE = "reference_genes.txt" - Press
A - Type ` # Path to gene reference`
- Press
ESC
Added a comment without navigating to the end first.
Command 3: Move to End of Word - e
You’ve learned w (move to start of next word). Now learn e (move to end of current word).
Try it:
- Navigate to any line with multiple words
- Position cursor at the start of a word
- Press
e - Cursor jumps to the last character of that word
- Press
eagain - Cursor jumps to end of next word
The difference:
w- moves to the start of the next worde- moves to the end of the current word (or next if already at end)
Why this matters:
Combined with operators, e becomes powerful:
de- delete to end of word (you learned this before)ce- change to end of word (you learned this too)ye- yank (copy) to end of word (coming up next!)
Navigation pattern:
- Moving forward through words: use
w(jump to starts) - Working with word endings: use
e(jump to ends) - Going back: use
b(back to start of previous word - you’ll learn this soon)
Command 4: Yank (Copy) - y
Finally! VIM’s copy command. In VIM terminology, copying is called “yanking” (because you yank text into a register for later use).
The yank operator: y
Just like d (delete) and c (change), y works with motions:
yw- yank wordye- yank to end of wordy$- yank to end of lineyy- yank entire liney3w- yank 3 words2yy- yank 2 lines
Yank a Line: yy
- Navigate to the line
import pandas as pd - Press
yy - The line is yanked (copied) - you’ll see a message briefly or nothing at all
- Navigate to where you want to paste
- Press
p - The line pastes below the cursor
Try it:
- Navigate to
import pandas as pd - Press
yy(yank the line) - Move down a few lines
- Press
p(paste) - The import line appears again
Press u to undo the paste.
Yank Multiple Lines: [number]yy
- Navigate to the first import line
- Press
3yy(yank 3 lines) - Navigate to somewhere else in the file
- Press
p - All three lines paste
Yank Word: yw
- Navigate to any variable name, like
sample_id - Position cursor at the start of the word
- Press
yw - Navigate elsewhere
- Press
p - The word pastes after cursor
Yank to End of Line: y$
- Navigate to middle of a line
- Press
y$ - Navigate to another line
- Press
p - The rest of that line pastes
Yank in Visual Mode
You can also select text visually and yank it:
- Press
vto enter Visual mode - Move cursor to select text
- Press
yto yank the selection - Navigate elsewhere
- Press
pto paste
Or use Visual Line mode:
- Press
V(Shift-V) to enter Visual Line mode - Press
jorkto select lines - Press
yto yank selected lines - Navigate elsewhere
- Press
pto paste
Yank and Paste a Function
Let’s copy an entire function:
- Navigate to
def load_reference(filename): - Press
Vto enter Visual Line mode - Press
jrepeatedly until you’ve selected the entire function (to thereturn genesline) - Press
yto yank - Navigate to the bottom of the file
- Press
pto paste
You’ve copied an entire function! Press u to undo.
The Complete Copy-Paste Workflow
Pattern:
- Navigate to text to copy
- Yank it:
yy(line),3yy(multiple lines),yw(word), or visual selection +y - Navigate to destination
- Paste:
p(after/below) orP(before/above)
Compare to delete-paste (cut-paste):
- Delete:
dd,dw, etc. - removes text AND puts in register - Yank:
yy,yw, etc. - copies text without removing it
Both can be pasted with p.
Command 5: Replace Mode - R
Replace mode is like Insert mode, but it overwrites existing text instead of inserting.
Press R (capital R, Shift-R) to enter Replace mode.
Every character you type replaces the character under the cursor, and the cursor moves forward.
Press ESC to return to Normal mode.
Example 1: Replace a Number
- Navigate to the line
filtered = data[data['quality'] > 30] - Position cursor on the
3in30 - Press
R - Type
20 - The
30becomes20(the3was replaced by2, the0was replaced by0) - Press
ESC
Example 2: Replace a Word
- Navigate to
mean_value = filtered['expression'].mean() - Position cursor on
meaninmean_value - Press
R - Type
avg_ - Now it reads
avg_value(but you’ve replaced 4 characters with 4 characters) - Press
ESC
Example 3: Replace Text of Different Length
- Navigate to
"sample_001" - Position cursor on
001 - Press
R - Type
042 - The
001becomes042 - Press
ESC
When to Use Replace Mode
Use Replace mode when:
- Fixing values that are the wrong length: wrong number, wrong ID
- Overwriting fixed-width fields in formatted text
- Changing parameter values in configuration
- Updating version numbers or dates
Don’t use Replace mode when:
- Inserting new text (use Insert mode)
- Changing text that will shift other text (use change commands)
Replace mode vs r command:
r- replace single character, automatically returns to Normal modeR- replace mode, keeps replacing until you pressESC
Use r for single characters, R for multiple characters.
Command 6: VIM Settings - :set
VIM’s behavior can be customized with settings. These settings make your editing experience more powerful and pleasant.
Set a Setting: :set option
:set option
Unset a Setting: :set nooption
:set nooption
The no prefix turns an option off.
Essential Search Settings
Ignore Case in Searches: :set ignorecase or :set ic
By default, VIM searches are case-sensitive. /sample won’t match “Sample”.
Turn on case-insensitive search:
:set ignorecase
Or use the short form:
:set ic
Try it:
- Type
:set icand pressENTER - Type
/sampleand pressENTER - VIM finds “sample”, “Sample”, “SAMPLE”, etc.
To turn it off:
:set noignorecase
Or:
:set noic
Incremental Search: :set incsearch or :set is
With incremental search, VIM shows matches as you type the search pattern.
:set incsearch
Try it:
- Type
:set isand pressENTER - Type
/pro(don’t press ENTER yet) - As you type each letter, VIM jumps to the first match
- Complete the search or press
ESCto cancel
This is incredibly useful for finding text quickly—you see results immediately and can stop typing once you’ve found what you want.
To turn it off:
:set noincsearch
Highlight Search Matches: :set hlsearch or :set hls
This highlights all matches of your search pattern in the file.
:set hlsearch
Try it:
- Type
:set hlsand pressENTER - Type
/sampleand pressENTER - All occurrences of “sample” are highlighted throughout the file
- Press
nto jump between highlighted matches
To clear highlights (without turning off the setting):
:nohlsearch
Or shorter:
:noh
This clears the current highlights but keeps the setting active for future searches.
To turn off the setting entirely:
:set nohlsearch
Combining Settings
These three search settings work beautifully together:
:set ignorecase
:set incsearch
:set hlsearch
Now searching is:
- Case-insensitive (finds Sample, SAMPLE, sample)
- Incremental (shows matches as you type)
- Highlighted (all matches visible at once)
Checking Settings
To see if a setting is on or off:
:set option?
For example:
:set ignorecase?
Shows ignorecase or noignorecase.
Other Useful Settings
Show Line Numbers: :set number
:set number
Or:
:set nu
Line numbers appear on the left side. Useful when jumping to specific lines or referencing code locations.
Turn off:
:set nonumber
Enable Mouse Support: :set mouse=a
:set mouse=a
Now you can click to position cursor, select text with mouse, scroll with mouse wheel.
Turn off:
:set mouse=
Show Matching Brackets: :set showmatch
:set showmatch
When you type a closing bracket ), ], or }, the cursor briefly jumps to the matching opening bracket.
Autoindent: :set autoindent
:set autoindent
When you press ENTER, the new line starts at the same indentation level as the previous line.
Making Settings Permanent
Settings you type with :set only last for the current session. To make them permanent, add them to your .vimrc file (we’ll cover this in a future lesson).
Practical Workflows
Workflow 1: Copy Function and Modify
You need to create a similar function:
- Navigate to the original function
- Press
Vto enter Visual Line mode - Select the entire function with
j - Press
yto yank (copy) - Navigate to where you want the new function
- Press
pto paste - Modify the pasted function using change commands (
cw,ce, etc.)
Workflow 2: Efficient Line Addition
Adding multiple lines with comments:
- Navigate to where you want new lines
- Press
o(new line below) - Type comment:
# Step 1: Load data - Press
ESC - Press
oagain - Type:
# Step 2: Process data - Press
ESC - Continue…
Much faster than navigating to end of line, pressing ENTER, etc.
Workflow 3: Replace Parameter Values
Updating configuration values:
- Enable settings for better search:
:set ic :set hls - Search for parameter:
/quality > 30 - Position cursor on
30 - Press
R - Type
25 - Press
ESC - Press
nto find next occurrence - Repeat replace if needed
Workflow 4: Copy-Paste Multiple Elements
Building a list from existing elements:
- Yank a line:
yy - Paste it multiple times:
p,p,p - Modify each pasted line:
cwto change words
Or:
- Yank once:
yy - Paste:
p - Modify:
cw, type,ESC - Yank again:
yy(yank the modified line) - Paste:
p - Modify:
cw, type,ESC - Continue building list
Practice Exercises
Exercise 1: Master Opening Lines
- Open the script:
vim data_processing.py - Navigate to the
# Configurationline - Press
Oto add line above - Type
# Pipeline settings - Press
ESC - Press
oto add line below - Type
VERSION = "2.0" - Press
ESC
Exercise 2: Copy and Paste Functions
- Navigate to the
load_referencefunction - Press
Vto enter Visual Line mode - Select entire function (including docstring)
- Press
yto yank - Navigate to end of file:
G - Press
pto paste - Undo:
u
Exercise 3: Use Replace Mode
- Navigate to
"sample_001" - Position cursor on
001 - Press
R - Type
999 - Press
ESC - Undo:
u
Exercise 4: Configure Search Settings
- Type
:set icand pressENTER - Type
:set hlsand pressENTER - Type
:set isand pressENTER - Search:
/defand pressENTER - Notice how all function definitions are highlighted
- Press
nto cycle through matches - Clear highlights:
:nohand pressENTER
Exercise 5: Yank and Paste Workflow
- Navigate to the imports section
- Press
yyonimport pandas as pd - Move down one line
- Press
pto paste - Change the pasted line:
cw, typeimport matplotlib.pyplot as plt,ESC - Undo:
u, thenuagain
Exercise 6: Build Code with o and Yank
- Navigate to the samples list in
__main__ - Press
oto open line below - Type a new sample:
"sample_004", - Press
ESC - Press
yyto yank the line - Press
pto paste - Change:
cw, type"sample_005",,ESC
Advanced Tips
Yank to Named Registers
You can yank to specific registers (like a clipboard with multiple slots):
"ayy - yank line to register 'a'
"ap - paste from register 'a'
This lets you have multiple copied items at once. We’ll cover registers in detail in an advanced lesson.
Visual Block Mode
Press Ctrl-V to enter Visual Block mode, which lets you select rectangular blocks of text. Great for editing columns of data or adding comments to multiple lines.
Append vs Insert After Yank-Paste
After pasting with p, you can:
- Press
ato append to the pasted text - Press
Ato append at end of pasted line - Press
oto add new line below pasted text
Replace Mode Variations
R- Replace mode (overwrite characters)r- Replace single character (you learned this before)gR- Virtual Replace mode (replaces screen space, handles tabs differently)
For most use cases, R is what you want.
Common Patterns
Open new line below and type:
o [type text] ESC
Open new line above and type:
O [type text] ESC
Copy line and paste:
yy [navigate] p
Copy multiple lines:
3yy [navigate] p
Copy visually selected text:
V [select] y [navigate] p
Replace multiple characters:
R [type replacement] ESC
Enable all search enhancements:
:set ic hls is
Why These Commands Complete Your VIM Mastery
Opening lines (o, O) makes adding content natural and fast—no more awkward navigation-enter-navigation sequences.
Yanking (y) finally lets you copy without deleting first. Combined with paste (p), you can duplicate code, repeat configurations, and build templates efficiently.
Replace mode (R) handles overwriting scenarios perfectly—updating fixed-width fields, correcting values, and replacing text of known length.
Settings (:set) customize VIM to work how you think, particularly making searches powerful and forgiving.
Together with everything you’ve learned:
- Navigation (
hjkl,gg,G,/,?) - Deletion (
x,dw,dd,d$) - Change (
cw,ce,c$,cc) - Paste (
p,P) - Undo (
u,Ctrl-R) - Search (
/,?,n,N) - Substitution (
:s)
You now have complete VIM proficiency for bioinformatics work.
Quick Reference
OPEN LINES:
o - open line below, enter Insert mode
O - open line above, enter Insert mode
INSERTION:
a - insert after cursor
A - insert at end of line
MOTION:
e - move to end of word
YANK (COPY):
yy - yank line
3yy - yank 3 lines
yw - yank word
y$ - yank to end of line
V [move] y - yank visual selection
REPLACE:
R - enter Replace mode
[ESC] - exit Replace mode
SETTINGS:
:set ic - ignore case in search
:set noic - case-sensitive search
:set is - incremental search
:set hls - highlight search matches
:noh - clear search highlights
:set number - show line numbers
:set mouse=a - enable mouse
What You’ve Mastered
You now know how to:
- Open new lines efficiently above or below
- Insert text after cursor or at end of line
- Navigate to word endings
- Copy (yank) text without deleting it
- Paste yanked or deleted text
- Overwrite text with Replace mode
- Customize VIM settings for better searching
- Toggle settings on and off
These commands complete your essential VIM toolkit. You can now handle any editing task efficiently.
What’s Next
You’ve mastered the core VIM commands! Future topics include:
- Advanced customization with
.vimrc - Working with multiple files and splits
- Macros for repeating complex edits
- Advanced registers and clipboard integration
- Plugins for extended functionality
- Language-specific features
But you don’t need these to be productive. Practice what you’ve learned:
- Use
oandOfor all new lines - Yank and paste instead of manual retyping
- Enable search settings that work for you
- Use Replace mode when appropriate
With these commands internalized, VIM becomes an extension of your thought process. You think “copy this function” and your fingers execute Vyp automatically. You think “add a line here” and o happens without conscious effort.
This is VIM mastery: editing at the speed of thought.
Stay tuned for the next post!
Congratulations—you’ve almost completed essential VIM training. You’re now equipped to edit configuration files, scripts, and data on any remote server with confidence and efficiency. Keep practicing, and VIM will become your most powerful bioinformatics tool.
Comments