vim_buffer
Retrieve buffer contents with line numbers from Neovim for efficient text editing and code management.
Instructions
Get buffer contents with line numbers
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "vim_buffer"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | No | Optional file name to view a specific buffer |
Implementation Reference
- src/index.ts:74-97 (registration)Registration of the 'vim_buffer' MCP tool, including schema, description, and inline handler function that delegates to NeovimManager.getBufferContents and formats output.
"vim_buffer", "Get buffer contents with line numbers", { filename: z.string().optional().describe("Optional file name to view a specific buffer") }, async ({ filename }) => { try { const bufferContents = await neovimManager.getBufferContents(filename); return { content: [{ type: "text", text: Array.from(bufferContents.entries()) .map(([lineNum, lineText]) => `${lineNum}: ${lineText}`) .join('\n') }] }; } catch (error) { return { content: [{ type: "text", text: error instanceof Error ? error.message : 'Error getting buffer contents' }] }; } } ); - src/neovim.ts:108-149 (handler)Core handler logic in NeovimManager.getBufferContents: connects to Neovim instance, selects target buffer (by filename or current), retrieves all lines, and returns Map of line number to line content.
public async getBufferContents(filename?: string): Promise<Map<number, string>> { try { const nvim = await this.connect(); let buffer; if (filename) { // Find buffer by filename const buffers = await nvim.buffers; let targetBuffer = null; for (const buf of buffers) { const bufName = await buf.name; if (bufName === filename || bufName.endsWith(filename)) { targetBuffer = buf; break; } } if (!targetBuffer) { throw new NeovimValidationError(`Buffer not found: ${filename}`); } buffer = targetBuffer; } else { buffer = await nvim.buffer; } const lines = await buffer.lines; const lineMap = new Map<number, string>(); lines.forEach((line: string, index: number) => { lineMap.set(index + 1, line); }); return lineMap; } catch (error) { if (error instanceof NeovimValidationError) { throw error; } console.error('Error getting buffer contents:', error); return new Map(); } } - src/index.ts:76-76 (schema)Input schema using Zod: optional filename parameter to specify which buffer to retrieve.
{ filename: z.string().optional().describe("Optional file name to view a specific buffer") }, - src/neovim.ts:94-106 (helper)Helper method connect() used by getBufferContents to establish Neovim connection via socket.
private async connect(): Promise<Neovim> { const socketPath = process.env.NVIM_SOCKET_PATH || '/tmp/nvim'; this.validateSocketPath(socketPath); try { return attach({ socket: socketPath }); } catch (error) { console.error('Error connecting to Neovim:', error); throw new NeovimConnectionError(socketPath, error as Error); } }