I have lately found the correct configuration for using Xming to get gvim.exe started from my WSL (Windows Subon Windows, and I wonder if I can automate the procedure. At the end of the day, I would like to replicate the following mapping that fires gvim.exe in WSL together with the parameter. (The short command to issue is gvim +SLoad

    ShellRun("C:\vim\vim80\gvim.exe", "+SLoad", "", "", 3)

Step 1: get things to work with cmd.exe

According to the guide to invoke wsl, I should be able to start GUI programs normally, by feeding in the -c flag as follows:

bash -c gvim
# Or, wsl does not need the flag, thus:
wsl gvim

Yet, per the two commands, I get similar error as if I did not set up the $DISPLAY varaible when I was with the WSL comamnd-line.

enter image description here

Step 2: reviewing the correct configuration from WSL

Depending on where the GUI program is fired, one needs to drop the following line to the correct configuration file. For the default bash shell, put it in ~/.bashrc. For Fish shell, put it in ~/.config/fish/config.fish.

# XMing config
export DISPLAY=:0 

The value of the $DISPLAY variable is set according to how Xming specifies its own starting parameters. Popping open one that got added to the Start Menu reveals the settings: "C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow.

Question: how may I set up the environmental variables for cmd?

  • This answer shows how to create a command file which runs each time cmd is started, before any command is executed or prompted for, allowing you to preset any environment variables, among other things. – AFH Jan 21 at 19:42
  • Try using bash -c "DISPLAY=:0 gvim". – harrymc Jan 21 at 19:55
  • @harrymc, Amazing! the flag worked great. Could you submit an answer? Let me know if you want me to post it. – llinfeng Jan 21 at 23:42

To ensure the environment variable is truly passed to the program, use:

bash -c "DISPLAY=:0 gvim"

The shell syntax VARIABLE=VALUE COMMAND sets the environment variable VARIABLE for the duration of the specified command only. It is roughly equivalent to (export VARIABLE=VALUE; exec COMMAND).

Displays are numbered from 0, so :0 is the first local display that was started.

Like all environment variables, DISPLAY is inherited from parent process to child process. However, when started from WSL, there are apparently some glitches.

  • Thanks a lot! I am appending your post with a working version of AHK script, that summons WSL-driven Gvim. – llinfeng Jan 22 at 14:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.