A fast and robust Emacs setup
- Fast. Only one second to start up
- Robust. Never crash after package upgrade. Packages could be installed without network
- Support Windows. You can find&grep files on Windows with minimum setup
- Tested with Emacs 25.3, 26.1, 27 on Linux/Windows/Cygwin/macOS/WSL(Windows Subsystem for Linux)
- Emacs vanilla key bindings and directory layout is respected
- Usable in emacs-nox and works in PuTTY
Table of Content
- A fast and robust Emacs setup
- Tutorial (OPTIONAL)
- Locked packages
- How to install new packages?
- Git blame current line
- Save/Load windows layout
- Use this configuration as merge tool for Git
- Default terminal shell
- Override default setup
- Code navigation and auto-completion
- Use “m” key for “evil-matchit”
- Color theme
- True colors in terminal Emacs
- Grep/Replace text in project
- React and JSX
- Start a shell inside Emacs
- Setup fonts in GUI Emacs
- Synchronize setup with Git
- Editing Lisp
- Use smart-mode-line or powerline?
- Key bindings doesn’t work?
- macOS user?
- Customize global variables
- Open/Save files with Counsel/Ivy
- Non-English users
- Behind corporate firewall
- Network is blocked
- Cannot download packages?
- Use packages on GNU ELPA
- Disable Vim key bindings
- Evil setup
- C++ auto-completion doesn’t work?
- Auto-completion for other languages
- Chinese Input Method Editor
- Install multiple versions of Emacs
- Change Time Locale
- Directory structure
- Run the unit test before git commit
- Python environment
- Set up lsp-mode
- Support legacy Emacs versions
- Report bug
- Emacs 26.1 is strongly recommended. It is faster and easier to set up
- Please read FAQ to disable Vim key bindings
- Please read section “Key bindings” to learn key bindings
- If you use Windows Emacs, you have to install GnuTLS Dlls manually to install packages from remote repositories. Or else you got error message like
error: Package `async-' is unavailableon Windows. Please note since
Emacs 26.1, you can download full version which works out of box
- Most packages from MELPA are invisible but packages from MELPA Stable are visible. You can modify variable
lisp/init-elpa.elto install unstable packages. Please choose stable packages if possible. Even advanced user could under-estimate the risk of unstable packages
- Emacs 25 support will be dropped on January 2021. Check “Support legacy Emacs versions” if you use old versions
- Other issues are covered in FAQ
Most users should follow the section “Install in normal way”.
You need the section “Install stable version” if and only if,
- You don’t have network access
- You have never used any command line program
~ means Home directory first.
Install in normal way
There are two ways to install this setup (I recommend the first way):
First way is to download latest setup and extract its content into
~/.emacs.d, OR run command
cd ~; git clone https://github.com/redguardtoo/emacs.d.git .emacs.d in shell.
Second way is to use stable setup, or run command
cd ~; git clone https://github.com/redguardtoo/emacs.d.git .emacs.d; cd .emacs.d; git reset --hard stable in shell.
By default, packages are installed automatically during Emacs startup.
Someone reported that package repository http://melpa.org is not responsive in China.
No worries. You could answer “YES” to the question “Switch to faster package repositories in China temporarily?” when firing Emacs. Please note after startup you could change variable
init-elpa.el to permanently switch to Chinese repositories.
Install stable version (OPTIONAL)
- Download https://github.com/redguardtoo/emacs.d/archive/stable.zip
- Extract its content into empty directory
- Download https://github.com/redguardtoo/myelpa/archive/stable.zip
- Extract the zip somewhere, say
- Make sure a file named
- Uncomment the line containing “myelpa” in
~/myelpa/could be modified.
- Start Emacs now!
Now you are using local package repository
Third party programs (OPTIONAL)
Most command line programs can be installed through
default package manager.
Default package manager could be:
- apt-cyg at Cygwin
- homebrew at macOS
- any package manager at Linux (
emergeon Gentoo …)
Some package managers allow you to install the program for current user. For example, python package manager
pip has the option ”–user”.
List of programs
These programs are OPTIONAL.
Please ignore error message related to command line programs. For example, if
hunspell are not installed, you can ignore all the flyspell error messages.
fortune-zh or fortune
- Show ancient Chinese poem or quotes from random sources
Please note you can’t install
fortune-zh through homebrew on macOS but there is a simple workaround:
- install fortune
fortune-zhcode from https://github.com/debiancn/fortune-zh
- Extract code and run
makein its folder
- copy data files and the script named
fortune-zhinto corresponding folders which
fortuneis using. You may need edit the file
w3m (web browser)
- Required by
You can insert
(setq mm-text-html-renderer 'w3m) in
~/.gnus.el to force HTML mail be rendered by
w3m instead of the default HTML rendering engine
shr supports colored text while
w3m based utilities can open video/audio/image with the help of
- Required by
aspell or hunspell
- Required by
hunspellis the alternative of
aspell. You need only one of them.
aspell, you may need its dictionary
- Default language for spell check is “en_US” in
init-spelling.el. It can be changed
- I recommend
aspellfor programmers. Non-programmers find
hunspellis better on typo correction
If you use hunspell, check my article. Please note the hunspell executable understands either unix format path or windows format path but not both. You need figure out hunspell version you are using.
You can set
init-spelling.el if you prefer hunspell.
This setup use wucuo to spell check camel case words. It supports both aspell and hunspell. Run
M-x wucuo-start and
M-x flyspell-buffer to check camel case words.
Aspell’s default personal English dictionary is
$HOME/.aspell.en.pws. Its content is like:
personal_ws-1.1 en 4 ABN ACC ACN ACT
The content of personal dictionary can be cleaned by
Hunspell’s default personal English dictionary is
$HOME/.hunspell_en_US. The file format is same as aspell.
Flyspell configuration is actually simple. Aspell uses
ispell-extra-args. Hunspell uses
ispell-local-dictionary. That’s it.
- Required by
web-modefor syntax check.
- Required by
org-modeto export org file to odt contain images
zip and unzip
- Required by
org-modeto export org file to odt file
node.jsthrough OS package manager, then
npm install -g jshint
- Required by js-mode
- I use js2-mode which does NOT need jshint
xsel or xclip
- Required by my clipboard command
- Required by
- Please use out-of-source build tree when using CMake
- Required by
- If you use
cpputils-cmakewill do all the setup for you. You need not read next item! But please spend a few minutes to learn the basics of cmake! There is a one minute step-by-step-guide in README of cpputils-cmake to teach you how to use cmake.
- If you use
(setq company-clang-arguments '("-I/example1/dir" "-I/example2/dir"))into
- Required by
- Required by
- It creates tags file for code navigation and code completion
- Required by many tags related packages (
- See How to use ctags in Emacs effectively
- Required by counsel-gtags and
- It creates index files for code navigation and auto-completion
- Please read GNU Global manual about environment variables
- Only its executable
sofficeis used for converting odt file into doc/pdf
- Conversion happens automatically when exporting org-mode to odt
- The conversion command is defined in variable
- Install pip through OS package manager, then
pip install jsbeautifier
sdcv (console version of StarDict)
- Required by
curl http://pkgs.fedoraproject.org/repo/pkgs/stardict-dic/stardict-dictd_www.dict.org_wn-2.4.2.tar.bz2/f164dcb24b1084e1cfa2b1cb63d590e6/stardict-dictd_www.dict.org_wn-2.4.2.tar.bz2 | tar jx -C ~/.stardict/dicto install dictionary
- Optionally used by
M-x counsel-etags-grepto search text in files
curl https://sh.rustup.rs -sSf | shin shell to install Rust then
cargo install ripgrep
- Tweak environment variable
PATHso Emacs can find ripgrep
- Some dired commands use ffmpeg to process video/audio
It’s Grammar, Style and Spell Checker
- Download from https://languagetool.org/download/LanguageTool-stable.zip.
- Used by [[https://github.com/mhayashi1120/Emacs-langtool][Langtool]. Check its README for usage
- Check syntax of shell script
- Required by
Please install corresponding Chrome/Firefox addons.
- Used by hydra command in dired
Knowledge of Linux/Unix is required. At least you should know the meanings of “environment variable”, “shell”, “stdin”, “stdout”, “man”, “info”.
Please read this tutorial at least for once.
Step 1, learn OS basic
At minimum you need know how Emacs interacts with other command line programs,
Step 2, read official tutorial
C-h t in Emacs (“C” means Ctrl key, “M” means Alt key) to read bundled tutorial.
At minimum, you need learn:
- How to move cursor
C-h vto describe variable
C-h fto describe function
C-h kto describe command key binding
Step 3, know org-mode basics
Org-mode is for notes-keeping and planning.
Please watch Carsten Dominik’s talk. It’s really simple. The only hot key to remember is
Step 4, start from a real world problem
Required for vim user,
I avoid overriding the original setup of third party command line program.
On Windows, you need install Cygwin which provides command line programs to Emacs. Cygwin could be installed on any hard drive but it’s highly recommended don’t change it relative path to the root driver.
Install Ctags (Universal Ctags is better. Exuberant Ctags is fine). On Windows, you could install Ctags through Cygwin.
M-x counsel-etags-find-tag-at-point from
counsel-etags to navigate code. It uses tags files created by ctags. Tags file will be automatically created/update when you start using `counsel-etags`.
M-x counsel-etags-grep to search text (grep) in project. Project root is automatically detected.
M-x find-file-in-project-by-selected from
find-file-in-project to find file in project. Project root is automatically detected. You can also add one line setup in
(setq ffip-project-root "~/proj1/").
Code auto-completion works out of box by using tags file created by Ctags. You need run
counsel-etags at least once to fire up Ctags.
company-mode provides the candidates for auto completion. No manual setup is required.
Please grep in the directory
lisp if you have any further questions on setup.
Don’t memorize any key binding. Try =M-x any-command-in-emacs and key binding hint is displayed.
Most key bindings are defined in
lisp/init-evil.el, a few key in
lisp/init-hydra.el which uses Hydra.
C-c C-y anywhere to bring up default hydra menu.
The tutorials I recommended have enough information about commands.
Besides, ”How to be extremely efficient in Emacs” lists my frequently used commands.
kj to escape from
evil-insert-state and everything else in Emacs. It’s much more efficient than
ESC in Vim or
C-g in Emacs. Search
init-evil.el for details.
Some packages are so important to my workflow that they are locked.
Those packages are placed at
They will not be upgraded by package system.
Please make sure same package is not installed through elpa. Please check the content of directory “elpa/” in
How to install new packages?
I only use two package repositories,
- https://stable.melpa.org (holding stable packages)
- https://melpa.org (holding latest but unstable packages)
If a package named
MY-PKG exists in the stable repository, you only need one line setup,
This line could be placed in
You’d better place everything related to
~/.custom.el so the main stream change won’t impact your own configuration.
If the package does not exist in the stable repository, modify
I encourage you to read
init-elpa.el to understand how packages are managed if you are good at Emacs Lisp,
Git blame current line
If you select a region inside current line, the correct commit which submits the selected snippet is displayed instead of the latest commit touch the whole line.
Save/Load windows layout
SPC s s or
M-x wg-create-workgrou to save windows layout.
SPC l l or
M-x my-wg-switch-workgroup to load windows layout.
Use this configuration as merge tool for Git
This configuration might be the most efficient and most powerful merge tool for VCS.
Insert below configuration into
[mergetool.ediff] # use git mergetool ediff to resolve conflicts cmd = emacs -nw -Q --eval \"(setq startup-now t)\" -l \"~/.emacs.d/init.el\" --eval \"(progn (setq ediff-quit-hook 'kill-emacs) (if (file-readable-p \\\"$BASE\\\") (ediff-merge-files-with-ancestor \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$BASE\\\" nil \\\"$MERGED\\\") (ediff-merge-files \\\"$LOCAL\\\" \\\"$REMOTE\\\" nil \\\"$MERGED\\\")))\"
git mergetool -t ediff to resolve conflicts.
Default terminal shell
You can customize
my-term-program whose default value is
/bin/bash. It’s used by
Override default setup
Place your setup in
~/.custom.el which is loaded after other “*.el”.
So you can use any functions defined in my emacs configuration.
Here is a sample to override keybindings defined in
(with-eval-after-load 'evil (my-space-leader-def "ss" 'pwd "ll" 'pwd "pp" 'pwd))
Code navigation and auto-completion
It’s usable out of box if Ctags is installed
To enable code auto-completion,
M-x counsel-etags-scan-code at least once.
Optionally, you can add
(add-hook 'after-save-hook 'counsel-etags-virtual-update-tags) into your
.emacs to automatically update tags file.
No further setup is required.
Use “m” key for “evil-matchit”
The default keybinding of
%. Its evil text object name is also
You can use
m instead of
% as new keybinding and the text object name from
It’s just one liner in
;; evil-matchit 2.3.0 is required (setq my-use-m-for-matchit t)
Use =,m= for
evil-set-marker whose original keybinding is
Preview color theme
Write down the name of color theme (for example, molokai).
Setup color theme manually (recommended)
M-x counsel-load-theme to switch themes.
Or you can insert below code into end of
;; Please note the color theme's name is "molokai" (load-theme 'molokai t)
You can also run
M-x random-color-theme to load random color theme.
Use color theme in terminal
Use 256 colors is just one CLI without any extra setup,
TERM=xterm-256color emacs -nw
True colors in terminal Emacs
- Install Emacs 26
- Install terminals supporting true color. I suggest mintty on Windows, terminator on Linux, iTerm2 on macOS
- Set up and start Emacs
Grep/Replace text in project
Many third party plugins bundled in this setup have already provided enough features. For example, if you use
counsel-git-grep from package counsel/ivy works out of the box.
A generic grep program
counsel-etags-grep is also provided. Since
counsel-etags-grep is based on
counsel/ivy, it also supports a magic called “multi-editing via Ivy”. You could read Nuclear weapon multi-editing via Ivy and Ag to get the idea.
Multi-edit workflow is optimized. After
M-x counsel-etags-grep or pressing =,qq=, press
C-c C-o C-x C-q to enable
wgrep-mode. You can edit text (for example, delete lines) in
You can exclude multiple keywords using
!keyword1 keyword2 in
You can input
:pinyin1 pinyin2 !pinyin3 pinyin4 in ivy UI to search by Chinese Pinyin. The key point is to make sure the first character of input is “:”.
React and JSX
rjsx-mode with Emacs v25+. It’s based on
js2-mode so it has excellent imenu support.
web-mode is also very popular to edit jsx files.
You can add
(add-auto-mode 'rjsx-mode "\\.jsx\\'") into
~/.custome.el to use
web-mode for jsx files.
I use modified version of
git-gutter for now until my pull request is merged into official repository.
You can set
git-gutter:exp-to-create-diff to make git gutter support other VCS (Perforce, for example),
(setq git-gutter:exp-to-create-diff (shell-command-to-string (format "p4 diff -du -db %s" (file-relative-name buffer-file-name))))
Start a shell inside Emacs
If you use Zsh instead of Bash, please modify
Setup fonts in GUI Emacs
Non-Chinese can use unicode-fonts.
Chinese can use chinese-fonts-setup.
They are not included in this setup. You need install them manually.
Synchronize setup with Git
Synchronize from my stable setup:
git pull https://email@example.com/redguardtoo/emacs.d.git stable
Or latest setup:
git pull https://firstname.lastname@example.org/redguardtoo/emacs.d.git
You can revert commit:
# always start from the latest related commit git revert commit-2014-12-01 git revert commit-2014-11-01
Please note paredit-mode is enabled when editing Lisp. Search “paredit cheat sheet” to learn its key bindings.
(require 'init-modeline) in init.el at first.
Key bindings doesn’t work?
Other desktop applications may intercept the key bindings. For example, it’s reported QQ on windows 8 can intercept “M-x”.
M-x org-version, then read corresponding online manual to set up.
org-capture requires manual setup.
M-x org-open-at-point to open link under cursor. Http link will be opened by the embedded
emacs-w3m is used. `C-u M-x org-open-at-point= uses the external browser specified by
browse-url-generic-program whose value could be
Please replace legacy Emacs 22 and ctags with the new versions.
The easiest way is change Environment variable PATH.
Customize global variables
Some variables are hard coded so you cannot
M-x customize to modify them.
Here are the steps to change their values:
- Find the variable description by
- For text “Company Clang Insert Arguments”, search
- You will find
BTW, please read my comments above the code at first.
Open/Save files with Counsel/Ivy
C-M-j to ignore candidates and open/save files using current input.
You can also press
M-o to apply other action on selected file. See https://oremacs.com/2015/07/23/ivy-multiaction/ for details.
I strongly suggest Cygwin version of Emacs. Native version of Emacs should know how to find third party command line programs from Cygwin. I suggest adding
C:\Cygwin64\bin to environment variable
PATH so Emacs can detect the programs automatically.
By default, environment variable
HOME points to the directory
C:\Users\<username> on Windows 7+. You need copy the folder
.emacs.d into that directory. Or you can change
- Instead of
M-x yas-expandor pressing
TABkey, you can press
- Yasnippet works out of box. But you can
M-x my-yas-reload-allto force Yasnippet compile all the snippets. If you run
my-yas-reload-allonce, you always need run it when new snippets is added. The purpose of
my-yas-reload-allis to make Emacs start up faster.
- You can add your snippets into
grep -rns --exclude‘.yas*’ ‘key:’ *= in
snippets/to see my own snippets
Locale must be UTF-8 compatible. For example, as I type
locale in shell, I got the output “zh_CN.UTF-8”.
Behind corporate firewall
Run below command in shell:
http_proxy=http://yourname:email@example.com:8080 emacs -nw
Network is blocked
Try https://github.com/XX-net/XX-Net. Run command
http_proxy=http://127.0.0.1:8087 emacs -nw in shell after starting XX-Net.
If you use Gnus for email, check
init-gnus.el and read my Gnus tutorial.
Cannot download packages?
Some package cannot be downloaded automatically because of network problem.
M-x package-refresh-content, restart Emacs, reinstall package.
By default, packages from GNU ELPA are NOT available. Search the line “uncomment below line if you need use GNU ELPA” in
init-elpa.el if you want to access GNU ELPA.
For example, flycheck requires packages from GNU ELPA.
Disable Vim key bindings
By default EVIL (Vim emulation in Emacs) is used. Comment out line containing
(require 'init-evil) in init.el to unload it.
It’s defined in
C-z to switch between Emacs and Vim key bindings.
Please read its PDF manual before using
C++ auto-completion doesn’t work?
I assume you are using company-mode. Other packages have similar setup.
There are many ways to scan the C++ source files.
Clang is good at handling C++.
If you use clang to parse the C++ code:
- Make sure code is syntax correct
- assign reasonable value into company-clang-arguments
Here is sample setup:
(setq company-clang-arguments '("-I/home/myname/projs/test-cmake" "-I/home/myname/projs/test-cmake/inc"))
In “friendly” Visual C++, similar setup is required.
Auto-completion for other languages
It’s similar to C++ setup. Since GNU Global supports many popular languages, you can use
For languages GNU Global doesn’t support, you can fall back to
company-ctags and Ctags. Ctags configuration file is
You can also complete line by
M-x eacl-complete-line and complete multi-lines statement by
Chinese Input Method Editor
Please note pyim is already built into this setup. You need not install it through ELPA.
M-x toggle-input-method to toggle input method pyim.
Dictionaries with “.pyim” extension under the directory
~/.eim/ are automatically loaded.
curl -L http://tumashu.github.io/pyim-bigdict/pyim-bigdict.pyim.gz | zcat > ~/.eim/pyim-bigdict.pyim to install dictionary for pyim.
Install multiple versions of Emacs
Run below commands in shell:
mkdir -p ~/tmp; curl http://ftp.gnu.org/gnu/emacs/emacs-24.4.tar.gz | tar xvz -C ~/tmp/emacs-24.4 cd ~/tmp/emacs-24.4; mkdir -p ~/myemacs/24.4; rm -rf ~/myemacs/24.4/*; ./configure --prefix=~/myemacs/24.4 --without-x --without-dbus --without-sound && make && make install
Feel free to replace 24.4 with other version number.
Change Time Locale
Insert below code into
;; Use en_US locale to format time. ;; if not set, the OS locale is used. (setq system-time-locale "C")
init.el is the main file. It includes other
lisp/init-elpa.el defines what packages will be installed from MELPA.
Packages are installed into
I also manually download and extract some packages into
site-lisp/. Packages in
site-lisp/ are not visible to the package manager.
My own snippets is at
The git hooks is placed in
Other directories don’t matter.
Run the unit test before git commit
On macOS/Linux/Cygwin, run
make githooks to install hooks into
Then unit test is run automatically before
This setup used elpy (Emacs Python Development Environment).
See elpy official documentation on how to use elpy.
You could insert below code into
Then run `M-x lsp` to start lsp client and server. Check https://github.com/emacs-lsp/lsp-mode on how to install lsp server.
Support legacy Emacs versions
Version 1.2 of this setup is the last version to support Emacs v23.
Here are the steps to use that setup:
- Download https://github.com/redguardtoo/emacs.d/archive/1.2.zip
- Download https://github.com/redguardtoo/myelpa/archive/1.2.zip
- Follow the section
Install stable version in easiest waybut skip the download steps
Version 2.6 is the last version to support
Download https://github.com/redguardtoo/emacs.d/archive/2.6.zip and https://github.com/redguardtoo/myelpa/archive/2.6.zip and you are good to go.
Emacs 24.4 and 24.5
Version 2.9 is the last version to support
- Never turn off any bundled mode if it’s on by default. Future version of Emacs may assume it’s on. Tweak its flag in mode hook instead!
- Git skills are extremely useful. Please read the chapters “Git Basics”, “Git Branching”, “Git Tools” from Pro Git
- You can run
optimize-emacs-startupto compile “*.el” under
Please check EmacsWiki and my FAQ at first
If you still can’t resolve the issue, restart Emacs with option
---debug-init in shell. Then run
M-x toggle-debug-on-error in Emacs. Try to reproduce the issue.
The full command line to run Emacs is
emacs -nw --debug-init in most environments.
If you use Emacs for Mac OS X, the command line is
/Application/Emacs.app/Contents/MacOS/Emacs -nw --debug-init.
Send error messages to the original developer if it’s third party package’s problem
If you are sure it’s this setup’s problem, file bug report at https://github.com/redguardtoo/emacs.d. Don’t email me!
Bug report should include environment details.