Refactor Commands
Explore 4 curated refactor commands for building with AI coding agents — what each one does, when to use it, and how to add it to your workflow.
Four slash commands for changing the structure of code without changing what it does. /refactor improves the readability and structure of a file or function. /extract-function moves a region of code into a well-named function and updates the call site. /rename-symbol renames a symbol across the project and tells the real symbol apart from coincidental substring matches, which is where search-and-replace renames go wrong. /optimize-imports removes unused imports and orders the rest by your project's conventions, using the project's own import tool when one is configured.
All four edit files, so run them on a clean working tree and review the diff before you commit. Start small, with /extract-function or /rename-symbol for one well-defined change, and use /refactor when a whole file needs work. If tests cover the code, run them after each change: behavior should not move.
4 commands
Extract Function
Extract a code region into a well-named function and update the call site.
Optimize Imports
Remove unused imports and organize the rest in a file or directory per the project's conventions — preferring the project's own import tool where one is configured — without changing behavior.
Refactor
Refactor the target for readability and structure without changing behavior.
Rename Symbol
Safely rename a symbol project-wide, distinguishing the real symbol from coincidental substring matches.