Clojure Journey III – Emacs for Beginners Course

On my last post I talked about why I chose Spacemacs to be my text editor while coding Clojure, installed it and made a nice first setup. In this post I’ll talk about some workflows/commands that I think is important to have a great and smooth coding.

This post probably will be one of these posts that keep opened in your browser and you read it everytime that you need to remember something.

Neotree

First let’s talk about our tree explorer, on the last post I installed the neotree that is similar to NERDTree(from vim), and started to using it.

The first thing that I did was to bind the open/close of it to F8 key with the fallowing code:

(global-set-key [f8] 'neotree-toggle)

Now just press F8 and your neotree will open.

Opening neotree

To navigate inside Neotree just use your moving keys (arrow keys or HJKL), if you have any question about it, you can just type ? and a nice window will be displayed with everything that you may need:

Neotree help page

And this image saved me from writing at least 500 words, this image has everything that you will need related to Neotree.

If you don’t want to open neotree with F8 you can open by pressing SPC + f + t.

Window

Split windows is a common move to every programmer, to split is easy, just press ESC to go back to visual mode (if not already), and press: SPC + w + / to split vertically or SPC + w + - to split horizontally, where SPC is your space key. If you don’t want to use SPC, M-m + number will work too.

M-<letter> stand for alt + m

Spliting Emacs Window

To navigate between window is simple, just press SPC and the number of the window that you want to go, like SPC + 2 to go to window 2. But how to know the window number?

Spacemacs window numberr

If you’re using spacemacs, your window number will be displayed by default on the bottom left corner of your window, as the pink arrows of the image is pointing. The current window will always be in different color (as the example, window 2).

Opening/Searching Files

Open using neotree

If you want to open a file inside a window, the easiest way is to open using Neotree, just go to the window that you want to have a file opened, open your neotree, navigate to the file and open it.

Opening file by Neotree
Project search aka Ctrl + P

But if you don’t want to search for the file in your neotree, you want something more like the famous ctrl + p command from other text editors? In spacemacs with helm (the setup that I’m using), it’s very simple, just type: SPC + p + f and a window will open, just write the name that you’re searching for, and press ENTER to open in the current window.

Searching for a file using spacemacs and helm projectile

Buffers

Now we know how to manage our windows, is time to understand and manage buffers! Technically… Windows displays emacs buffers!

Everything that you open inside emacs resides on what is called “buffer”, every code, really everything! And the plot is, if you close your file/window with the traditional :q! (if you use vim mode), this code isn’t close at all, the buffer still alive!

Too see your buffers just type C-c + b and a window will open with a list of all buffers.

C-c stands for ctrl + c.

List buffers

But this window is not very useful…If we want to open a buffer the best way is to use the buffer search (similar to the file search that we see previously), just press C-x + b and you’ll have a full interactive search for buffers:

Helm buffers find

Just search for the wanted buffer and press enter to open it. If you use C-c + o it will open in a splitted window. (If you just want to open in the current window, just type enter)

Search on spacemacs bufferr

A nice shortcut is to press SPC + tab to change to the last buffer!

All the buffers keep alive, ok! But how do we kill them? Just press C-x + k select the buffer and press enter!

Search on project

To search for a pattern across all the project is very easy, just type: SPC + / and start writing your pattern:

Global search spacemacs

If you press C-c + o like in other commands, it will open in a splitted window.

Replace

It has a nice built-in way to do search/replace inside project too, it ‘s simple, SPC + p + R (pay attention on capitalized R) . After this it will ask for a pattern to replace, and what to replace, after this it will open a window with the first match, press y if you want to replace and n if no. Do this for all matches.

Search and replace

Search for commands

Emacs comes with a global commands search, just type M-x` and write what you’re searching for.

OFF: Themes

If you want to install more themes is very easy! You can install a layer names themes-megapack (If you don’t know how to install layers read my last post) and you’ll have more than 100 themes to choose.

OFF: Games inside editor

Emacs comes with some games installed, just open the global search with M-x and write the name of one of the games, like, tetris!

Tetris on emacs

Remember: With great power comes great responsibility.

Conclusions

This is a short and beginner-friendly emacs tutorial with all flows that I think is important to get started in a new text editor.

I hope I haven’t forgotten anything important to get started with it.

On the next posts related to Clojure, I’ll be using my spacemacs as my text editor, I’m loving to use it and I want that everyone that trries to start using it, have a great experience too.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Follow my blog to get notified off every new post:

Check my last posts:

Leave a comment