How to use X Windows with Emacs on Windows 10 Bash?

novice picture novice · Aug 27, 2016 · Viewed 23.1k times · Source

I am using the Bash on Ubuntu on Windows program to use Emacs for C++. Right now, I can code everything using the keyboard shortcuts, however, I want to select text with my mouse or set the mark with my mouse instead of always having to use my keyboard.

To get the X Windows System, I already did sudo apt-get install xserver -xorg but emacs still runs in a terminal editor.

I don't know what other commands to use or how to get X Windows to start running.

Answer

Yuri Steinschreiber picture Yuri Steinschreiber · Aug 28, 2016

Windows bash does not support X Server. However, using a Windows X Server port such as MobaXTerm, XMing or vcXsrv in conjunction with Windows bash works. Some things work out of the box (gvim did,) emacs requires more effort.

Here's how I've made it work:

  • Install MobaXterm. Most probably you can make it work with XMing as well, but I have tested only with MobaXterm. Update: Tested with vcXsrv and XMing (paid version.) Works essentially the same, I will probably stick with MobaXTerm.

  • Install emacs 25.1 snapshot. The reason: Ubuntu 14.04 comes with emacs 24.3, which doesn't work with Windows bash and MobaXTerm X server (no idea why, and it's too old anyway.)

Install the snapshot using the following commands:

sudo add-apt-repository ppa:ubuntu-elisp/ppa
sudo apt-get update
sudo apt-get install emacs-snapshot

Then run

sudo update-alternatives --config emacs

and select emacs-snapshot

Make sure you set the DISPLAY

export DISPLAY=localhost:0.0

Run MobaXTerm (you don't need anything to do in it, just start it - it will start X Server when needed.)

Run emacs in bash and enjoy.

One problem remains, and it persists across all three packages I've tested (MobaXTerm, XMing and vcXsrv). Emacs sticks to the initial frame size, and doesn't resize its frame when the window is resized. Same problem with the menu bar - when menu bar is disabled, emacs doesn't reclaim its space, it remains as a grey rectangle. This problem seems to be peculiar to emacs on Windows bash - gvim and other GUI programs seem to work fine. Also, running emacs 25.1 on Linux using any X Server on Windows works fine. I have tried to build emacs with different GUI settings (GTK 2.0 etc.) but emacs make on Windows bash gets stuck. If I find a workaround for this I will post an update.

Update 09/24/2017 According to Jarett the following in your init.el should fix the frame resizing problem:

(setq frame-resize-pixelwise t)

I don't have Windows with bash active anymore, so I can't test it.