line_edit
Modify specific lines in a file by number or range using actions like replace, delete, or insert. Ideal for precise edits without replacing entire files, enhancing efficiency in file management tasks.
Instructions
Edit specific lines by number or range
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action to perform | |
| content | No | New content (for replace/insert actions) | |
| file | Yes | File to edit | |
| lineNumber | No | Line number to edit (1-based) | |
| lineRange | No | Line range (e.g., "10,20" or "5,$") |
Implementation Reference
- src/index.ts:700-737 (handler)Handler implementation for line_edit tool. Uses sed commands based on action (replace, delete, insert_after, insert_before) to edit specific lines or ranges in a file, creating a .bak backup.case 'line_edit': { const { file, lineNumber, lineRange, action, content } = args; if (!existsSync(file)) { throw new Error(`File not found: ${file}`); } let sedCmd = 'sed -i.bak '; const range = lineRange || `${lineNumber}`; switch (action) { case 'replace': sedCmd += `'${range}s/.*/${content}/' '${file}'`; break; case 'delete': sedCmd += `'${range}d' '${file}'`; break; case 'insert_after': sedCmd += `'${range}a\\ ${content}' '${file}'`; break; case 'insert_before': sedCmd += `'${range}i\\ ${content}' '${file}'`; break; default: throw new Error(`Unknown action: ${action}`); } await execAsync(sedCmd); return { content: [{ type: 'text', text: `Successfully performed ${action} on line(s) ${range} in ${file}` }] }; }
- src/index.ts:144-174 (schema)Input schema definition for the line_edit tool, defining parameters like file, lineNumber/lineRange, action, and content.{ name: 'line_edit', description: 'Edit specific lines by number or range', inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'File to edit' }, lineNumber: { type: 'number', description: 'Line number to edit (1-based)' }, lineRange: { type: 'string', description: 'Line range (e.g., "10,20" or "5,$")' }, action: { type: 'string', enum: ['replace', 'delete', 'insert_after', 'insert_before'], description: 'Action to perform' }, content: { type: 'string', description: 'New content (for replace/insert actions)' } }, required: ['file', 'action'] } },
- src/index.ts:278-280 (registration)Part of the tools array registration where line_edit is included in server.setTools.] }; });