Learning and setting up Emacs can be intimidating and while some folks find much use in using a pre-configured setup like - Emacs Prelude, I personally think it is better that someone new to Emacs installs each and every module himself and learns from it. I am assuming OSX as development environment here but the instructions should work well enough on Linux as well.
-
Install Emacs : Install Emacs using Homebrew. I personally do not recommend using Emacs for OSX distribution. You can find out why here.
brew install emacs --use-git-head --cocoa --srgb
There is a bug in Emacs HEAD that prevents it to be working with Cask and thus try not to use Emacs HEAD.
-
Install a Package Manager : I use pallet for managing various packages. Think of pallet as Bundler/Maven for Emacs. Follow instructions available in section For newly installed Emacs. Once you have pallet installed each package you install via
package-install
command will automatically update theCask
and you can checkin the file in version control system and carry it around.Sometimes you have to install a package directly from git. I personally use Git Subtrees for adding packages from git. Feel free to use
submodules
or directly copying files as well. -
Structuring your Customization : Over last couple of years I personally have been using following style of structuring my emacs setup.
.emacs.d/ init.el -> The real .emacs file. custom/ 01ruby.el 02org.el 03auto-complete.el 04web-mode.el
-
The .emacs file : Contents of
init.el
looks something like this:(require 'cask "~/.cask/cask.el") (cask-initialize) (require 'pallet) (add-to-list 'load-path "~/.emacs.d/custom") (add-to-list 'load-path "~/.emacs.d/other_paths) .. .. (load "00common-setup.el") (load "01ruby.el") (load "02org.el")
-
Global Config : I use a lot of global configuration stuff which isn't mode specific, some important ones are:
;; Navigate between windows using Alt-1, Alt-2, Shift-left, shift-up, shift-right (windmove-default-keybindings)
;; Enable copy and pasting from clipboard (setq x-select-enable-clipboard t)
You can see complete file on, 00common-setup.el. If you are completely new to Emacs you should use
C-h t
(That is press Control-h and then t) to launch inbuilt Emacs tutorial from within Emacs.
Packages You need
For complete list of package see my Caskfile. Also some packages I use straight from git and hence may not be listed in Caskfile. With that out of the way, I will try to cover most important packages I love and use everyday:
-
auto-complete : You can install it via
package-install
and below is my configuration ofauto-complete
mode:(require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "~/.emacs.d/.cask/24.3.50.1/elpa/auto-complete-20130724.1750/dict") (ac-config-default) (setq ac-ignore-case nil) (add-to-list 'ac-modes 'enh-ruby-mode) (add-to-list 'ac-modes 'web-mode)
It gives me access to all powerful auto-completion system for Emacs. Below is a screenshot.
-
ag : You should use silver searcher and corresponding Emacs mode ag. They beat likes of
ack
orgrep
hands down. -
enh-ruby-mode : Personally I am big fan of Enhanced Ruby Mode. It gives you better syntax highligting, better indentation schemes and code intelligence without running
flymake
kind of thing.You can see rest of my enh-ruby-mode specific configuration in file 01ruby.el. You will notice that I have included a mini
rspec-mode
there , which allows me to run specs of a complete file or just spec under the cursor. -
smartparens : smartparens mode is too much of a moving target and hence I always use it from git. My
smartparens
configuration is:
SmartParens mode is many things. It automatically inserts closing parethesis, tags, end's depending on major-mode. Highlights them, allows you to move between them. Allows you to wrap existing texts. However you may not even have to go through their documentation, it mostly just works.
-
Projectile Mode : Projectile is one among many ways of handling projects in Emacs. My projectile configuration looks like:
(require 'grizzl) (projectile-global-mode) (setq projectile-enable-caching t) (setq projectile-completion-system 'grizzl) ;; Press Command-p for fuzzy find in project (global-set-key (kbd "s-p") 'projectile-find-file) ;; Press Command-b for fuzzy switch buffer (global-set-key (kbd "s-b") 'projectile-switch-to-buffer)
-
Rainbow Mode : Add color to your css/scss. Check screenshot below:
- Robe Mode : Robe mode is what makes Emacs full featured IDE even comparable to likes of RubyMine etc.
Robe can also integrate with auto-complete
mode to provide more intelligent auto-completion.
I personally do not use that feature because it makes things slower.
-
Highlight indentation for Emacs : Highlight Indentation mode allows you to see indentation guides. My setup for the mode is:
(require 'highlight-indentation) (add-hook 'enh-ruby-mode-hook (lambda () (highlight-indentation-current-column-mode)))
(add-hook 'coffee-mode-hook (lambda () (highlight-indentation-current-column-mode)))
-
Yasnippet : Yasnippet is Emacs snippet library. Out of box it won't work with
enh-ruby-mode
because it searches forruby-mode
. You can make it work by copyingruby-mode
snippets toenh-ruby-mode
folder. -
Flyspell : If you are like me, you make lot of typos in code comments, strings etc. Using
Flyspell
mode you need not worry about it - interfering with programming language keywords etc. My setup forFlyspell
is :(require 'flyspell) (setq flyspell-issue-message-flg nil) (add-hook 'enh-ruby-mode-hook (lambda () (flyspell-prog-mode)))
(add-hook 'web-mode-hook (lambda () (flyspell-prog-mode))) ;; flyspell mode breaks auto-complete mode without this. (ac-flyspell-workaround)
If you are on OSX you may have to run
brew install ispell
to make it work. -
Rinari : With Projectile mode, I am finding less and less use of this mode. But this is your canonical rails mode for Emacs. Get it via
package-install
or Rinari Github repo. -
dash-at-point : If you are on OSX you should totally use Dash. Using dash-at-point you can view documentation of methods, classes etc right from Emacs.
-
multiple-cursors : Multiple Cursors is Emacs answer to multi-line editing feature of Sublime etc. Anything I can add here is probably better covered in this Emacs Rocks screencast.
-
textmate.el : textmate.el allows me to quickly move between methods in a file, independent of programming language mode (and without ctags). It has many other utilities.
-
WebMode : For a long time Emacs users were mostly stuck with half-assed multi-language mode when editing html templates. WebMode is like breath of fresh air for those who mix - ruby/js/css/coffee in their HTML!
Apart from aforementioned modes, I still use too many other modes - sometimes even unknowingly! Some modes that deserve honourable mention but I don't use them is - Magit, Expand Region. If you do lot of Javascript then have a look at - Swank-js. If you are coming from Vim and looking for decent nerdtree replacement, you can try Dirtree or ECB or Project explorer.
Learning Emacs in a way is - as deep as you are willing go. For resources I recommend:
I hope you enjoyed this article and if you want to keep updated with latest stuff we are building or blogging about, follow us on twitter
@codemancershq
.