Skip to main content
Glama

vim_buffer

Retrieve buffer contents with line numbers from Neovim for efficient text editing and code management.

Instructions

Get buffer contents with line numbers

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameNoOptional 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' }] }; } } );
  • 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(); } }
  • 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") },
  • 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); } }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bigcodegen/mcp-neovim-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server