Setting up Magit and using it as a standalone tool

This post will guide you through the steps needed to set up Magit, a great Git porcelain for Emacs, to be used as a standalone app.

For that, you will need to setup Emacs, Spacemacs, Git and Magit (Duh).

While Spacemacs is not required to run Magit, it's a great configuration bundle for Emacs and will provide you with a nice usability boost from the base emacs settings.

I'm assuming zero Emacs experience, so if you have used Emacs before, this may not be the post you are looking for.

Installing Emacs

You will need to install Emacs and Git. If you are running Debian, Ubuntu or other derivatives, just open a terminal and type:

$ sudo apt install emacs git

Have in mind that Emacs 24.4 or newer is needed. If you are running Ubuntu 14.04 or older, you will need to upgrade.

Once Emacs is installed, you can proceed to download Spacemacs.

Installing Spacemacs

Let's move your old .emacs.d/ out of the way (you can skip this part if you have never used Emacs before):

$ mv ~/.emacs.d ~/dot.emacs.bak

Then clone spacemacs:

$ git clone --depth=1 ~/.emacs.d

And fire up Emacs!

$ emacs

The first time you start Emacs, Spacemacs will boostrap itself and after a few moments it will ask you to choose your preferred editing style. If you already are familiar with Vim and like modal editing, go for Vim, otherwise you can choose the Emacs style. To do that, just use the left/right arrow keys to highlight the desired mode and hit RET.

The next question will be about the distribution type you want to use, just choose spacemacs (the default one) by pressing RET again. Some more packages will be installed.

Once this process is finished, you will be left with an all-new and shinny spacemacs setup.

Let's add Magit to the mix!

To do that, you will need to open the .spacemacs config file and enable the git layer. Press <SPC f e d> (that is: space, then f then e then d in Vim style or <M-m f e d> (that is Alt+m, then f then e then d) otherwise, and the config file will be opened for you.

Scroll down until you find the layers section:

     ;; ----------------------------------------------------------------
     ;; Example of useful layers you may want to use right away.
     ;; Uncomment some layer names and press <SPC f e R> (Vim style) or
     ;; <M-m f e R> (Emacs style) to install them.
     ;; ----------------------------------------------------------------
     ;; auto-completion
     ;; better-defaults
     ;; git
     ;; markdown
     ;; org
     ;; (shell :variables
     ;;        shell-default-height 30
     ;;        shell-default-position 'bottom)
     ;; spell-checking
     ;; syntax-checking
     ;; version-control

and uncomment the git layer by removing the double semicolons in front of it.

To make the whole experience a lot more snappier, scroll down until you find this line:

dotspacemacs-persistent-server nil

and replace nil with t.

Tell spacemacs to reload its config file by using <SPC f e R> or <M-m f e R> and everything required will be downloaded and installed for you.

From now on, typing <SPC g s> or <M-m g s> in Emacs will pop up Magit's status window if you are editing a file contained in a Git repository or pop up a directory selector so you can browse to the Git repository you want to manage if not.

Using Magit as a standalone tool

In case you are happy with your current text editor/IDE and only want to use Magit as a standalone tool and ignore the rest of Emacs. Let's create a small script to jump directly into magit:

Run the following commands in a shell:

$ mkdir -p ~/.local/bin
$ echo 'export PATH=$PATH:~/.local/bin' >> ~/.bashrc # or ~/.zshrc
$ source ~/.bashrc # or ~/.zshrc
$ echo 'emacs --eval "(progn (magit-status \"'\$PWD'\") (spacemacs/toggle-maximize-buffer))"' > ~/.local/bin/magit
$ chmod +x ~/.local/bin/magit

Now go to any Git managed directory and execute magit from the shell.

That's all for now. In a future post I'll do a quick introduction on using Magit but for now press the ? key on the Magit status buffer to see the available actions.

Oh, and to quit emacs, just press <SPC q q> or <M-n q q>.