micro icon indicating copy to clipboard operation
micro copied to clipboard

Lua Error: function does not exist

Open wf5w opened this issue 3 weeks ago • 3 comments

In my .config/micro/ I have two files:

init.lua:

function addTextAtTop(buffer, text) buffer:Start() -- Move cursor to the beginning of the file buffer:Insert(text) -- Insert the specified text end

and in my bindings.json I have:

{ "Ctrl-t": "lua:addTextAtTop(buffer, "Your text here\n")" }

I want to be able to insert Your text here\n at the top of the current buffer, by pressing Ctrl-t

But when create a file with Micro I get: Lua Error: addTextAtTop(buffer does not exist

What am I not doing correctly? I am a noob when it comes to Micro.

Thanks for your help,

Jerry

wf5w avatar Dec 04 '25 18:12 wf5w

You can refer to a function in init.lua as "lua:initlua.yourFunctionName" in bindings.json. It will be called with the current BufPane from which the function was called from as the first parameter. I don't think there's a way to pass custom parameters to functions from bindings.json.

Here's a complete example:

~/.config/micro/init.lua

local buffer = import("micro/buffer")

function addHashBang(bp)
    bp.Buf:Insert(buffer.Loc(0, 0), "#!/usr/bin/env bash\n")
end

~/.config/micro/bindings.json

{
    "Ctrl-t": "lua:initlua.addHashBang"
}

Andriamanitra avatar Dec 04 '25 19:12 Andriamanitra

Andriamanitra,

Thank you so much for your timely response. I'm assuming that if the file was called something else other than init.lua (like myfunctions.lua and there was a function named myfuction1 in that file, that the key binding would be "lua:myfunctionslua.myfunction1"

Also, is there documentation on what is in say micro/buffer ? And whatever else there is?

Thanks, I think this will be the editor of choice for my students, other than nano.

Jerry

wf5w avatar Dec 04 '25 23:12 wf5w

I'm assuming that if the file was called something else other than init.lua (like myfunctions.lua and there was a function named myfuction1 in that file, that the key binding would be "lua:myfunctionslua.myfunction1"

init.lua is a bit of a special case, micro treats it as a plugin named "initlua". When you have Lua files in a plugin folder (eg. ~/.config/micro/plug/myplugin/) you would refer to functions in that plugin as "lua:myplugin.myfunction1" (only the plugin name matters, the filenames are irrelevant).

Also, is there documentation on what is in say micro/buffer ? And whatever else there is?

You can use micro's help command to browse the help pages, eg. help plugins (you can press Tab to use auto-completion to browse the available help pages).

Andriamanitra avatar Dec 05 '25 17:12 Andriamanitra