Features
- Fast. Emacs starts up in one second
- Robust. Emacs will never crash after a full package upgrade. You can install packages without network
- Support Windows
- Tested with Emacs 24.4, 24.5, 25.3, 26.1 on Linux/Windows/Cygwin/macOS
- Original setup (key bindings, directories …) of Emacs and 3rd party packages are respected
- Usable in emacs-nox. It works when you ssh to server with PuTTY
Table of Content
- Features
- Checklist
- Installation
- Tutorial (OPTIONAL)
- Usage
- FAQ
- default terminal shell
- Override default setup
- Code navigation and auto-completion
- Color theme
- Grep/Replace text in project
- Hydra/Swiper/Counsel/Ivy
- js2-mode
- React and JSX
- git-gutter
- Start a shell inside Emacs
- Setup fonts in GUI Emacs
- Synchronize setup with Git
- Indentation
- Editing Lisp
- Use smart-mode-line or powerline?
- Key bindings doesn’t work?
- Use org-mode
- macOS user?
- Locked packages
- Customize global variables
- Open/Save file with Counsel/Ivy
- Windows
- Yasnippet
- 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
- Support legacy Emacs versions
- Tips
- Report bug
Checklist
- 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 - Most packages from MELPA are invisible while packages from MELPA Stable are visible. You can modify variable
melpa-include-packagesin~/.emacs.d/lisp/init-elpa.elto install unstable packages - Emacs 24 support will be dropped on 2019. Check “Support legacy Emacs versions” if you use old versions
- Other issues are covered in FAQ
Installation
Most users should follow the section “Install in normal way”.
You need the section “Install stable version in easiest way” if and only if:
- You have never heard Unix/Linux
- You are living in North Korea without internet access
Install in normal way
Please remove ~/.emacs.d/init.el and ~/.emacs. ~ means Home directory.
Please uninstall third party Emacs package which is not placed at ~/.emacs.d. For example, run apt-get autoremove emacs-w3m on Debian/Ubuntu.
If you don’t know what I mean, then do nothing.
There are two ways to install this setup (I recommend the first way):
- download latest setup and extract its content into
~/.emacs.d, OR run commandcd ~; git clone https://github.com/redguardtoo/emacs.d.git .emacs.din shell. - use stable setup, OR run command
cd ~; git clone https://github.com/redguardtoo/emacs.d.git .emacs.d; cd .emacs.d; git reset --hard stablein shell.
Ensure that init.el contained in this repo ends up at ~/.emacs.d/init.el.
By default, packages are installed automatically during Emacs startup.
Install stable version in easiest way (OPTIONAL)
You only need two zip files.
Here are the exact steps,
- Remove
~/.emacs. - Download https://github.com/redguardtoo/emacs.d/archive/stable.zip
- Extract its content into empty directory
~/.emacs.d - Download https://github.com/redguardtoo/myelpa/archive/stable.zip
- Extract the zip somewhere, say
~/projs/myelpa - Make sure a file named
archive-contentsexists in~/projs/myelpa - Uncomment the line containing “myelpa” in
~/.emacs.d/lisp/init-elpa.el. Path~/projs/myelpa/could be modified. - Start Emacs now!
Now you are using local package repository ~/projs/melpa.
You can switch to online repositories like http://elpa.gnu.org or http://melpa.org by modifying ~/.emacs.d/lisp/init-elpa.el.
Third party command line program (OPTIONAL)
Install
You can install them in any way you like. But using OS package manager is easier.
OS package manager means:
- apt-cyg at Cygwin
- homebrew at macOS
- any package manager at Linux (
apt-geton Debian/Ubuntu,yumon Redhat,pacmanon Arch,emergeon Gentoo …)
If the program is developed with certain programming language. That language may provide its own package manager which allows you to install the program only for current user. For example, python package manager has the option ”–user”.
List of command line programs
These programs are OPTIONAL.
Please ignore any related error message. For example, if aspell and 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
- Install through OS package manager
Please note you can’t install fortune-zh through homebrew on macOS but there is a simple workaround:
- install fortune
- Download
fortune-zhcode from https://github.com/debiancn/fortune-zh - Extract code and run
makein its folder - copy data files and the script named fortune-zh into corresponding folders which
fortuneis using. You may need edit the filefortune-zhbefore copying
w3m (web browser)
- Required by
emacs-w3m - Install through OS package manager
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.
shr supports COLOR while w3m not.
But my w3m based utilities can open video/audio/image with the help of w3m. So your choice.
lua
- Required by
flymake-lua - Install through OS package manager
aspell or hunspell
- Required by
flyspell - hunspell is the alternative of
aspell. So you need only one of them. - Install through OS package manager
- For aspell, you may need its dictionary
aspell-en - I force the dictionary to “en_US” in
~/.emacs./lisp/init-spelling.el. You can modify it. - I recommend aspell for programmers. Non-programmers find hunspell is 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 force-to-use-hunspell in init-spelling.el if you prefer hunspell.
This setup can spell check camel case words either using aspell or hunspell.
Aspell’s default personal English dictionary is $HOME/.aspell.en.pws. It’s content is like:
personal_ws-1.1 en 4
ABN
ACC
ACN
ACT
Hunspell’s default personal English dictionary is $HOME/.hunspell_en_US. The file format is same as aspell.
sbcl (lisp environment)
- Required by lisp
slime - Install through OS package manager
tidy
- Required by
web-modefor syntax check andtidy.elhtml formatting - Install through OS package manager
csslint
- Install
node.jsthrough OS package manager, thennpm install -g csslint
identify from ImageMagick
- Required by
org-modeto export org file to odt file when image embedded - Install through OS package manager
zip and unzip
- Required by
org-modeto export org file to odt file - Install through OS package manager
jshint
- Install
node.jsthrough OS package manager, thennpm install -g jshint - Required by js-mode
- I use js2-mode which does NOT need jshint
xsel
- Required by my clipboard command
copy-to-x-clipboardandpaste-from-x-clipboardunder Linux - Install through OS package manager
CMake
- Required by
cpputils-cmake - Install through OS package manager
- Please use out-of-source build tree when using CMake
Clang
- Required by
cpputils-cmake,company-clang - Install through OS package manager
- If you use
cpputils-cmakeandcmake,cpputils-cmakewill do all the setup for you. You don’t need 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
company-clang, add(setq company-clang-arguments '("-I/example1/dir" "-I/example2/dir"))into ~/.emacs.d/init.el
Pandoc
- Required by
markdown-previewfrommarkdown-mode - Install through OS package manager
GCC/Make
- Required by
flymake - Install through OS package manager
MozRepl (Firefox addon)
- Required by MozRepl
- Used by Firefox
Universal Ctags (recommended) or Exuberant CTags
- It creates tags file for code navigation and code completion
- Required by many tags related packages
- Install through OS package manager
- See How to use ctags in Emacs effectively
GNU Global
- Required by counsel-gtags
- It creates index files for code navigation and auto-completion
- Please read GNU Global manual to understand environment variables
GTAGSLIBPATHandMAKEOBJDIRPREFIX - Install through OS package manager
LibreOffice
- Only the executable
sofficeis used when converting odt file into doc/pdf - conversion happens automatically when exporting org-mode to odt
- The conversion command is in variable
org-export-odt-convert-processes - Install through OS package manager
js-beautify
- Beautify javascript code
- Install pip through OS package manager, then
pip install jsbeautifier
syntaxerl
- syntax check Erlang through flymake
- Install from https://github.com/ten0s/syntaxerl
jedi & flake8
- Required by elpy which is python IDE
- At least
pip install jedi flake8. Check elpy website for more tips.
sdcv (console version of StarDict)
- Required by sdcv.el
- Install through OS package manager
- Run
curl http://abloz.com/huzheng/stardict-dic/dict.org/stardict-dictd_www.dict.org_wn-2.4.2.tar.bz2 | tar jx -C ~/.stardict/dicto install dictionary
ripgrep
- Used by
M-x counsel-etags-grepto search text in files - Run
curl https://sh.rustup.rs -sSf | shin shell to install Rust thencargo install ripgrep - Tweak environment variable PATH to let Emacs find ripgrep
I will keep using ripgrep instead of the-silver-searcher from now on.
Tutorial (OPTIONAL)
Basic knowledge of Linux/Unix is required. At least you should understand the words like “environment variable”, “shell”, “stdin”, “stdout”, “man”, “info”.
Basic tutorial
Please read this tutorial at least for once.
Step 1, learn OS basics
At minimum you need know,
- What is Environment Variable
- What is Pipeline (Unix), Standard Streams
The purpose it to know how Emacs interacts with other command line programs.
Step 2, read official tutorial at least once
Press C-h t in Emacs (“C” means Ctrl key, “M” means Alt key) to read bundled tutorial.
At minimum you need know:
- How to move cursor
C-h vto describe variableC-h fto describe functionC-h kto check function 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 Tab.
Step 4, start from a real world problem
You can visit EmacsWiki for the solution. Newbies can ask for help at http://www.reddit.com/r/emacs/.
Evil-mode tutorial
Required for vim user,
- Finish vimtutor.
- Please Read
~/.emacs.d/site-lisp/evil/doc/evil.pdf
Methodology
Usage
I avoid overriding the original setup of third party command line program.
Quick start
On windows, you need install Cygwin which provides some command line tools Emacs will use. Cygwin could be installed on any hard drive but it’s highly recommended don’t change path of Cygwin.
Install Ctags (Universal Ctags is recommended, but Exuberant Ctags is fine). On Windows you could install Ctags through Cygwin.
Run M-x cousnel-etags-find-ctags from counsel-etags to navigate code by using tags files created by ctags. Tags file will be automatically created/update when you start using `counsel-etags`.
Run M-x counsel-etags-grep to search text (grep) in project. Project root is automatically detected if you use open source version control software. Or else the parent directory of tags file is used.
Run M-x find-file-in-project-by-selected from find-file-in-project to find file in project. The project root is automatically detected if you use any popular open source version control software. Or else, you need only one line setup in .emacs like (setq ffip-project-root "~/proj1/").
The code auto completion works out of box by using tags file created by Ctags. So it means you need run counsel-etags at least once to fire up Ctags. company-etags from company-mode provides the candidates for auto completion. No manual setup is required.
You can also grep in the directory ~/.emacs.d/lisp if you got any question on setup.
Key bindings
You don’t need remember key bindings. At the beginning, try `M-x my-command` (corresponding key binding is also displayed) instead.
Most key bindings are defined in ~/.emacs.d/lisp/init-evil.el.
For example, (nvmap :prefix "," "bu" 'backward-up-list) means pressing “,bu” executes command backward-up-list.
The tutorials I recommended provides enough information about commands.
Besides, ”How to be extremely efficient in Emacs” lists my frequently used commands.
Hydra is used to define key bindings when evil-mode is disabled. See ~/.emacs.d/lisp/init-hydra.el for details.
You can always press C-c C-y anywhere to bring up default hydra menu.
You can override any key bindings in ~/.custome.el.
FAQ
default terminal shell
You can customize my-term-program whose default value is /bin/bash. It’s used by ansi-term and multi-term.
Override default setup
Place your setup in ~/.custom.el.
Code navigation and auto-completion
It’s usable out of box if Ctags is installed
To navigate, M-x counsel-etags-find-tag-at-point.
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.
Color theme
Preview color theme
Check https://emacsthemes.com/.
Write down the name of color theme (for example, molokai).
Setup color theme manually (recommended)
You can M-x counsel-load-theme to switch themes.
Or you can insert below code into end of ~/.custom.el or ~/.emacs.d/init.el,
;; Please note the color theme's name is "molokai"
(when (or (display-graphic-p)
(string-match-p "256color"(getenv "TERM")))
(load-theme 'molokai t))You can also run M-x random-color-theme to load random color theme.
Use color theme in terminal
Start Emacs this way,
TERM=xterm-256color emacs -nwGrep/Replace text in project
Many third party plugins bundled in this setup have already provided all the features you needed. For example, if you use git, counsel-git-grep from package counsel/ivy works out of the box.
A generic grep program counsel-etags-grep is provided if you don’t use git. Since counsel-etags-grep is based on counsel/ivy, it also supports “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 wgrep-mode directly.
You can specify the ignore regex like !keyword1 in ivy. If you want to ignore multiple keywords, you must use syntax like !keyword1\|!keyword2. As I tested in counsel v0.9.1, non of its grep commands supports ignore syntax reliably. So I suggest using counsel-etags-grep instead.
Hydra/Swiper/Counsel/Ivy
I love all the packages from Oleh Krehel (AKA abo-abo). Every article from his blog is worth reading ten times.
You can input :keyword in ivy UI to search by Chinese Pinyin.
js2-mode
I release patched js2-mode based on latest official version every three months. My patched version has better imenu support.
Please package-refresh-content from time to time upgrade js2-mode.
React and JSX
I use rjsx-mode with Emacs v25+. It’s based on js2-mode so it has excellent imenu support.
But web-mode v15+ is also very popular to edit jsx files.
In order to replace rjsx-mode with web-mode, you need search line (add-to-list 'auto-mode-alist '("\\.jsx\\'" . rjsx-mode)) in init-javascript.el and replace rjsx-mode with web-mode on that line.
git-gutter
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
Please M-x multi-term.
If you want to use Zsh instead of Bash, please modify init-term-mode.el
Setup fonts in GUI Emacs
Non-Chinese use unicode-fonts.
Chinese 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 which is updated every 6+ months:
git pull https://redguardtoo@github.com/redguardtoo/emacs.d.git stableOr latest setup which is unstable:
git pull https://redguardtoo@github.com/redguardtoo/emacs.d.gitIf you don’t like my commits, you can revert them:
# always start from the latest related commit
git revert commit-2014-12-01
git revert commit-2014-11-01Indentation
Learn basics. Then use my solution.
Editing Lisp
Please note paredit-mode is enabled when editing Lisp. Search “paredit cheat sheet” to learn its key bindings.
Use smart-mode-line or powerline?
Comment out (require 'init-modeline) in init.el at first.
Key bindings doesn’t work?
Other desktop applications may intercept the key bindings. For example, someone reported QQ on windows 8 can intercept “M-x”.
Use org-mode
Press M-x org-version, then read corresponding online manual to setup.
For example, org-capture need your manual setup.
macOS user?
Please replace legacy Emacs 22 and ctags with the new versions.
The easiest way is change Environment variable PATH.
Locked packages
Some packages (Evil, Web-mode …) are so important to my workflow that they are locked.
Those packages are placed at ~/.emacs.d/site-lisp.
They will not be upgraded through ELPA unless you delete them at ~/.emacs.d/site-lisp at first.
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
M-x customize - For text “Company Clang Insert Arguments”, search
company-clang-insert-argumentin~/.emacs.d/lisp/ - You will find
~/.emacs.d/lisp/init-company.eland modifycompany-clang-insert-argument
BTW, please read my comments above the code at first.
Open/Save file with Counsel/Ivy
Keep pressing 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.
Windows
I strongly suggest Cygwin version of Emacs. But native windows version is OK if it knows how to find the third party command line programs. Cygwin provides most of them by default. Please add C:\Cygwin64\bin to environment variable PATH so Emacs can detect the program 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 setup HOME.
Yasnippet
- Instead of
M-x yas-expandor pressingTABkey, you can pressM-jinstead. - Yasnippet works out of box. But you can
M-x my-yas-reload-allto force Yasnippet compile all the snippets. If you runmy-yas-reload-allonce, you need always to run it when you update the snippets. The purpose ofmy-yas-reload-allis to optimize the Emacs startup only. IMO, it’s not worth the effort. - You can add your snippets into
~/.emacs.d/snippets. - Run
grep -rns --exclude‘.yas*’ ‘key:’ *= in~/.emacs.d/snippetsto see my own snippets
Non-English users
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:passwd@proxy.company.com:8080 emacs -nwNetwork 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.
You could M-x package-refresh-content and restart Emacs. the package will be installed automatically.
use packages on GNU ELPA
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.
Evil setup
It’s defined in ~/.emacs.d/lisp/init-evil.el. Press C-z to switch between Emacs and Vim key bindings.
Please read its PDF manual before using evil-mode.
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. The Emacs Lisp code and command line programs to scan the C++ files are company backends. company-clang is a popular backend because Clang is good at processing 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 code:
(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.
You can use other company backends instead of company-clang. For example, you can use company-gtags and GNU Global instead. See Emacs as C++ IDE, easy way for details.
Other backends produce less precise results but are more efficient and easier to setup.
Auto-completion for other languages
It’s similar to C++ setup. You can use company as frontend. But backend is the key. For example, you can’t use company-clang for PHP because Clang can’t handle PHP. But GNU Global supports PHP, so you can use company-gtags instead.
For languages GNU Global doesn’t support, you can always fall back to company-etags and Ctags by using regular expression. Regular expressions could be placed in ~/.ctags.
You can also complete line by M-x eacl-complete-line and complete multi-lines statement by M-x eacl-complete-statement.
Chinese Input Method Editor
M-x toggle-input-method to toggle input method pyim.
If your personal dictionary ~/.eim/personal.pyim exists, it will be used.
You can M-x my-pyim-export-dictionary to export your personal words into personal.pyim. Check init-chinese-pyim.el for more information.
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 installFeel free to replace 24.4 with other version number.
Change Time Locale
Insert below code into ~/.emacs or =~/.custom.el,
;; Use en_US locale to format time.
;; if not set, the OS locale is used.
(setq system-time-locale "C")Directory structure
~/.emacs.d/init.el is the main file. It includes all the other *.el files.
~/.emacs.d/lisp/init-elpa.el defines what packages will be installed from MELPA.
Packages are installed into ~/.emacs.d/elpa/.
I also manually download and extract some packages into ~/.emacs.d/site-lisp/. Packages in ~/.emacs.d/site-lisp/ are not visible to the package manager.
My own snippets is at ~/.emacs.d/snippets.
Other directories don’t matter.
Support legacy Emacs versions
Emacs 23
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
Emacs 24.3
Version 2.6 is the last version to support Emacs 24.3.
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
Support will be dropped on 2019
Tips
- 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~/.emacs.d/lisp/
Report bug
- Please check EmacsWiki and my FAQ at first
- If the issue is still NOT resolved, restart Emacs with option
---debug-init, runM-x toggle-debug-on-errorin Emacs. Try to reproduce the issue - Send error messages to the original developer if it’s third party package issue
- If you are sure it’s my issue, file bug report at https://github.com/redguardtoo/emacs.d. Don’t email me!
Bug report should include details (OS, Emacs version …).