Spacemacs – Among the stars aboard the Evil flagship (vim)

Screen Shot 2018-04-09 at 6.23.06 PM

I’ve been using Spacemacs as by IDE/editor of choice for about half a year now. I absolutely love it! It’s basic premise is that

“The best editor is neither Emacs nor Vim, it’s Emacs and Vim!”.

In this post, I will try to explain briefly what it is and how I use it in a way that is newbie friendly.

Background

I’ve been programming for over 10 years now, professionally for about 4 years. I’ve worked in a variety of different languages over time – HTML/CSS/JS, C/C++, PHP, Python, Java and most recently Clojure.

Till now, I did not have a consistent IDE/Editor. For web development, I used to use Sublime and later Atom. For Python, PyCharm. For Java, I used to use Eclipse and switched to IntelliJ later (I still use IntelliJ + IdeaVIM for Java as I will explain later). I used to use basic Emacs in college for C/C++ projects and basic Vim at work whenever I needed to quickly make small changes to files when sshed into servers.

The original reason I made the switch to Spacemacs was because I couldn’t find a good IDE for Clojure elsewhere. CounterClockwise, the Eclipse Clojure plugin is not being maintained actively. Cursive, the IntelliJ plugin is paid. The most recommended/actively developed/open-source IDE for Clojure Cider (Clojure(Script) Interactive Development Environment that Rocks) was available only for Emacs and its derivatives such as Spacemacs.

cider_repl
Clojure development using CIDER, complete with a fully featured REPL having auto-complete and doc-string tooltips.

However, now Spacemacs in Evil (Vim) mode has become my IDE/editor of choice for most languages and for any editing outside programming as well. Spacemacs makes it a lot easier to get to a really productive state without having to do a lot of research online, tuning your dotfiles, installing plugins etc – it comes with batteries included!

The Vim Modal editing

Spacemacs supports 3 modes of usage:

  1. Among the stars aboard the Evil flagship (vim)
  2. On the planet Emacs in the Holy control tower (emacs)
  3. A hybrid mode combining 1 and 2

If you are new to both Emacs and Vim, I strongly recommend using Spacemacs in Evil (Vim) mode. Emacs/Hybrid mode needs to be considered only if you are already comfortable with the Emacs keybindings.

Vim or its modal editing lets you edit text at the Speed of Thought. Many people argue that coding is more about thinking and less about churning out code, and that the minimal time savings you get from optimizations such as Vim is negligible. I disagree:

  1. Vim helps you make editing as least intrusive to your thinking as possible
  2. The small savings add up across your profession as a coder.
  3. As a programmer, having a hackable/fully-programmable editor gives you the flexibility to program your editor as per your needs.

Sylvain Benner, the creator of Spacemacs has written an excellent short piece on ‘Modal editing – why and how‘ that I highly recommend. In fact, I recommend going through that whole article (Beginner tutorial) if you are new to Spacemacs.

Vim itself has a steep initial learning curve. But it pays off greatly after the first few weeks if you write code as a profession. Some helpful/interesting Vim resources to get you started:

  1. Why Vim? – http://www.viemu.com/a-why-vi-vim.html
  2. Vim interactive tutorial – http://www.openvim.com/
  3. Vim adventures (interactive game) – https://vim-adventures.com/
  4. Vim Creep (story) – https://www.norfolkwinters.com/vim-creep/
  5. Vim cheatsheet – http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

There are lot of good video tutorials on Vim in Youtube too if that’s what you prefer. Here is one I liked – Learning Vim in a week (link).

In short, Vim lets you edit text similar to how you think about editing text. Examples:

  1. Typing ciw lets you change the current word (change in word)
  2. Typing di” lets you delete everything within two double-quotes (delete in “)
  3. Typing dt> lets you delete everything till > (delete till >)

You get the idea. These are executed in the normal mode, where you will spend most time. There is much to be said about the composability of vim commands too (some other time).

Pro tip: It is very highly recommended that you remap your Control (Ctrl) key to the Capslock key once you start using Vim. You won’t need to move your fingers from home row much with this change. For Mac, I used Karabiner to remap the keys.

Why Spacemacs?

Emacs,  “a great operating system, lacking only a decent editor” – http://en.wikipedia.org/wiki/Editor_war

At the same time, Vim is said to suffer from a less than ideal scripting language and window management capabilities. Spacemacs gives you the best of both worlds.

Two primary reasons which I went with Spacemacs were:

  1. Batteries included
  2. Mnemonics

Batteries included

This was the biggest factor for me. I’ve tried to transition into using Vim in the past. To get to a really productive dev environment, you need to spend significant time going through various forums, identifying the plugins you need, installing them using some package manager like Vundle etc. This can be quite daunting for a newcomer.

Spacemacs has this concept of layers. For example, each language has its own layer. When you install a layer, Spacemacs installs the best community-curated plugins for that language for you. Even better, it auto-detects which layer to install when you open a new type of file and asks you whether you want to install it. A simple ‘y’ and you’re set to go!

The layers that I have installed as of April 2018:

dotspacemacs-configuration-layers
 '(ruby
 php
 csv
 yaml
 clojure
 javascript
 python
 java
 c-c++
 markdown
 html
 ivy
 (auto-completion :variables
                   auto-completion-enable-help-tooltip t
                   auto-completion-enable-snippets-in-popup t)
 better-defaults
 emacs-lisp
 git
 markdown
 org
 (shell :variables
        shell-default-height 30
        shell-default-position 'bottom)
 ;; spell-checking
 syntax-checking
 version-control)

Mnemonics

Another issue in that I had faced in the past was learning and memorizing all the keyboard shortcuts. Vim/Emacs has evolved over more than 20 years now. Different plugins were created at different points of time. Hence there is no consistency or logical grouping for these.

Spacemacs solves that by taking all the plugins/shortcuts and logically grouping them by mnemonics. Spacebar is the leader key (main key for issuing any command). That itself is an improvement over other editors which strains your little fingers and can result in carpal tunnel syndrome (CTS).

To give some example of how mnemonics work:

  1. SPC w gives you all the window related commands.
    1. SPC w d deletes a window
    2. SPC w m maximizes a window
  2. SPC b gives you all buffer related commands.
  3. SPC g gives you all git related commands.

Spacemacs comes with the which-key plugin which shows a helpful menu whenever you press SPC so that you don’t need to memorize all the shortcuts.

spacemacs_which_keys
Spacemacs which-keys plugin helps find what you are looking for

If you are interested in learning more about Spacemacs, I highly recommend the Spacemacs ABC tutorial series by Eivind Fonn – Youtube playlist link. It is a series of 8 videos, in each of which Eivind explores one ore more keys under SPC in detail.

Some cool stuff

These are some of the cool features that made the whole experience more fun.

Magit

Magit is a plugin for git version control. Till now, I generally used to prefer command line git for all version control use-cases. But the 3 window Ediff merging capability of Magit is something that sold me completely. Resolving merge conflicts were never so much fun!

magit
3 window Ediff merging using Magit. Courtesy: Magit Ediff tutorial

Undo Tree

Shortcut: SPC a u

spacemacs_undo_tree
Undo tree lets you browse through all past changes

With most modern editors, we are used to a linear timeline of changes. If you go back to your past change and modify something, you effectively lose history of all the things you did after that change. With the undo tree, your entire history is preserved and you can easily navigate to any point in time including any branching in history you did.

Vim Macros

You can record and replay commands using Macros. A good introduction here – link. This can be pretty powerful if you need to do something repetitive in a smart way.

Shortcut to record: q<letter><commands>q
Shortcut to replay: <number>@<letter>

You might think these are pretty esoteric tools and of not much practical use. I too though so until I came across multiple situations where I was able to use them effectively.

Vimium (Bonus)

Once you get to using Vim, you will resent having to take your hands off the home row (where hjkl keys are situated) at any time. Moving your hands to the trackpad could feel like too much work.

Worry not, Vimium comes to the rescue – https://vimium.github.io/.
Its a plugin for Chrome (you have Vimari for Safari, not as feature-rich as Vimium) that lets you navigate the web using your keyboard, mostly with keys in the home-row.

vimium.png
Vimium lets you navigate the web using your keyboard

Being Pragmatic

Even though I’ve talked so much about Spacemacs, I have realized that it might just not be the right tool for some cases. In particular, for me, it was not working for Java development.

I tried all the options available in Spacemacs. Eclim, which comes with the Java layer, essentially runs a headless eclipse process in the backend. However, it is pretty slow. Other options like malabar, jdee or meghanada were either outdated, not being actively developed or not working. It’s difficult to write Java without proper auto-complete or go-to-definition etc. features.

I found that IntelliJ team offers an IdeaVim plugin that lets you use IntelliJ with Vim keybindings. This along with the distraction free mode (Cmd + I) gave me a near similar experience to Spacemacs, but with all the Java goodness.


I am still learning a lot in both Vim and Spacemacs. I hope to update this post with more interesting/useful features that I uncover over time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s