vim_search_replace
Use this tool to find and replace text in Vim, supporting regex, case-insensitive searches, and optional confirmation for each replacement. Access global and precise editing workflows.
Instructions
Find and replace with global, case-insensitive, and confirm options
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| confirm | No | Whether to confirm each replacement (default: false) | |
| global | No | Replace all occurrences in each line (default: false) | |
| ignoreCase | No | Whether to ignore case in search (default: false) | |
| pattern | Yes | Search pattern (supports regex) | |
| replacement | Yes | Replacement text |
Implementation Reference
- src/index.ts:407-435 (registration)Registers the vim_search_replace MCP tool with name, description, Zod input schema, and async handler that delegates to NeovimManager.searchAndReplace.server.tool( "vim_search_replace", "Find and replace with global, case-insensitive, and confirm options", { pattern: z.string().describe("Search pattern (supports regex)"), replacement: z.string().describe("Replacement text"), global: z.boolean().optional().describe("Replace all occurrences in each line (default: false)"), ignoreCase: z.boolean().optional().describe("Whether to ignore case in search (default: false)"), confirm: z.boolean().optional().describe("Whether to confirm each replacement (default: false)") }, async ({ pattern, replacement, global = false, ignoreCase = false, confirm = false }) => { try { const result = await neovimManager.searchAndReplace(pattern, replacement, { global, ignoreCase, confirm }); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: error instanceof Error ? error.message : 'Error in search and replace' }] }; } } );
- src/index.ts:410-416 (schema)Zod schema defining input parameters for the vim_search_replace tool.{ pattern: z.string().describe("Search pattern (supports regex)"), replacement: z.string().describe("Replacement text"), global: z.boolean().optional().describe("Replace all occurrences in each line (default: false)"), ignoreCase: z.boolean().optional().describe("Whether to ignore case in search (default: false)"), confirm: z.boolean().optional().describe("Whether to confirm each replacement (default: false)") },
- src/neovim.ts:682-704 (handler)Implements the core search-and-replace functionality in NeovimManager by constructing and executing a substitute command with configurable flags (g, i, c).public async searchAndReplace(pattern: string, replacement: string, options: { global?: boolean; ignoreCase?: boolean; confirm?: boolean } = {}): Promise<string> { if (!pattern || pattern.trim().length === 0) { throw new NeovimValidationError('Search pattern cannot be empty'); } try { const nvim = await this.connect(); // Build substitute command let flags = ''; if (options.global) flags += 'g'; if (options.ignoreCase) flags += 'i'; if (options.confirm) flags += 'c'; const command = `%s/${pattern.replace(/\//g, '\\/')}/${replacement.replace(/\//g, '\\/')}/${flags}`; const result = await nvim.call('execute', [command]); return result ? String(result).trim() : 'Search and replace completed'; } catch (error) { console.error('Error in search and replace:', error); throw new NeovimCommandError(`substitute ${pattern} -> ${replacement}`, error instanceof Error ? error.message : 'Unknown error'); } }