Emacs is yet another editor available in Unix. Comparing to Vi/Vim, Emacs may not initialize as quickly as vim, but it has more powerful customization ability than vim. Before I learn to use Emacs, I use vim as my daily editor, and I love it deeply. Up to this day, I use both Emacs and Vim as my daily editor for different scenarios. Basically, I use vim for source code quick view and basic editing with its great syntax highlight and code folding supports. Most importantly, Vim can be opened or closed smoothly which adapts to its usage scenarios. For Emacs, I use it for most interactive coding because Emacs can be customized to cover mainly procedures like shell interpreter, debuger, tester, version control and so on. By the way, I also use Emacs Org mode as GTD tool to schedule my daily life. Moreover, org-trello is highly recommended if you used to use trello as your schedule tool.
Why Begin to Use Emacs
I have a quite handy Vim configuration in my dotfiles. Integrated with tmux, I leverage vim and terminal fantastically. Unlike others who want to try new things, I began to use Emacs because I occasionally learnt scheme when I was reading SICP. That sounds confusing. What’s the relationship between Emacs and scheme?
When I was read SICP, I was attracted by the programming thinking of scheme. Scheme follows a minimalist design philosophy, and higher level functions can be implemented just using a small standard core. This phiolosophy makes scheme much different with the programming languages that I have learnt before. Finally, I decided to learn more about this minimalist design philosophy. Common lisp became my target, because it shares the philosophy of scheme as another lisp dialect. I was told that SLIME+Emacs is the best development environment for studying lisp. So learning to use Emacs became reasonable.
In a word, if your coding workflow is to write one and evaluate one iteratively, Emacs may be always your best choice.
Unlike many Emacs tutorials, I don’t want to do a bottom-up, step-by-step configuration, I directly use Emacs masters’ configuration and modify it for myself preference. Even though some features may not be used at the begining, the Emacs configuration is already a considerable good practice to setup your own Emacs. I forked Purcell’s emacs.d on github, and began my way to Emacs configuration. As a previous Vim user, the first thing that I did is to enable Evil mode, because it’s still difficult for a Vim user to use Emacs default key binding and Evil is a good alternation for Vim users.
For Evil, my goal is to make it work like my original Vim. To do so, I found some Vim plugins replacements in Emacs. Briefly speaking, I use vim-snippets in Vim but yasnippet in Emacs, nerdcommenter in Vim but evil-nerd-commenter in Emacs, vim-easymotion in Vim but avy in Emacs, ect. Also Vim has bulit-in code folding ability, in Emacs, I use hideshow to do this.
Besides Evil, I added some new programming languages mode to Emacs as well. They are elpy for Python, ess for R, SLIME for Common Lisp and matlab.el for Matlab. For the details, please visit my emacs.d repo on github and project page at https://shadowthink.com/emacs.d.
Emacs itself is an Emacs lisp interpreter, and all functions are implemented in Emacs lisp. Of course, Emacs lisp is a dialect of lisp, and it shares the philosophy of lisp. I haven’t try to learn Emacs lisp on purpose til now, but with the knowledge of scheme programming, I can easily understand and write some Emacs lisp code by imitating.
Purcell’s emacs configuration separates different package configurations into different files. So the first thing is to figure out where to put your own Emacs lisp code and how to intergrate it into Emacs initialization procedure. After you have installed and set up serveral third part packages, you may notice that the key point is to add the path of package to Emacs load path and require the package. Then set serveral package variables according to your preference. Sometimes, define your preferable key bindings. Moreover, define your own functions and add them as a mode hook.
If you want to build more advanced functionalities, you would have to learn about built-in interfaces of Emacs lisp which give you high level operations. For instance, we want to define a interactive function which can be called by
M-x in Emacs. To do so, we need to use
(interactive) statement. As below, is a full example to switch between Python2 and Python3 quickly in elpy.
(defun elpy-python2 () (interactive) (setq elpy-rpc-python-command "python2") (setq python-shell-interpreter "ipython2")) (defun elpy-python3 () (interactive) (setq elpy-rpc-python-command "python3") (setq python-shell-interpreter "ipython3"))
In 1984, Richard Stallman began writing a new Emacs implementation, GnuEmacs, which is the first version of nowadays widely used Emacs. More and more features are developed, but the some core framework almost didn’t change. So it’s a strong evidence to prove that Emacs framework is extensible and well-designed.
When we talk about a framework of an editor, it’s common to discuss its display system at first. There are three basic elements in Emacs display system - frame, window and buffer. In Emacs, frame occupies a graphical system-level “window”, and it can be splited into multiple windows. Each window displays one buffer.
After define these graphical system, we also need to resolve data flow control and interactive events handling. A modern approach is MVC framework, so did Emacs in 32 years ago.
Model of Emacs framework is buffer. Because Emacs is an editor, the core role of buffer is to store the text file contents. Every buffer in Emacs has a mode with different editing behaviors. And the local variables of the buffer maintain the state of this buffer, such as editing history. Emacs provides some Emacs lisp functions to these states making it possible to monitor and assist your editing.
View of Emacs framework is frame and window. Emacs defines its inner render system to gain the ability for auto re-render, which means third part packages don’t need to manually decide what the exact render operations are.
Controller of Emacs framework is an Emacs lisp interpreter. With this controller, it handles interactive events like keyboad input, mouse actions and menu selections.
Thanks to Emacs, it gives me comfortable programming and blogging experience. In future, I’ll do deeper in Emacs lisp and try to write some useful packages, and contribute to Emacs community.