Perform bulk code refactoring operations like renaming variables/functions across files, replacing patterns, and updating API calls. Use when users request renaming identifiers, replacing deprecated code patterns, updating method calls, or making consistent changes across multiple locations. Activates on phrases like "rename all instances", "replace X with Y everywhere", "refactor to use", or "update all calls to".
Perform systematic code refactoring operations across files using pattern-based search and replace. This skill focuses on bulk transformations that maintain code functionality while improving structure, naming, or updating APIs.
Activate this skill when users request:
getUserData to fetchUserData everywhere"var declarations with let or const"Activation phrases:
Before refactoring, locate all instances of the pattern to understand scope and impact.
Search for exact matches:
Grep(pattern="getUserData", output_mode="files_with_matches")
Search with context to verify usage:
Grep(pattern="getUserData", output_mode="content", -n=true, -B=2, -A=2)
Case-insensitive search:
Grep(pattern="getuserdata", -i=true, output_mode="files_with_matches")
Regex patterns:
Grep(pattern="get\\w+Data", output_mode="content")
Use the Edit tool with replace_all=true for bulk replacements:
Simple replacement:
Edit(
file_path="src/api.js",
old_string="getUserData",
new_string="fetchUserData",
replace_all=true
)
Multi-line replacement:
Edit(
file_path="src/auth.js",
old_string="function authenticate(user) {\\n return user.valid;\\n}",
new_string="async function authenticate(user) {\\n return await validateUser(user);\\n}",
replace_all=true
)
For single occurrences or when context matters, use Edit without replace_all:
Edit(
file_path="src/config.js",
old_string="const API_URL = 'http://old-api.com'",
new_string="const API_URL = 'https://new-api.com'"
)
User: "Rename getUserData to fetchUserData everywhere"
Workflow:
Find all occurrences to understand scope:
Grep(pattern="getUserData", output_mode="files_with_matches")
Preview changes by viewing context:
Grep(pattern="getUserData", output_mode="content", -n=true, -B=1, -A=1)
Inform user of scope: "Found 15 occurrences in 5 files"
Replace in each file:
Edit(file_path="src/api.js", old_string="getUserData", new_string="fetchUserData", replace_all=true)
Edit(file_path="src/utils.js", old_string="getUserData", new_string="fetchUserData", replace_all=true)
# ... for each file
Verify changes: Re-run Grep to confirm all instances were replaced
Suggest testing: Recommend running tests to ensure refactoring didn't break functionality
User: "Replace all var declarations with let or const"
Workflow:
Find all var declarations:
Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true)
Analyze each occurrence to determine whether to use let or const:
letconstReplace systematically:
Edit(file_path="src/index.js", old_string="var count = 0", new_string="let count = 0", replace_all=false)
Edit(file_path="src/index.js", old_string="var MAX_SIZE = 100", new_string="const MAX_SIZE = 100", replace_all=false)
Use linter if available to verify syntax:
npm run lint
User: "Update all authentication API calls to use the new endpoint"
Workflow:
Find all API calls:
Grep(pattern="/api/auth/login", output_mode="content", -n=true)
Identify variation patterns:
Replace with updated endpoint:
Edit(
file_path="src/auth.js",
old_string="fetch('/api/auth/login', {",
new_string="fetch('/api/v2/authentication/login', {",
replace_all=true
)
Update any response handling if API contract changed
Recommend integration tests
User: "Refactor this file to use async/await instead of callbacks"
Workflow:
Analyze callback patterns:
Grep(pattern="function\\s*\\([^)]*\\)\\s*{", output_mode="content", -n=true)
Identify callback functions (typically with callback, cb, or done parameters)
Transform each function:
Edit(
file_path="src/data.js",
old_string="function loadData(callback) {\\n db.query('SELECT *', callback);\\n}",
new_string="async function loadData() {\\n return await db.query('SELECT *');\\n}"
)
Update call sites to use await:
Edit(
file_path="src/app.js",
old_string="loadData((err, data) => {\\n if (err) throw err;\\n process(data);\\n});",
new_string="const data = await loadData();\\nprocess(data);"
)
Add try-catch for error handling where callbacks had error parameters
User: "Convert all relative imports to absolute imports"
Workflow:
Find all relative imports:
Grep(pattern="from ['\"]\\.\\.?/", output_mode="content", -n=true)
Calculate absolute path for each import based on project structure
Replace each import:
Edit(
file_path="src/components/Button.jsx",
old_string="import { theme } from '../../utils/theme'",
new_string="import { theme } from '@/utils/theme'",
replace_all=true
)
Verify module resolution works with new paths
Before executing refactoring:
The refactoring workflow should be:
String literals and comments:
Exported APIs:
Case sensitivity:
-i flag in Grep for case-insensitive when appropriateAfter refactoring:
Issue: "Replacement created syntax errors"
Issue: "Not all instances were replaced"
Issue: "Replaced instances in comments/strings unintentionally"
Issue: "Replace broke tests"
Required:
file_path: File to modifyold_string: Exact string to findnew_string: Replacement stringOptional:
replace_all: Boolean (default: false)
true: Replace all occurrences in filefalse: Replace only first occurrence (or fail if multiple matches)Important notes:
old_string must match EXACTLY (including whitespace, quotes)replace_all=true, Edit will failreplace_all=true for bulk refactoringUseful Grep options for refactoring:
-n=true: Show line numbers (helps locate changes)-B=N, -A=N: Show context (verify match is correct)-i=true: Case-insensitive (find variations)output_mode="content": See actual codeoutput_mode="count": Count occurrences per filetype: Filter by file type (e.g., type="py")Common refactoring patterns:
var → let/constCommon refactoring patterns:
% formatting → .format() or f-strings.get() with defaultsUniversal refactoring patterns: