Replua.nvim – an Emacs-style scratch buffer for executing Lua

2 weeks ago 2

An Emacs-style scratch buffer for executing Lua inside Neovim. replua.nvim opens a dedicated buffer where you can experiment with Lua, call any Neovim API, and see results printed inline -- much like the classic Emacs Lisp interaction mode.

  • Opens a scratch buffer (replua://scratch) with Neovim APIs and the current global environment available.
  • Evaluate the current line, surrounding block, or the whole buffer.
  • Captures both returned values and print() output, appending results as Lua comments.
  • Each scratch buffer owns its own Lua environment. Close a buffer (or open a new one with :RepluaOpen!) to start from a clean slate without affecting other replua instances.
-- lazy.nvim example { "mghaight/replua.nvim", config = function() require("replua").setup() end, }

If you prefer to manage configuration manually, require the plugin somewhere in your startup files:

require("replua").setup()

The bundled plugin/replua.lua file calls setup() on load, so the commands are available even without manual configuration.

Open the scratch buffer with:

Need another scratch buffer? Run :RepluaOpen! for a fresh instance.

The default keymaps inside the buffer mirror Emacs-style interactions:

Mapping Mode Action
<localleader>e n Evaluate the current line
<localleader><CR> n Evaluate the surrounding block
<localleader>r n Evaluate the entire scratch buffer

Each evaluation appends comment lines such as -- => result or -- print: output, and drops you onto a new blank line ready for more Lua.

Additional commands:

  • :RepluaEval — Evaluate the entire scratch buffer.
  • :RepluaOpen! — Create a new replua buffer with a fresh environment.
  • :RepluaReset — Reset the Lua environment used for evaluation.

Because the environment proxies _G, anything you define becomes available to Neovim instantly. For example:

vim.api.nvim_set_option_value("number", true, { scope = "local", win = 0 }) -- => nil

Customize behaviour through setup():

-- example configuration require("replua").setup({ open_command = "botright 15split", keymaps = { eval_line = "<leader>rl", eval_block = nil, -- disable eval_buffer = "<leader>ra", }, intro_lines = { "-- replua.nvim", "-- Custom scratch buffer - happy hacking!", "", }, print_prefix = "-- -> ", result_prefix = "-- => ", newline_after_result = true, persist_env = true, })

Any option may be omitted to keep the defaults. Tables are merged, so redefining a single keymap leaves the others untouched.

Read Entire Article