Preface
Dear readers,
I am aware that many were waiting to read my new post after my last one on GNUKhata, the FOSS accounting software. The launch of version 4 of the said software surely deserved a blog in itself. Indeed, the software now has enterprise quality Inventory features along with more advanced accounting modules. You can look at the feature list at gitlab.com/gnukhata/gkwebapp/milestones where version 4 is detailed. But today I am going to talk about some thing very technical and specific for professional and would-be professional programmers. I know this is disappointing for all non-technical readers in general and I profoundly apologize for those who may not find this post to their taste. But this is also the first time I am writing a technical post and so I guess many of my techy friends will be very happy.
I am going to share my views and experiences about a very powerful, old yet mordan programmers IDE/ work horse called Emacs. I wish to say at the very beginning that I am only speaking from my experiences and from the facts shared by few veteran programmers. I have formed these views after using several IDEs over the period of 12 + years of programming experience. I might be wrong from some other programmer’s point of view and some fact I have stated might need updating. For example I might be talking about a feature in Emacs which is also available in other IDE. Or perhaps some functionality may seem to be superior in other program. In short the facts which I am sharing in terms of features and functionality are on the basis of my experience I had in last few months with Emacs. I will not just share my experience but also give some tips and tricks which I found very valuable on my way to getting grip on Emacs. I will be providing my approach on learning emacs and also my entire configuration. Enough of the preface, it was just meant to prevent any IDE wars.
Introduction for beginners
Emacs is a powerful programming environment. It is also a rich text editor. However calling it a text editor would be like calling a cannon as a revolver. It not just has all the bells and whistles of a modern IDE but has a powerful yet relatively easy way of customizing user experience. I say relatively easy because you will really get lost at the beginning, often frustrated to the point of throwing it out of your machine. But take my word on this. Once you start getting comfortable configuring Emacs, the production level grows exponentially, Coding becomes seemlessly easy and you feel like you are getting more and more power in your hands. So don’t feel guilty when u feel like laughing on yourself for intending to throw it out of your box when you look behind.
Before I get into the pros and cons of using Emacs for programming, I must explain a bit about the philosophy behind this powerful IDE. Firstly, emacs is nothing as such and every thing in general. Confused? let me make it more clear. Emacs is mostly a programmer’s tool and just like most programming languages, it has a very small core set of functionality. Just like we use numerous libraries to augment the programming language, Emacs has a lot of modules to give it the very power that it possesses. That said, the basic version of Emacs itself comes with “batteries included ” for many common place programming tasks. However the philosophy advocates “make it your tool and use your rule” approach. This means almost every Emacs user ought to know how to configure it. In fact I can go to the extent of saying that if you can’t configure Emacs, then either go with some one else’s configurations which suites you and compromise with what that configuration does not have, or don’t use Emacs at all. Surely there are several other IDEs like Eclipse and Atom. There is also sublime and Anjuta.
So first of all you need to learn the basic text navigation and other related commands and then learn the configuration. I would also warn all my readers to be patient and take it easy. Learn few things at a time and once those are done, keep some notes handy on what you did to achieve what you achieved. It is easy to forget the steps you took initially, when it comes to a huge thing like Emacs so the above tip is extremely important. Well I will give my own configurations in this Blog, so many of you can benefit from it. Specially programmers coding in Python, Django templates, Javascript can take full advantage of the configuration I have assembled. But mind you, there are few things you may like to change. Again I repeat, configuring Emacs is the key to using it with all the power. So what is this configuration all about, how we do it? And what’s so unique that I am recommending programmers to become proficient with this super powerful programmer’s editor?
Pros and Cons in a summary
Now that we are clear with the approach Emacs advocates, let’s look at how it is different in terms of an IDE and how it is best suited for most programming work we do as professionals. We will then discuss about the major aspects in details. Surely there are several angles to using Emacs but I will cover those which matter to programmers who need real power and precision in their work.
Pros
Extremely lite weight
Smooth and super fast
Free as in cost and freedom
Most powerful code navigation capability
Best for editing complex code
Syntax highlighting, and code assist work great
Multiple options for code completion
Limitless customization possibilities
Once understood, configuration becomes easy
Carry your entire setup with you
Does not rely on heavy platforms
Cons
Not a “ready to use ” tool for every thing
Some time investment needed to master
Extreme customization may not appeal to all
Detailed analysis and reasoning
Just one look at the pros and cons will tell you that the number of advantages outweigh the disadvantages. But just don’t go by the numbers, the cons I listed are only 3 but in terms of approach might be major concerns for several people. One big concern and rightly so is “why should I invest my time customising my experience when I already can get an out-of-the box tool like Eclipse?” Well, if that is the only concern then either you should not use Emacs in the first place or look at the investment benefit ratio. If you look at the latter carefully, then you can observe that customising comes with real power and you will never have to crib about some thing not working the way you wished it did. And “precision ” is the word here. Do you like auto-complete to work in a certain specific way? Do you wish you immediately cook up your own snippets and integrate them instantly in your IDE for custom code templates? How difficult would it be to customize any other IDE?
Here I must introduce the concept of init file in Emacs. Emacs reads it’s configuration from a file called init.el in a folder called ‘.emacs.d’ on your Linux machine. This also has alternatives. I have given the link to the emacs page and will also give links to some good manuals at the end of this blog. YOu can look at the alternatives there. But for the sake of this blog and for making absolute beginners comfortable, we will consider init.el in ‘.emacs.d’ folder. Another important fact about emacs is that it is written in a language called LISP. It is an extremely powerful language with minimal syntax. So powerful is the language that it has existed since late 1950S and is still being used in various forms at top notch IT giants like Facebook and Amazon The power of this language is that it can bend according to your syntactical needs. Code can be treated as data and data can be treated as code. There are some rich variants like Clojure made out of LISP. the Syntax is basically (your code ). Yes that’s correct just () is the core syntax. Every thing is treated as a list in LISP. I discussed this here so that you know how and why the init.el file is written in a particular way. Basically, you will tell Emacs exactly what you want and how you want in the init.el file.
You want auto complete to work in a certain way. You wish to have tabbed list of all your files on top of Emacs, you wish that Indentation should be marked. All this is part of the init.el file in your ‘.emacs.d’ directory. Well, there are some other things in that directory as well. For example you will have a snippets directory where all the code templates are kept. Just like the Android play store or the Ubuntu repository, Emacs too has it’s repos containing several packages. One such is melpa. You need to configure the repository in init file. As a matter of fact this should be the first thing in your init file. You can then use the package-install or the list-packages command which I will discuss later. As I said you can see details about it in the emacs manual.
In a nutshell you install emacs packages from a repo and then do the necessary configurations in the init.el file in ‘.emacs.d’ directory and restart emacs. Apart from knowing that Emacs uses configurations in the init file, there are couple of major concepts you must be aware of to start with. If you wish to have a first look at emacs, here’s how you install it on Ubuntu 16.04 There is a separate PPA for emacs 25. Execute these following 3 commands in exactly the same order and you will have Emacs version 25 on your machine. sudo add-apt-repository ppa:kelleyk/emacs sudo apt-get update sudo apt-get install emacs25 You can search for Emacs in your dash pain and then add it to your launcher.
Emacs has a menu bar at the top and a thin strip called modeline at the bottom with the remaining space for your work. Now that you have Emacs setup, let’s go ahead and understand the core concepts. Firstly, Emacs has mode for every programming language. For example there is python-mode for python, web-mode for web related programming (django templates, Java script, html etc ) and so on and so forth. This is somewhat similar to what Eclipse users know as perspectives, just way more advanced, easy to mix and match. These different modes behave differently as per the need of a certain programming language. For instance, Python does not have braces {} to mark blocks of statements like if or else. This is done through indentation, as Python programmers like yours truly would know. And html tags need to start and to end. So there are different requirements for different languages. Readers might have one question at this point. So this is the case with IDEs like Eclipse, what is more special in Emacs modes? And yes what about auto completion of code? I will address auto completion in just a while, but first about the modes.
There are two types of modes, major and minor modes. To understand this difference, we must first know that files in Emacs are opened in what we call as buffers. You can consider buffers as the visible view of the file. So what ever changes you made to the buffer are then saved back to files. One file can be opened in many buffers at the same time and you can partially edit one part of the file in one buffer and partially in another. So buffer is a vew of the file visually. You can remotely compare this to the text area of a word processor, but buffers are much more than that. Now the rule is that one buffer can only be associated with one major mode. So a buffer containing an opened python module is for example associated with python-mode, a js file in web-mode etc. But one or more minor modes can be associated with one buffer. Here, it’s time to take an example and we will look at auto completion.
We ideally wish to have auto completion of code in all programming languages to save our time. So considering the above scenario’ both the python buffer (with python-mode ) and js buffer (web-mode ) needs to have auto completion. Amongst many auto completion modules I am aware of, company-mode is the best. There is also an older auto-complete mode but more and more Emacs users have started to like the company-mode. How about also having a system where you not just get auto completion but also entire code templates written for you? Again programmers wish this for all languages right? Yasnippet is one such module. The above mentioned company and yasnippet are two minor modes which can be hooked into python, web or any major mode of your choice. Just type some thing and auto completion starts immediately. you can press tab to accept the completion or use arrows to move in the list of choices and select one.
If you look at my init file, you will see several such hooks being added. Hook is an event where a certain major mode gets activated when a file is opened in a buffer. for example consider this line of code. (add-hook ‘web-mode-hook ‘company-mode) This says that when any file results in web-mode to be active, the company-mode (auto completion ) must also be attached to it for auto completion. now here is another line for the same web-mode. (add-hook ‘web-mode-hook ‘linum-mode) This says that along with company-mode I also want the minor mode to show me line numbers when web-mode is active. one minor mode can be associated with two major modes. For example just like web-mode, python-mode is also a major mode. I can surely have line numbering for python mode as well. you guessed it right, the code will be, (add-hook ‘python-mode-hook ‘linum-mode) Get the point?
Multiple combinations of facilities in different programming environments.
Major modes offer very specific facilities, eg. correct mandatory indentation in Python and proper braces in javascript. Minor modes have features which multiple languages can share, eg. auto completion, code assist etc. That’s why I gave you the Nara “make it your tool apply your rule”. But how does Emacs know which major mode to open for what file type? again, in most cases your rule. But these rules are sheer common sence. For example .py file should be obviously opened in python-mode. .c file in c-mode. Such obvious rules need not be mentioned so Emacs takes care of them. But some times you need to explicitly make associations between certain file types and major modes. For example for javascript, there is the web-mode and another major one called js2-mode. Which one do you choose? There are people who like web-mode for its comprehensiveness, specially when you have several libraries involved in your js file. But some need simpler tool. So I have web-mode by choice. How do I tell Emacs to start web-mode when js file is opened in a buffer? Secondly, I wish not just js, but even my jinja2 and html files open in web-mode. Well, I am just providing with a couple of lines to show how you define this in your init.el file. You can right this either before the hooks or after them. (add-to-list ‘auto-mode-alist ‘(“\\.html\\'” . web-mode)) (add-to-list ‘auto-mode-alist ‘(“\\.js\\'” . web-mode))
So the instructions are clear here. I wish to have web-mode active when an html or js file is opened. in LISP you can add things to a list with the add-to-list command. Every element has an ‘ preceding without spaces. Now you can add your own rules and decide which mode to use for what file. got the power? By the way you have also learned the syntax of Lisp language in this process. This is how every line of lisp code is written. Every thing is an element in a list enclosed by ().
To summarize, Emacs looks at a configuration file, generally referred to as init.el in a folder ‘.emacs.d in your home. Buffers are the visual views of a file and the place where you do all your work. Modes are those components of Emacs which determine the behaviour in particular programming environment. There are major modes (one per every buffer ) and optionally more than one minor modes which can be mixed and matched to give added functionality to the major mode. Minor modes can be shared so that features like auto-complete behave in exactly same way every where.
We have just seen how easy it is to configure emacs for auto completion and line numbering. This is power in itself with speed and performance added to it. Emacs can also remember your last action or set of actions. For example if you are working on a project with many files, a mode called IDO can predict where you are most likely to go when you come back to your desk after a break. Rather it takes you to that location by default. You can press backspace at any point to change the course. Remember all this at great speed, less memory foot print and with your code taking more than 90% or more space on your screen. No extra fancy tool bars, no complex windows, no multi level menus (well you can have them at your choice ), but a great editing and navigation experience, which I will be discussing right away.
Navigation and editing – a unique experience
The real power of Emacs is in it’s editing capabilities and navigation shortcuts. While the high degree of customization may have already impressed you enough, the real fun starts when you learn how to move around in the code seamlessly and edit code with optimum comfort. But before I get into some overview, first let’s get some conventions clear. In all the emacs documentation and manuals, the control key is referred to as “C” while the alt (meta ) key is called “M” and shift is called “S”. so when you read some thing like “press C-x C-c to exit Emacs ” it means, hold down the control key and press x and then press c. Well if you are really getting frustrated with Emacs, I just gave you the way to exit. But jokes apart, most keyboard shortcuts in Emacs either use C-x or M-x as the prefix. In the above example C-x is the prefix. Further more, C-x is used for major operation’s such as Exiting Emacs, Resizing the buffer, etc. While M-x is used for most other things such as issuing a command. For instance C-x 1 will make the current buffer occupy the entire screen space except the Menu bar if full-screen is not enabled. -x 2 will split horizontally., giving two buffers place in the same window. If you remember I had mentioned a command named package-install.
You may have guessed that since it is a command, you have to type it after pressing alt_x, where you get the prompt to enter it. Note that you can type just a few characters and press the tab key to get auto completion suggestions for the incomplete command. Besides these group of commands, there are several other commands you may want to learn. These are already detailed in the manuals which will be provided at the end of this blog. Remember one thing though, Emacs is highly keyboard driven just like VIM. The reason why Emacs or VIM does this is obvious yet sub-conscious. It has been scientifically proven that the more you are typing as you think, the more productive you are in terms of converting logic to code. So moving your hand off the keyboard on to the mouse can often break your link.
I will also link a cheat sheet for all basic Emacs commands along with the manuals. Just to highlight some important commands. M-w copy, C-w cut, C-y paste (called as yank ), M-y after C-y, multi level paste. You can use the shift key to mark more than one line at a time. You can also kill the entire line by placing the cursor at the beginning and pressing C-k (k = kill). C-f goes one word forward, C-b one word backward. If you are a Python programmer then C-M-e takes you to end of function or class. C-M-a at the beginning. M-e takes you at the end of current block and M-a at the beginning. You can press C-s and search for any thing you wish. The cursor directly takes you to the first found location. You can keep pressing the command to move forward and search the next occurrence. C-R does reverse search.
Do take out some time and read the manuals which are listed at the end of this blog. Those are hand picked by yours truly for quick yet efficient learning. To open a file you use the command C-xC-f and then … Well, here some fun starts. You can type the name of the file partially and hit the tab key. But what if you wish some clever auto completion. I had made a passing reference to the ido-mode some time back. So you can try it out. if you are running emacs already, you can first press M-x and type ido-mode and hit enter. Look at the modeline (the thin strip below your code buffer ) to see some thing like “ido set to both “. Now when you press C-xC-f command, you will see the list of files in the current folder. You start typing and completion happens in a dictionary style.
If you open some file and then quit Emacs. you can come back to this file again very easily if you keep ido on. It will predict your path to this file when you press C-x-C-f again. I am sure once you learn these commands and more, you would love to use Emacs for all your programming. Another handy command is C-x C-b which lists all the buffers currently open. you can use your arrow keys to go up and down in the list and hit enter on which ever buffer you wish to open. By the way, Enter is often called “ret ” in Emacs lingo. You can also press k on any buffer in the list to kill (close ) it. To start or enable any mode, you press M-x and type the mode’s name and press Enter. Now you might say “how do I remember all the commands all the time? how many mode names to recall?” Well, here comes the last fun part of this blog before I give you the wealth of manuals and my own personal collection of the tool box. the customizations in the init.el file are not just restricted to combining minor modes or associating files to specific major modes.
You can make your own keyboard shortcuts for all the features in Emacs. Most of them are already made for you but you can cook up your own for your comfort. Let’s take the example of ido which we had just discussed. Isn’t it very tedious to always do M-x and then type ido-mode? how about associating a single key to toggle it for you? look at the following line from my init file. (global-set-key [f12] (quote ido-mode)) You guessed it right. Now next time you wish to use ido mode, just press f12. All the tediousness is just gone.
I believe now the importance of configuration has become very obvious to all my readers. Also I hope I convinced you about how easy it is to do these customizations. All in all, with all the built-in keyboard shortcuts already present, combined with the editing and search capabilities and your own combination of minor modes, you get an equally or in some cases more powerful IDE than most popular ones. And hey, I did not even mention yasnepit which gives you complete code templates apart from the auto completion which company-mode provides. So let’s say you type a . after a certain object. YOu get list of auto completion options. You can select one by pressing tab or hitting enter. Pressing tab again mostly gives you a complete code template. In the configuration I have provided, this works particularly well with jquery. There is parameter description, syntax checking on the fly and several other features which you aught to explore and try your self. I need not repeat again that you can customize every thing to your taste. There is a plethora of packages right from smart syntax checking, parenthesis matching, refactoring, project management and any thing that you can dream of. Listing all of them is beyond this blog and after all “your tool your rule “. Go ahead and give it a try!
Summary and References
As you have mostly realized by now, Emacs is complex, because it is tremendously powerful. You have sky as the limit when it comes to shaping it as per your requirement. This for many comes at a cost. It is all about time investement to get the huge profit in terms of programming efficiency. And mind you, if you find that some thing you wish can’t be done in Emacs, then don’t hesitate to research a bit. Chances are that you might find a better alternative for your requirement. If after that you still don’t find what you want, then you may as well use another tool. I am not an Emacs evangelist and don’t take it as a religion. VIM is great as well and may be some might still fine Eclipse or Atom even more powerful. But After so many years of programming, I can say one thing with great responsibility. Emacs has every thing that a professional, busy programmer needs for challenging work.
To get started you can read the beginner’s manual here which has more than 1 part. That in turn will give you all the comprehensive documentation along with a wonderful quick start guide. For those who wish to know more on Python related configurations (I already have it in my collection ), go to the article http://onthecode.com/post/2014/03/06/emacs-on-steroids-for-python-elpy-el.html for details. As an exercise research on web-mode which we have referred to time and time again. Again, configurations and the needed package is there in my collection, ready for you to use. Make sure you see the init.el file at least few times to know what it does. You can perhaps customize the keyboard shortcuts. The git users who have their code on gitlab or github also have a good news! We have magit as a complete tool kit for handling your work. Right from your commits till pulling or pushing code, making branches etc. every thing is present. For those who wish to have a quick glance at all the basic commands here is the cheetsheet. And yes, finally here’s the link to my collection. Download Emacs ToolBox to and extract the contents in a folder called ‘.emacs.d’ in your home.
Now just start emacs and may the power be with you.