From 2b9599d36c6b9cb0407bf66d6a0ca82ea2e09547 Mon Sep 17 00:00:00 2001 From: Whiteknight Date: Mon, 29 Jun 2026 09:39:13 -0400 Subject: [PATCH] Add -exec CLI argument to execute commands when micro starts 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. --- assets/packaging/micro.1 | 6 ++++++ cmd/micro/micro.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/assets/packaging/micro.1 b/assets/packaging/micro.1 index 61db841a08..f137092d8e 100644 --- a/assets/packaging/micro.1 +++ b/assets/packaging/micro.1 @@ -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 diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index c8a99b2517..44596a391b 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -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() }