This is Part 2 of “My Emacs For Scala Development. You will find Part 1 here.
So far we’ve learned how to open a new file (`C-x C-f`). Each time we open a new file, buffer is created. To get the list of all opened buffers you will have to `C-x C-b`. To split current window (in order to see two different buffers at the same time) you will `C-x 2` (for horizontal split) and `C-x 3` (for vertical split). To switch between splits you will have to `C-x o` and to go back to full-window mode run `C-x 0`. Finally to switch quickly to the buffer on left(or right) of your given buffer, you will `C-x left-arrow` or `C-x right-arrow` accordingly.
You will quickly learn that the default configuration described above… it, well, it simply sucks! You might wonder “What? Why? It gets the job done, what more do you need?”. The answer is: “The Flow”.
Whenever we are working with Emacs, we are targeting to grab what some people call “flow”. An efficient, smooth and fast way to craft your code. In ideal state, your hands are steady, placed in one position, with your palms firmly laying next to the keyboard, never changing their position. While in the same time your fingers gently move around your keyboard, each key being pressed by the right finger. Modes and functions are being executed instantly, providing quickly currently needed functionality. An unfamiliar observer may get a notion of magic appearing in front of his eyes, as code is being produced, shortcut are being used and mouse is never touched by an Emacs user. Getting to that semi-Nirvana state takes time and practice, but it’s worth to aim for it, because “the Flow” is where the true power of Emacs really lies.
Our general goal is to seek that enlightenment. Endeavor true efficiency and speed. Let’s take small steps toward that now.
As mentioned, default keybindings for buffer and window manipulation suck and they will most definitely destroy your “flow” if you don’t do something about it . Almost as if creator of Emacs did it on purpose. Intentionally leaving crappy shortcuts for you (as a user) to change, forced by the pain of your sore and swollen fingers.
Jumping between buffers is one of the most common and frequent thing that you will do while using Emacs. Most of the time the buffers that you use are placed closely to each other and you only need to shift to the right or to the left to reach the buffer that you are currently most interested in. `C-x left-arrow` or `C-x right-arrow` are useful, but they will most likely destroy your “Flow”, as (in almost every keyboard out there) your right hand has to move to the bottom-right side of the keyboard in order to reach the arrows. As arrows will be placed somewhere under your “pinky” finger, you have to take your hand from the table, to actually reach them.
Just because of the reason above, I’ve reconfigured keybindings in my MacBook, so that shifting opened applications can be achieved by `C-[` (to the left) and `C-]` (to the right). Because this configuration proved to be very convenient, I’ve decided to have the “shifting” of buffers available in similar way: under `M-[` and `M-]`
I’ve added following to my `init.el` file:
(global-set-key (kbd "M-]") 'next-buffer) (global-set-key (kbd "M-[") 'previous-buffer)
on a site note: you may argue that reaching ‘Ctrl’ with your left hand will also destroy your “flow” as it shares the same lack of availability as arrows. And I will agree. That’s why I strongly recommend “binding” Ctrl to CapsLock key. Having Ctrl under Caps allows you to reach it easily with your left pinky finger without moving your hands (your finger will probably be laying already on the CapsLoc, ready to be pressed). And let’s be serious, unless you are an Internet troll, when was the last time that you really needed CapsLock? Use it as Ctrl!
Key Chord mode
You’ve already saw how we can bind specific command to key strokes. For most useful and mostly used functionalities you really need a quick access. That when the handy key-chord mode comes in.
For example: to list all opened buffers normally you would run `C-x C-b`. However when working in Emacs you will switch between buffers so often that this function would really come in handy, if we could trigger it with hitting two keys and the same time, e.g `fm`. Let’s see how we can achieve that
First install package key-chord by `M-x package-install RET key-chord`, then add following to your `init.el`
(key-chord-define-global "fm" 'list-buffers)
Restart Emacs or reload init.el (M-x load-file RET ~/.emacs.d/init.el) to see the effect. Now when you press `fm` together, Emacs will list all your opened buffers.
“Helm is like a religion”
Our Emacs looks now a lot better, however it is still cumbersome and far away from the nice environment that we were hoping to get. Fear not, things will get better with the next mode we are going to install – Helm. Helm is an Emacs mode designed to provide completions and narrowing selections. For some Helm is like a religion and they can’t think of using Emacs without it.
After installing Helm (`M-x package-install RET helm`) we will go straight to the `init.el` to configure it.
First we will improve the way we execute commands using `M-x`
(global-set-key (kbd "M-x") 'helm-M-x) (global-set-key (kbd "C-x C-m") 'helm-M-x) (global-set-key (kbd "C-c C-m") 'helm-M-x)
Short explanation of what I did above is required. Normally `M-x` is bound to a `execute-extended-command`. We want to improve the way we execute commands by using the Helm’s equivalent called `helm-M-x`. We also bind `helm-M-x` to two other key bindings for convenience – you will use it relatively often to realise that M-x key stroke is destroying your flow and using `C-c C-m` is just simply faster and more convenient.
With `helm-M-x` we get list of commands that can be filtered. Each listed commands is shown with its key binding (if it exists). We also get most used commands on top for a quick usage.
Listing opened buffers
Before prociding install one addtional mode – recentf. And add following to your init.el
(global-set-key (kbd "C-x b") 'helm-mini) (key-chord-define-global "fm" 'helm-mini)
With above configuration not only do we get a more friendly way to list & search open buffers, but also files that were recently opened. This is brilliant feature that I use almost all the time. Switching between files has never been more easy. Search is incredibly fast, keeping the most recent visited buffers on the top of the list.
Looking for files
Last thing we’re going to add (at least for now) is `helm-find-files` which will replace default find-files provided by Emacs.
(global-set-key (kbd "C-x C-f") 'helm-find-files)
“Can you make that font a little bit bigger?”
I do a lot of tech talks on numerous programming conferences. The most common question that I get from the audience is … “Can you make font a bit bigger?”. So I need to be able to change font sizes and change them fast and easily. Now that feature does not come be default. We need `zoom-frm` mode for that.
First lets install zoom-frm: `M-x package-install RET zoom-frm`. We can now zoom-in and zoom-out by running `M-x zoom-from-in` and `M-x zoom-frm-out`. But lets be honest, that would be at least troublesome. We want to bind those two functions to key sequence. I’ve chosen C-= from zooming in and C– for zooming out, but you can use whatever key sequence that you want. That’s the whole beauty!
Then add the following two lines to `init.el`
(global-set-key (kbd "C-=") 'zoom-frm-in) (global-set-key (kbd "C--") 'zoom-frm-out)
After reloading configuration (`M-x load-file RET ~/emacs.d/init.el) we can now zoom-in & and zoom-out pretty easily.