20

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.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
novice
  • 285
  • 1
  • 2
  • 9
  • 1
    As far as I understand, Windows Bash does not support X Server. However, I a combination of Windows Bash + either [XMing](https://channel9.msdn.com/Blogs/WinCoder/XMING--Bash-on-Ubuntu-on-Windows--X11-Window-System-Running-from-Windows10-Subsystem-for-Linux) or MobaXTerm may work for you. – Yuri Steinschreiber Aug 27 '16 at 17:38
  • 2
    vcXsrv is highly recommended over the free version of Xming. I'm not sure if the donation-ware version of Xming matches it in compatibility, but the free Xming is very old and dated. – Harry Gindi Aug 30 '16 at 09:23

3 Answers3

38

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.

Yuri Steinschreiber
  • 2,648
  • 2
  • 12
  • 19
  • 2
    This worked! However, I have 2 small complaints: The emacs window is incredibly small and I don't know how to change the size of the display (resizing the window doesn't actually change the size of the display; i just get big white borders). Also, whenever I click a link, a lot of lines in the editor become white and it doesn't go away. http://tinypic.com/r/27xi5bn/9 – novice Aug 29 '16 at 02:47
  • I have the same behavior. I am checking if building emacs with Gtk2.0 instead of default Gtk3.0 helps. The reason I think it might help is gvim works better on Windows bash with MobaXterm, and the only substantial difference I've found is the Gtk version of the build. Will update as soon as I know if it helps. – Yuri Steinschreiber Aug 29 '16 at 03:03
  • 1
    In the meantime, you can work around the first problem by using the geometry command option, like `emacs -g 200x50`. Unfortunately, it doesn't work for `emacsclient`. – Yuri Steinschreiber Aug 29 '16 at 03:06
  • Thanks yet again! Also, is there some way to have emacs centered and focused on when it starts on my screen? I have to click the window to actually start typing in it. And do I always have to call "export DISPLAY-localhost:0.0" everytime I reopen the bash shell in order to use emacs? – novice Aug 29 '16 at 03:55
  • 1
    Put the `export` command in your `.bashrc` file. The focus part is extremely tricky, I've tried it and the gist of it is - it's nearly impossible to do. – Yuri Steinschreiber Aug 29 '16 at 03:57
  • I tried putting the export command in the `.bashrc` file, but it wouldn't work. Is there a particular place I need to put it? – novice Aug 29 '16 at 19:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122141/discussion-between-yuri-steinschreiber-and-novice). – Yuri Steinschreiber Aug 29 '16 at 19:58
  • @novice - I have checked with MobaXTerm version 9.1, white lines after mouse click don't appear any longer. Also tested with vcSrv and XMing (paid version) - they all work the same. Windows maximize doesn't work across all of them. Any update - will let you know. – Yuri Steinschreiber Sep 18 '16 at 00:40
  • was there ever a solution found to the window resizing issue? – Baywatch Nov 12 '16 at 07:57
  • @Baywatch - no, still not. I've seen through a few updates already, of both Windows and emacs (on 26 already,) still the same. – Yuri Steinschreiber Nov 14 '16 at 19:17
  • For me, DISPLAY=localhost:0:0 did not work - I had to set DISPLAY=:0 . Appreciate the solution! – hacoo Jan 26 '17 at 01:36
  • there is a problem with gtk 3.22 and vcxsrv. you have to actually downgrade vcxsrv like 4 releases down to solve the problem. Or stick to gtk 3.14 and use the latest vcxsrv version. – ninrod Nov 01 '17 at 21:37
  • 1
    The `(setq frame-resize-pixelwise t)` worked like a champ. I'm running VcXsrv with Ubuntu on WSL2, and this issue was driving me nuts. Thank you. And thanks to Jarett who doesn't seem to be a user anymore. – theherk Nov 18 '20 at 16:01
7

Thanks for @Yuri's nice answer, it works! After some further try&errors I found a way to fix the white border issue. You will need to rebuild from Emacs v24.5 source code (I tried some v25 versions in the git repository but all failed, if you are using git repository, checkout tag "emacs-24.5-rc3"). During the configuration stage, you will need to choose "lucid" (or "athena", same thing) x-toolkit instead of the current "gtk3". Notice that the "motif" has the same problem.

First, download Emacs source code and install "libxaw7-dev" package. The "libncurses5-dev" is just in case you don't have it. Assume we do everything in the directory ~/build-emacs24:

mkdir ~/build-emacs24
cd ~/build-emacs24
wget https://ftp.gnu.org/gnu/emacs/emacs-24.5.tar.xz
sudo apt-get install libxaw7-dev libncurses5-dev
tar xvf emacs-24.5.tar.xz

Now we have "emacs-24.5" directory extracted. In order to prevent the D-BUS issue reported elsewhere let's configure without it, and installed it in /usr/local/bin (by default):

cd emacs-24.5
./configure --without-dbus --without-gconf --without-gsettings --with-x-toolkit=athena 

Now, we are almost ready to build Emacs. But before that, we need to tweak the kernel a bit or the build will soon fail. (Let's skip the details, if you are interested, it's explained in the "etc/PROBLEMS" in your emacs source code.) You only need this during the build stage. Once your new emacs installed you don't need to do this again.

sudo sh -c "echo 0 > /proc/sys/kernel/randomize_va_space"

Now let's go into the long build process, you might experience an overall system slowdown during the build, you can reduce the number of threads used in the build (here I use "4" for a 2 cores 4-threads Intel CPU, don't use a number larger than supported threads (cat /proc/cpuinfo) or it will become even slower.)

time nice make -j 4

Takes only a few minutes? More to come, now install it (remember the "sudo" here):

sudo make install 

The whole build and install takes less than 10 minutes on my machine, not too bad. Now we have a working Emacs 24.5 located at "/usr/local/bin/emacs". If you have everything setup as @Yuri's answer, you should be able to launch it now:

/usr/local/bin/emacs &

Now you can maximize it and do things you want. Here is a screen shot, noticed the upper right corner, it's maximized:

enter image description here

Don't worry about the colors, it's my own theme selection.

There is still one more problem. Let's again workaround it. Sometimes you selected a menu item and the "white border" attacks again!

enter image description here

It's now smaller but still annoying.

enter image description here

Fortunately this time we can let it go away. Maximize/restore the window and it will go away. After a few rounds it sometimes stop happening again, but not always. This is still annoying. Since resizing the window can fix this issue, why don't we make it automatic? Maybe a not-too-bad workaround?

Now edit your "~/.emacs" file and add the following code (: use your new Emacs, of course ;) :

(defvar fix-frame-white-border-last-check 0)

(defun current-sec ()
  (let ((currtime (current-time)))
    (logior (lsh (car currtime) 16) (cadr currtime))))

(defun fix-frame-white-border-uow (&optional frame)
  (when (> (- (current-sec) fix-frame-white-border-last-check) 2)
    (setf frame (or frame (selected-frame)))
    (let ((width  (frame-width frame))
          (height (frame-height frame)))
      (set-frame-size frame (1- width) height)
      (set-frame-size frame width height))
    (setq fix-frame-white-border-last-check (current-sec))))

(advice-add 'menu-bar-open :after #'fix-frame-white-border-uow)
(add-hook 'menu-bar-update-hook #'fix-frame-white-border-uow)

The code is not perfect, but it works. It will resize your frame a bit and restore it right away when you use the menu. It also prevent itself from frequent resizing.

Luke Lee
  • 261
  • 3
  • 11
  • Comment by [Chihiro Fukazawa](http://stackoverflow.com/users/7388121/chihiro-fukazawa) suggests that after successful Emacs build, `/proc/sys/kernel/randomize_va_space` should be reverted to its original value – Antti Haapala -- Слава Україні Jan 07 '17 at 16:58
  • Actually `/proc/sys/kernel/randomize_va_space` will be restored to its original value once you exited the root bash (and all its child process). Next time you run bash you can see it restored to "2". – Luke Lee Jan 09 '17 at 02:57
  • this is really awesome; how did you get fonts to work? I've installed exactly per these instructions, but emacs can't see any of my system fonts. neither windows fonts, nor fonts that `fclist` shows, which makes emacs waaaaay ugly for me. – hal3 Nov 10 '17 at 14:23
1

The default emacs will work with Xming and other options. The issue is that the default alternative points to the emacs24-nox command. You need to install 'emacs24' instead, which removes emacs24-nox.

$ sudo update-alternatives --query emacs
Name: emacs
Link: /usr/bin/emacs
....
Status: auto
Best: /usr/bin/emacs24-nox
Value: /usr/bin/emacs24-nox

$ sudo apt install emacs24

$ sudo update-alternatives --query emacs
Name: emacs
Link: /usr/bin/emacs
...
Status: auto
Best: /usr/bin/emacs24-x
Value: /usr/bin/emacs24-x

Make sure that $DISPLAY is set correctly.

export DISPLAY=localhost:0.0
  • Thank you, that is a much easier solution then the one listed above, and I don't need to add any additional repositories. – Canageek Nov 29 '18 at 01:23