Do you think Emacs’s performance in windows is bad? Do you really want to use a native Speed Emacs in Windows? Do you hate the unnatural path transition between windows convention and Linux convention? Do you feel frustrated when you try to install and configure Emacs in WSL? Do you just want to taste the power of Emacs running in WSL 2?
This tutorial may help you :)
What is WSL?
Earlier on August 2, 2016, Microsoft released Windows Subsystem for Linux (WSL), enabling the native way to run Linux Tools in Windows 10 and Windows Server 2019.
In May 2019, WSL 2 was introduced, by importing the Real Linux Kernel through Hyper-V features (in a Virtual Machine Environment), providing the users with the full & immerse way to work with Linux under windows, with 20 times the read/write performances of WSL 1.
For Windows Emacs Users, here are some advantages/disadvantages for you to consider before switching the WSL 2:
Advantages
- The performance of Magit is way faster than the GNU compiled original windows Emacs-27 binaries.
- The Font Rendering is better.
- No flickering.
- interoperability between Windows and Linux.
- Native OneDrive support.
- Super fast boot up time for Emacs.
Disadvantages
- The network configuration is a pain, workaround is available.
- X11 may lost connection when network changes.
More Details
More details of the differences between WSL 1 and WSL 2, check https://docs.microsoft.com/en-us/windows/wsl/compare-versions.
Taste WSL 2
Install and enable WSL 2
Update Windows 10
The first step is to make sure you have updated to the latest Windows 10. For Windows 10 Versions 1903 & 1909 users, make sure the minor version number is 1049, according to Microsoft’s Devblog.
Install WSL 2
|
|
Enable WSL 2
|
|
Check WSL version
|
|
There are many people who may be not able to run the above commands, just make sure you have updated to the latest windows. https://devblogs.microsoft.com/commandline/wsl-2-support-is-coming-to-windows-10-versions-1903-and-1909/
Set default WSL version to 2
|
|
All later WSL Distros are installed will be WSL 2. No worries, you can switch between WSL 1 and WSL 2 with just one command without pain, just do it~
Install Ubuntu 18.04 in Microsoft Store
Optional: Convert WSL 1 to WSL 2
If you already installed some Distros before but they are in WSL 1, no worries, it is very easy to switch from WSL 1 to WSL 2:
|
|
Choosing the terminal
The first step to talk to WSL 2 is via Terminal Applications. In macOS, we have iTerm2, how about Windows? You may wonder we could just use the default Ubuntu Terminal, but it lacks UTF8 and Unicode supports, and only a few configuration options. Apparently, it is not the best option. We just need a more powerful and modern terminal. Luckily, Microsoft brings us another great product - Windows Terminal, and it is ranked with 67.6k stars in GitHub up to the time this article composed.
Windows Terminal has the following benefits compared with other terminals:
- Windows Terminal is multi-tabs and multi-panels.
- UTF8 & Unicode support.
- Supports Command Prompt, PowerShell, and WSL.
- GPU accelerated Text Rendering Engine.
- Custom Themes, styles and configurations.
- Modern and user-friendly.
- Open Source and Official Support.
Install windows Terminal in Microsoft Store
Windows Terminal
Windows Terminal’s setting is implemented in a json file - settings.json
, every time you modify and save the file, it will take effects immediately, nice!
-
Add “cursorShape” and “fontFace” to the “defaults” section, it will apply on all tabs
1 2 3 4 5 6
"defaults": { // Put settings here that you want to apply to all profiles. "cursorShape": "filledBox" , "fontFace": "JetBrains Mono" }
-
Update the home directory
1 2 3 4 5 6 7 8
{ "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}", "hidden": false, "name": "Ubuntu-18.04", "source": "Windows.Terminal.Wsl", "startingDirectory": "//wsl$/Ubuntu-18.04/home/damonchan" }
-
Disable keyboard cursor blink
Disable globally in keyboard setting:
zsh
I like to use zsh which provides more enhancement and configuration options compared with Bash.
|
|
Fonts
Just create a soft link from Windows to Ubuntu:
|
|
OneDrive
Just link Windows OneDrive Root Directory to Ubuntu:
|
|
X11 Server (VcXsrv)
In this tutorial, we choose VcXsrv as X11 server.
Install VcXsrv
|
|
Configure VcXsrv
-
Start XLunch (VcXsrv)
-
Multiple windows -> Display number: -1 -> Next
-
Start no client
-
Tick Clipboard, Primary Selection, Native opengl, Disable access control (If you use
socat
to do X11 forwarding, Access control can be enabled, check Other Methods below) -> Next -
Save configuration PS:
- Press “Win + R” -> insert
shell:startup
-> Press Enter - Save to startup can start X11 server when system boots
- Press “Win + R” -> insert
-
Finish
-
-
allow both Private and Public network in windows firewall setting
Control Panel\System and Security\Windows Defender Firewall\Allowed apps
Disable /etc/resolv.conf
generation
We disable /etc/resolv.conf
generation, so that we can use custom name servers which points to google.
vi /etc/wsl.conf
|
|
Setup custom name servers and point to google
vi /etc/resolv.conf
|
|
Export DISPLAY and LIBGL_ALWAYS_INDIRECT settings to ~/.zshrc
vi ~/.zshrc
|
|
Other Methods
There is another way to enable the X11 forwarding through socat, check this github issue. It is more safe but has some performance lost.
HiDPI
-
Right click
XLaunch
(VcXsrv) -> Compatibility -> Change high DPI settings -> Tick Override high DPI scaling Behavior, Application. -
Add the following statement to
.zshrc
|
|
Bigger Cursor
Supposed VcXsrv is installed in C:\Program Files\VcXsrv\
,
-
Install big-cursor
1
sudo apt install big-cursor
-
Rename
C:\Program Files\VcXsrv\fonts\misc\cursor.pcf.gz
toC:\Program Files\VcXsrv\fonts\misc\cursor-small.pcf.gz
-
Copy
/usr/share/fonts/X11/misc/big-cursor.pcf.gz
from WSL to asC:\Program Files\VcXsrv\fonts\misc\cursor.pcf.gz
Setup Emacs
Let’s dive into the meat of Compiling and installing the latest Emacs 27.1!
install dependencies
|
|
Download, compile and install
|
|
Install doom
|
|
Start Emacs
|
|
Please notice:
- The first time to start Emacs may need some times (Every time the first time to start Emacs after system boots, it also needs some time at times), font setting, x11 checking, sound checking etc, please wait a moment. Normally it will finish within one or two minute.
- After refreshing font setting through
fc-cache -fv
, Emacs will take some time to configure the font setting, but it will only conduct one time. - The second time to start Emacs will resume to the normal startup time.
Fix WSL_INTEROP issue
When you start Emacs, it will create another interop
file, it make us can not start windows programs in Emacs, we can make it to use the one same interop
file with the terminal.
Add the following to .zshrc
|
|
Optional: Setup Python Development Environment
pyenv
|
|
mypyls
mspyls can be installed by typing M-x lsp-install-server RET mspyls
.
Optional: Setup Node.js Development Environment
node
Install nvm
|
|
Add to .zshrc
|
|
|
|
Optional: Setup Rust Development Environment
|
|
Optional: Install other useful packages
apt
With apt
|
|
pdf-tools
|
|
rga
|
|
Rime
-
Install and setup Rime
1 2 3 4 5 6 7 8 9 10
sudo apt install git build-essential cmake libboost-all-dev libgoogle-glog-dev libleveldb-dev libmarisa-dev libopencc-dev libyaml-cpp-dev libgtest-dev cd /usr/src/gtest sudo cmake CMakeLists.txt sudo make #copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder sudo cp *.a /usr/lib cd ~/.emacs.d/librime make sudo make install sudo apt-get install ibus-rime
PS: It is better to not share Rime User folder between windows and WSL, it may cause troubles.
In Emacs
1
M-x rime-compile-module
-
plum
1 2
curl -fsSL https://git.io/rime-install | bash # rime_dir="$HOME/.rime" bash rime-install
sdcv
|
|
telega
|
|
An .zshrc example
|
|
Export WSL (Backup WSL)
wsl ships with a –export option for users to do export the WSL distro to a tar file which can be imported to other machines:
|
|
An Emacs Shortcut
Create two file in desktop: Emacs.sh
and Emacs.bat
Emacs.sh
|
|
Emacs.bat
|
|
Double click Emacs.bat
to launch Emacs.
Some Emacs hacking ideas
It is very easy to use Emacs to interactive with Windows’s programs, such as browsing the URL with Chrome, open the PDF file with Acrobat Reader DC, open the current file with default program, launch explorer.exe
, etc. Here are some ideas:
Browser URL with default browser
|
|
If you a calibredb user, you can add the following advice to open the PDF/EPUB with windows default programs
|
|
Open the file with windows default programs or reveal it in explorer
|
|
|
|
References
- WSL 2 Support is coming to Windows 10 Versions 1903 and 1909 | Windows Comman…
- Ubuntu on WSL 2 Is Generally Available | Ubuntu
- https://github.com/microsoft/WSL/issues/5336
- https://en.wikipedia.org/wiki/Windows%5FSubsystem%5Ffor%5FLinux
- https://docs.microsoft.com/en-us/windows/wsl/wsl2-index
- https://docs.microsoft.com/en-us/windows/wsl/compare-versions
- https://github.com/microsoft/terminal/issues/1379
- https://github.com/microsoft/terminal
- https://docs.microsoft.com/en-us/windows/terminal/customize-settings/profile-settings
- https://stackoverflow.com/questions/61110603/how-to-set-up-working-x11-forwarding-on-wsl2
- https://superuser.com/questions/1196399/how-do-i-set-the-size-of-the-x-mouse-pointer-in-the-windows-subsystem-for-linux
- https://github.com/hubisan/emacs-wsl
- https://stackoverflow.com/questions/27022373/python3-importerror-no-module-named-ctypes-when-using-value-from-module-mul
- https://docs.microsoft.com/en-us/windows/nodejs/setup-on-wsl2
- https://github.com/microsoft/WSL/issues/5065
- https://docs.microsoft.com/en-us/windows/wsl/interop