Micro is a modern and intuitive terminal-based text editor, written in Go. Micro supports plugins, which are also written in Go. To create a plugin for Micro that displays "Hello world" when the editor starts, you'll need to write a small Go program and place it in the appropriate directory for Micro to recognize it.
Here is a step-by-step guide to creating the plugin:
Set up your Go environment if you haven't already. You can find instructions on the Go website: https://golang.org/doc/install
Create a new directory for your plugin. Micro plugins are typically stored in ~/.config/micro/plug. You can name your plugin directory helloworld.
bash
mkdir -p ~/.config/micro/plug/helloworld
cd ~/.config/micro/plug/helloworld
Create a Go file for your plugin. You can name it helloworld.go.
bash
touch helloworld.go
Edit the helloworld.go file with the following Go code:
go
// helloworld.go
package main
import (
"github.com/zyedidia/micro/v2/cmd/micro/microplugin"
lua "github.com/yuin/gopher-lua"
)
// init is called when the micro editor starts up.
func init() {
// When the editor initializes, this function will register the Lua function 'helloWorld'.
microplugin.RegisterOnInit(func(L *lua.LState) {
L.SetGlobal("helloWorld", L.NewFunction(func(L *lua.LState) int {
// This Lua function prints "Hello world" to the message buffer.
microplugin.Message("Hello world")
return 0 // Number of return values.
}))
})
}
func main() {}
Compile your plugin. Micro has its own build system for compiling plugins which can be used by running make in the micro source directory, but since this is a simple plugin you can compile it with the go build command.
bash
go build -buildmode=plugin helloworld.go
Now you should have a helloworld.so file in your plugin directory. This is the compiled plugin that Micro will load.
To tell Micro to run your helloWorld function when it starts up, you will need to add a little bit of Lua code to your ~/.config/micro/init.lua file.
lua
-- init.lua
-- Check if the helloWorld function exists (meaning your plugin is loaded).
if helloWorld then
helloWorld()
end
Start Micro, and you should see the message "Hello world" in the status line at the bottom of the editor.
Keep in mind that the Micro editor's plugin API can change, so if you're using a version of Micro that's different from the one this code was written for, you may need to adjust the code to match the current plugin API. The example code provided is based on the plugin API of Micro around the time of the knowledge cutoff in early 2023. Be sure to check the Micro documentation for the most up-to-date information on writing plugins: https://github.com/zyedidia/micro/tree/master/runtime/help/plugins.md