Add -exec CLI argument to execute commands when micro starts (#4132)

This argument allows us to execute one or more commands immediately
after editor initialization.
This is useful in cases where you know you are going to want to open
two panes immediately, or for scripting purposes so you can start
with certain things set up, or if you want to execute certain plugin
actions at startup, etc.

Usage:
	micro -exec "...cmd..."
	micro -exec "cmd1" -exec "cmd2" -exec "cmd3" ...

(multiple commands are gathered into an array and executed in order)
Commands are executed after plugin initialization, so you can use
plugin commands as well.

Updated man page with information about the new argument.
This commit is contained in:
Andrew Whitworth
2026-07-09 19:01:30 +02:00
committed by GitHub
parent ee8f6de484
commit de6b5b1768
2 changed files with 23 additions and 0 deletions
+6
View File
@@ -65,6 +65,12 @@ Enable CPU profiling (writes profile info to ./micro.prof so it can be analyzed
.RS 4
Show the version number and information and exit
.RE
.PP
.B \-exec
.I command
.RS 4
Run a command after the editor initializes (may be repeated)
.RE
Micro's plugins can be managed at the command line with the following commands.
.RS 4
+17
View File
@@ -29,6 +29,15 @@ import (
lua "github.com/yuin/gopher-lua"
)
// multiStringFlag is a flag.Value that accumulates multiple values.
type multiStringFlag []string
func (f *multiStringFlag) String() string { return "" }
func (f *multiStringFlag) Set(v string) error {
*f = append(*f, v)
return nil
}
var (
// Command line flags
flagVersion = flag.Bool("version", false, "Show the version number and information")
@@ -38,6 +47,7 @@ var (
flagProfile = flag.Bool("profile", false, "Enable CPU profiling (writes profile info to ./micro.prof)")
flagPlugin = flag.String("plugin", "", "Plugin command")
flagClean = flag.Bool("clean", false, "Clean configuration directory")
flagExec multiStringFlag
optionFlags map[string]*string
sighup chan os.Signal
@@ -68,6 +78,8 @@ func InitFlags() {
fmt.Println(" \tso it can be analyzed later with \"go tool pprof micro.prof\")")
fmt.Println("-version")
fmt.Println(" \tShow the version number and information and exit")
fmt.Println("-exec command")
fmt.Println(" \tRun a command after the editor initializes (may be repeated)")
fmt.Print("\nMicro's plugins can be managed at the command line with the following commands.\n")
fmt.Println("-plugin install [PLUGIN]...")
@@ -95,6 +107,7 @@ func InitFlags() {
for k, v := range config.DefaultAllSettings() {
optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'.", k, v))
}
flag.Var(&flagExec, "exec", "Run a command after the editor initializes (may be repeated)")
flag.Parse()
@@ -478,6 +491,10 @@ func main() {
// time out after 10ms
}
for _, cmd := range flagExec {
action.MainTab().CurPane().HandleCommand(cmd)
}
for {
DoEvent()
}