DEV Community

Cadu Ribeiro
Cadu Ribeiro

Posted on • Originally published at thecode.pub on

An Introduction into Spacemacs from a Vim user

I’m a Vim user. I use it in my workflow all the time, but I enjoy trying other editors sometimes and I will share my experience playing with Spacemacs.

What is Spacemacs?

Spacemacs is a starter kit for Emacs that makes it easier for anyone to use it. It has the Evil plugin by default. Evil is a layer for Emacs that emulates Vim. I always try vim mode in all other editors and they all are not so good, Evil is one of the best Vim emulators that I’ve already seen. It makes it easier for me to use Emacs, since it emulates Vim commands that I already know.

Spacemacs has easy to remember keybindings. Everything starts with the SPC key (it is like the leader key from Vim) and all belongs to some group. For example: SPC b for all buffers commands and SPC p for all project commands. If you don’t remember the command, press SPC SPC and it brings an autocomplete buffer to search for commands.

How do I experiment another editor?

It’s hard for me to learn all the stuffs from a tool in a day or two. So when I want to learn more about an editor, I force myself to experiment it for at least one week, trying to do everything on the new editor without opening others. To use an editor I need to learn how to search files, how to navigate through files and how split the window, only this.

Installation

First, you should install Emacs.

You can use Homebrew to install emacs on mac:

brew install emacs --use-git-head --cocoa --srgb
Enter fullscreen mode Exit fullscreen mode

To install on Linux, you can follow this documentation.

Now you can install the Spacemacs distribution:

git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d
Enter fullscreen mode Exit fullscreen mode

Now open Emacs..

Spacemacs Startup Screen

The Basics

Spacemacs has the concept oflayers . The idea is to have a collection of packages with some configurations. The documentation says:

Layer: A collected unit of configuration that can be enabled (or disabled) in Spacemacs. A layer typically brings together one or more packages, as well as the glue configuration code required to make them play well with each other and Spacemacs in general.

Once you open Emacs, enter SPC f e d, this will take you to Spacemacs configuration file (~/.spacemacs). Here you can add layers inside dotspacemacs-configuration-layers. For example:

dotspacemacs-configuration-layers
 '(
 elixir
 (ruby :variables ruby-version-manager 'rvm)
 ruby-on-rails
 )
Enter fullscreen mode Exit fullscreen mode

This adds layers for working with Elixir, Ruby and Ruby on Rails

Navigating through a file

To navigate on the file, I use the Vim commands.

gg =\> goes to the top of the file
G =\> goes to the bottom
h =\> left
j =\> down
k =\> up
l =\> right
Enter fullscreen mode Exit fullscreen mode

(to learn more about navigation, read here)

Loading your project

After loaded, you can use SPC a d (or File -> Open Directory) to open your project.

It will load this buffer and you can find your project file. Hit Enter to load it

Spacemacs includes Projectile, an Emacs library for handling projects. So, after you added your project the first time with SPC a d , you can use SPC p p to Switch between projects

Terminology

I like to use this image to understand Emacs terminology

https://emacs.stackexchange.com/questions/13583/whats-the-difference-between-a-buffer-a-file-a-window-and-a-frame

Usually in most applications we call the running instance as window, but on Emacs we call it the Frame . A frame can contain multiple Windows and inside each window we have a Buffer loaded (on the image above, the buffer loaded is indicated by the yellow circle). A buffer can be the content of a loaded file or the output of some command execution. Every time you open a file, it changes the active buffer to this file, but the buffer with the past file opened is still alive. You can use SPC b b to see all buffers and switch them

You can easily switch between the last used buffer with SPC TAB

Searching for a file

The first thing that I learn in a new editor is: How can I search for a file?

In Spacemacs we can do a fuzzy file search pressing SPC p f

Fuzzy file search

Another way is to search for Classes and methods using Tags. SPC p G will generate a TAGS file for your project. After generated, you can search through tags with SPC p g

Searching by Class/Method Name using Tags

You can also open a tree explorer using SPC p t

SPC p t will open the tree explorer

Splitting window

Another feature that I use a lot is splitting the window. You can use the Emacs keybinding to do it:

SPC w - =\> Split window into two windows, one above the other.
SPC w / =\> Split window into two windows, side by side.
Enter fullscreen mode Exit fullscreen mode

Or using the vim commands:

:sp =\> Split window into two windows, one above the other.
:vsp =\> Split window into two windows, side by side.
Enter fullscreen mode Exit fullscreen mode

Splitting the window

After splitting, you will notice that a number is defined in the window

To jump into a window, you can press SPC NUMBER to go to that window (for example, SPC 3 take me to the right window on the image)

Switching between windows

You can kill a window with SPC w d or :q .

Sometimes when working on more than one window in the same frame (for example, having a class and a test file opened together), I like to maximize one of them. You can use SPC w m to maximize and use it again to return it.

My Workflow working with it

Usually, my workflow with Vim involves Tmux too. I typically have 3 Tmux panes open. One for the editor (Vim), another for the terminal (to run tests, ssh, etc), and one for the server (and the logs).

My workflow with Vim and tmux

I also like to use vim-test to run my tests inside Vim

Emacs for another side, have the idea that you can do everything on the Emacs itself. You can start your server on it, open a terminal, etc, without having the needing to have multiple terminals for each thing (although I still have they open when working on Emacs 😄)

My next examples use the rails layer on Spacemacs.

I can run bundle install (to install my ruby dependencies) inside it with SPC m b i

I can run the rails server inside it and check the logs:

and also run the tests directly from Emacs:

Follow a simple mindmap that I created for these basic commands:

And this is all the basics that I need to use it and that I want to share.

Cheers 🍻


Top comments (1)

Collapse
 
maxdevjs profile image
maxdevjs

Great article, I decided to give a shot to spac(emacs) and your nice guide helped. Some keybinding are not working as exposed here, but not a big deal. Obrigado :)