For almost a year now, I have been using Spacemacs, an Emacs distribution designed to make Vim users and newcomers to Emacs comfortable in Emacs, as my daily text editor. I was initially hooked by the nearly flawless Vim emulation, which is the best I have used in any text editor. After I had gotten my feet wet, I fell in love with the pre-defined mnemonic shortcuts and amazing packages that are included in the distribution.
In this article, I am going to discuss a few of my favorite features in Spacemacs and the packages included with it.
Project-Wide Refactoring with helm-ag-edit
Spacemacs provides multiple ways to make project-wide edits, but my favorite by far is through the use of
helm-ag-edit. This allows you to search for a term and compile every line that fits that search term into a single Emacs buffer. From here, there are multiple actions you can take:
- Edit each line individually
- Do a simple find and replace in the buffer
- Run a Vim/Emacs macro
In my opinion, this is better than traditional project refactoring since it allows you to make multiple changes at once on each line using macros. Also, since all of the search results are on one page, it makes it easy to notice unwanted changes and fix them. Lastly, none of the changes are final until you decide to save them.
In order to use
- Enter a
SPC /or any related command.
- Search for something you want to edit. This can be regex or just a word.
- Don’t hit enter. Instead enter
- Make your desired changes.
CTRL-c CTRL-Cto save your changes.
If you’re more of a fan of normal project search and replace, Spacemacs offers that as well via K
projectile-replace. Just enter
SPC p R for a normal string search, or
SPC p % for a regex search.
File Navigation with avy
avy is an Emacs plugin (included in Spacemacs) that allows you to easily jump to places in a file. It provides simple shortcuts that allow you to jump to characters (
avy-goto-char), words (
avy-goto-word-or-subword), and lines (
avy-goto-line). This is better than traditional searching with backslash since you can jump directly to the desired point.
avy-goto-char can be used by pressing
SPC j j, then pressing the character that you want to jump to. After this, every occurrence of the character you pressed will be highlighted with a string of characters. To jump to the desired character, press the sequence of characters that is highlighting the location where you want to go.
Git workflow with Magit
One of my favorite plugins with Spacemacs is the Git integration Magit. It provides all of the common Git functionality in just a few keystrokes. On top of that, it provides visual interfaces that are extremely helpful through diffing, checking untracked files, and handling merge conflicts.
The first step to utilizing Magit is to enter the Magit status window by pressing
SPC g s. In this window, you are given a list of all of the unstaged and staged files. You can stage/unstage them by hovering over them and pressing
Commands can also be entered in this window. For example, if you wanted to stage and commit all unstaged files, then push them to the current branch, you would press
c -a c, type and save your commit message, press
p p, then hit enter. This will commit your changes and push to the remote.
If you want to find out how to do more commands with Magit, either look at the documentation, or press
SPC g m to see a help menu with a list of guided commands.
There are many more features built into packages included in Spacemacs. If you’re a Spacemacs user, or you are thinking about giving it a try, I highly recommend taking the time to read the documentation and looking for ways to optimize your development workflow.