Search code examples
zshwindows-subsystem-for-linuxconemu

Running a zsh alias with WSL in a ConEmu startup task


My current WSL2 + ConEmu + bash/zsh setup works as expected.

I have some aliases set up in my .zshrc:

//.zshrc
alias mycommand1="[does some stuff]"
alias mycommand2="[does other stuff]"

What I want to achieve is to have a ConEmu startup Task that would run mycommand1 and mycommand2 in two separate tabs, and then leave these tabs open.

This requires ConEmu to load up WSL+bash+zsh, and then run the command.

As per the docs, my default Task currently runs native wsl.exe:

// {Bash::bash} Task commands
%windir%\system32\wsl.exe -cur_console:pm:/mnt --distribution Ubuntu-20.04

And, after carefully reading all the docs (1, 2, 3) and spending a fair amount of time on fiddling with params, I was only able to produce following attempts:

%windir%\system32\wsl.exe --distribution Ubuntu-20.04 -new_console:pm:/mnt -- ls
// logs my Windows User directory and prompts "Press Enter or Esc to exit..."
%windir%\system32\wsl.exe --distribution Ubuntu-20.04 -new_console:pm:/mnt -- mycommand1
// logs "zsh:1: command not found: mycommand1" and prompts "Press Enter or Esc to exit..."

I would appreciate some pointers:

  • How can I pass a command from wsl.exe that will be run from within the Ubuntu context, with all my bash/zsh configs?
  • How can I do that from ConEmu Task, ensuring that after running the command the tabs stay open with a regular zsh shell prompt?
  • Is there anything else I need to know to solve my problem?

Solution

  • Adding this as a second answer in case it works for you (and it very well may).

    My (very long) other answer assumes that you need the alias to run in the same subshell instance that continues to run. If you are okay with the alias running, then exec'ing a new shell, then this answer will be much simpler.

    Just create a conemu_start.txt (or whatever you want to call it, wherever you want to place it), with the following:

    >%windir%\system32\wsl.exe -cur_console:t:"MyCommand1" -cur_console:pm:/mnt ~ --distribution Ubuntu-20.04 --exec zsh -li -c "mycommand1; zsh -li"
    >%windir%\system32\wsl.exe -cur_console:t:"MyCommand2" -cur_console:pm:/mnt ~ --distribution Ubuntu-20.04 --exec zsh -li -c "mycommand2; zsh -li"
    

    As with the other answer, in ComEmu, set Settings -> Startup -> Tasks file to this file.

    That's it. The -li will cause the shells to be login (which processes .zprofile) and interactive (which processes .zshrc). That way, the aliases get defined, executed, and then another subshell is run to keep the ConEmu tab open.