Execute JavaScript/TypeScript code directly from your Neovim buffer and see the results instantly
- ✅ Execute JavaScript/TypeScript code directly from your buffer
- ✅ View results as comments or in a separate window
- ✅ Built-in timeout protection for long-running code
- ✅ Seamless integration with your Neovim workflow
- Neovim 0.9.5 or later
- Node.js installation
- nvim-treesitter with appropriate parser installation
- Install language parsers using
:TSInstall javascriptor:TSInstall typescript
- Install language parsers using
- Typescript installation
- ts-node
Using lazy.nvim
return {
"tbsklg/nvim-exec",
branch = "main",
dependencies = {
'nvim-treesitter/nvim-treesitter',
},
config = function()
local nvim_exec = require("nvim-exec").setup({
timeout = 10000,
output_mode = "window",
})
vim.keymap.set({ "n", "v" }, "<leader>r", function()
nvim_exec.run()
end, { desc = "Execute code" })
end,
ft = { "javascript", "typescript" },
}- Write your JavaScript/TypeScript code in a buffer
- Add a comment with code to execute (e.g.
// fibs(10)) - Place your cursor on the comment line
- Execute
:ExecCodeor use your configured keybinding (e.g.<leader>r) - See results appear below as comments or in a window
// fibs(10)
// [
// 1, 1, 2, 3, 5,
// 8, 13, 21, 34, 55
// ]
//
const fibs = (n) => {
const go = (n) => {
if (n < 2) return 1;
return go(n - 1) + go(n - 2);
}
return Array.from({ length: n }).reduce((acc, _, i) => [...acc, go(i)], []);
}The plugin automatically terminates code execution that takes longer than the configured timeout (default: 10 seconds).
// fibs(100) // Using the inefficient implementation
// Job timed out
//require("nvim-exec").setup({
timeout = 10000, -- Timeout in milliseconds (default: 10000)
output_mode = "window" -- "comment" or "window" (default: "window")
})| Option | Description | Default |
|---|---|---|
timeout |
Maximum execution time in milliseconds | 10000 |
output_mode |
Where to display results ("comment" or "window") |
"window" |
- Lua and Neovim development environment
- Make
make lint # Run linting
make fmt # Run formatting
make test # Run tests