Getting Started With Emacs

6 minute read

I have been using emacs on my ubuntu for over 9 months now and I absolutely love it. Emacs is a text editor but in reality it is a lot more than that. With some customization it can transform into a full-fledged IDE.

This post is intended for people who would like to use emacs. I have mostly included all the customization that I found amazing and would have been much happier if I had known about these functionalities much earlier. For most cases I will provide specific links or keywords and simple googling should be sufficient to get how to install a specific plugin and perform the corresponding customization.

Emacs

As suggested earlier Emacs is a text editor but it has a lot of functionalities and a lot of customizations can be performed. There is a .emacs file and a .emacs.d folder in your $HOME directory which will contain all the customization. If you are working on a different computer, simply copying these two files to the $HOME directory would be sufficient in most cases. Emacs offers both GUI as well as terminal editing mode. The latter simply means that even without a GUI you can use emacs directly from your terminal. Another thing that I really like about emacs is the automatic indentation for many types of files especially C++.

Emacs is available on linux, mac as well as on windows.

Note: The installation part would be differnt on windows, linux and mac. Since I am using Ubuntu 16, I can only suggest how to install on ubuntu. But everything post installation should carry forward to other OS as well.

Aside : Other Editors

While I do want to talk about Emacs, it would be unfair to not suggest other powerful editors as well. So here are some other great editors in no particular order :

  • Vim
  • Sublime Text
  • Atom

All of the above are quite elegant and each of them has its pros and cons. If you are still unsure of which editor to use do try all of them at least once.

Disclaimer: These are the only popular editors known to me. If you would like to include any new editor feel free to drop a mail.

Tid Bits

Before I start talking about any installation procedure or customization I would like to point you to the website Emacs Wiki and Emacs Stackexchange which are invaluable resources for working with emacs. Also there is the book Mastering Emacs which has some amazing tutorials. Be sure to check them out after this blog post.

Installation

Easiest way to install emacs on ubuntu would be

sudo apt-get install emacs

Confirm which version of emacs is installed

emacs --version

My version is 24.5.1

Getting Started

If this is the first time you are using emacs I highly encourage you to spend 20-30 minutes on the emacs tutorial. Start emacs in one of the following ways:

  1. Press the windows button to get the dash board and search for emacs GUI.
  2. On terminal type (emacs &) or (emacs file.ext &) If you would rather use emacs on terminal simply replace emacs with emacs -nw

Note : We add the & symbol so that you can continue using the terminal. The brackets are present so that the emacs process is detached from the terminal, so even if the terminal is closed you can still continue to use emacs.

Aside : Notation

Notation: C here means control key and M here means alt key. This is the notation that emacs uses and I am going to follow that notation

Emacs tutorial

After your emacs is running simply press C-h t. This should start the emacs tutorial for you. It should take around 20-30 minutes to finish this and you should be familiar with most of the shortcuts by now. I will just say a few shortcuts which I consider to be extremely important :

C-/ : Undo

C-k : Cut the current line

M-d : Delete the current word

C-a : Go to start of line

C-e : Go to end of line

M-x- : There are a lot of available options after this. Just type what you wanted to do, and most likely there would likely be the suitable option.

Customizations

Now that you have basic knowledge of emacs lets start customizing a bit. Some basic customizations like theme and font sizes can be directly done using Options -> Customize Emacs. This would add some lines into your .emacs file. Try not to change these options from this file, though other customizations can be made into this file itself.

Now here are some things that you should start doing. (Note : Most of them are hyperlinked to blog posts which detail on how to use that package).

  1. Melpa Packages : In simple words this allows you to install packages with ease. I strongly recommend using this as it makes installing packages very easy. For example to use a package all you need to do is :

    M-x package-install RET package

  2. Ido-Mode : It means interactively do things. While in vanilla emacs you have C-x C-f to find file in current directory or create a new one there is no autocompletion of file which you might often require. Ido-mode gets around this and autocompletes the file name and it is honestly quite amazing.

  3. AucTex for Latex : If you are working with latex then emacs is genuinely the best editor because it provides amazing flexibility and great visuals for previewing. One of the primary reasons I have stuck with emacs is because how elegant it is to write documents in latex.

  4. Elpy for Python : While emacs has default python-mode it is nowhere near to amazing capabilities that elpy provides. There is automatic completion for parameters and function definitions in the buffer as well as simple error mistakes shown in red-line. This makes emacs better than a lot of IDEs for python. There is also ipython shell that you can use inside of emacs itself!

  5. Magit for version control : This is easily one of my favorites. Version control had never been simpler. Small tip : consider using a custom key board binding for magit status using :

    (global-set-key (kbd "M-m") 'magit-status)

    This sets Alt-m as a shortcut for magit-status. You can ofcourse change it with any other custom shortcut you feel comfortable with. Once in magit-status you can simply stage files by typing s in front of the particular file you want to git add. Type C-c C-c to commit and type a message and then again press C-c C-c to commit. Finally P p to do git push. To do git pull simply do F p. Extremely simply isn’t it?

  6. Set Keys for Commenting : A useful line to add to your .emacs file would be

    (global-set-key (kbd "M-;") 'comment-dwim-2)

    This sets the key Alt-; for commening. You can always set which symbol to use to comment when opening a new type of file

  7. Removing Whitespaces while saving : I hate trailing white spaces. Add the following line to .emacs file:

    (add-hook 'before-save-hook 'whitespace-cleanup)

    When you save using C-x C-s it removes all the trailing whitespaces :)

  8. Autocomplete for Text files: Another awesome package (available via melpa). Allows autocompletion of simple text files but I particularly like it for C++.

That’s it for my customization!

Extra tips

Using version control to save .emacs file. Many people like to keep a repository to keep their .emacs file. I haven’t particularly felt the need to do so, since I simply copied my file from one system to other as I use a small number of systems. But it might be worth looking into if you are doing heavy customization.

That’s it folks!

If you have any comments for my post feel free to shoot me an email at ark.sadhu2904@gmail.com.

Updated: