Getting Virtual with Emacs (Part 3)

Last time, we talked about the two emacs extensions I use to emulate my most used features of Sublime Text. Even a cursory look at the manual would show way more features than I even thought about using, but by learning basic editing in emacs I’m already able to fully switch over to the Gnu editor.

This time I’m going to write about the cool stuff I’ve installed in emacs that you don’t see in the standard Sublime installation. Maybe Sublime has support for some of these functions, but not out of the box.

Magit

Magit allows you to control git from inside emacs. ‘But Matt,’ I hear you cry, ‘I can just leave emacs if I want to do git stuff! Why do I need this package?’ If I had to give you a a good reasons.. The whole thing is a pleasant user experience. Magit itself manifests as a new buffer splitting your window, listing the current status of the repository. Asking for a diff gives you the diff in another buffer; committing causes the commit message to open in a new buffer.. It’s very self-contained inside emacs. You don’t have to leave emacs for any part of the staging/committing/pushing, so you never have to break your flow. Even if you want to run an arbitrary git subcommand, Magit has you covered: The ! command while in the Magit buffer lets you specify subcommand you want.

But why alt-tab out to your terminal, then type ‘git commit -a’ only to be thrown back in to emacs for the commit message? Then you have to go back to the terminal to type ‘git push’.. What a chore. With Magit, I can just hit my Magit key binding (C-x C-a), then ‘c’ for commit, ‘-a’ for ‘add all unstaged files’, ‘c’ again, and I’m in a buffer where I can write my commit message. Write the message followed by C-c C-c to complete the commit. Then I just have to type ‘P’ twice to push, enter my SSH key-phrase, and that’s it.

Sure, Magit is definitely more of a convenience package than something that will supplement your coding skills, but it’s such a great convenience I can’t see why you wouldn’t want to use it!

Yasnippet

This one is a little unfair, because Sublime has support for snippets, and I just never really used them. Anyway: Yasnippet by João Távora allows you to use snippets when coding. Snippets are blocks of (usually) often used code that you can assign a shorthand command for. These commands take the form of a sequence of characters typed in to the editor followed by a tab. For instance, when writing Python I could type ‘cls’ followed by a tab and cls would be replaced by:

Yasnippet1

Emacs highlights ‘class’, ready for me to type the new class’ name. Once typed, I press tab again and I’m done. Snippets can contain more than one field to modify. In that case, tab moves you to the next field.

That happens to be one of the snippets you get when you first grab yasnippets via the package manager. It’s very simple to write your own, and dare I say, easier than writing the equivalent template in Sublime. The snippet above is defined like this:

Yasnippet2

If we wanted to save ourselves even more writing and not have to worry about writing the usual init function, we could modify it like so:

Yasnippet3

Notice how I’ve referenced $1 in two places and added a second field for us to potentially modify for when the new class is a subclass. By referencing a field more than once, the field is mirrored in other places. Expanding the snippet now gives this:

Yasnippet4

Notice how I’ve typed ‘Woah’ and it has automagically appeared in other places! Pretty cool. There’s a lot of potential in snippets to unlock, and they can save you from large amounts of typing. They’re a great feature to have, and I can’t wait to think up some useful snippets to improve my workflow.

Ace-jump-mode

One of the simplest packages on this list, ace-jump-mode by winterTTr is one of the most difficult to explain. Functionally, it’s a method of moving the cursor to the beginning of any word on the screen. The way you use it might seem weird at first, but it quickly becomes second nature. In this screenshot, my point is at the bottom of the screen but I want it to jump up to the creation of the Player object, because I just remembered I renamed that class.

AceJump1

Rather than move the point all that way (a whole 12 lines, so much effort), I can activate ace-jump-mode with my key-binding (Ctrl+C SPC), type the first letter of the word I want to jump to, and bam:

AceJump2

Ace-jump-mode now finds all instances of the chosen letter on the screen when it appears at the start of a word, and labels them with a letter of the alphabet. In this case, I want to jump to Player, so all I have to do is type ‘i’ and the point will jump to that location! I think it’s pretty neat in its simplicity, and saves some tedious point moving action you’d otherwise be doing.

That’s pretty much it! Before I finish this post, I’d like to take a little while to help out anyone having problems combining some of the packages discussed in these posts.

Jedi.el and multiple-cursors.el

These two packages get along quite well until you have multiple cursors active and decide to type a dot. Upon seeing a dot, Jedi.el decides to attempt to call auto-completion to help out with class methods and the like. However, multiple-cursors explicitly disables auto-complete mode because auto-complete doesn’t play well with it. The end result is that an error is thrown on typing dot, so only the first cursor actually types it. This is problematic, but I have a solution! In fact, I’ve submitted a pull request to the multiple-cursors.el repository that was recently merged. But for you people who don’t want to upgrade multiple-cursors to the latest pull or want to wait for a new release, here’s another solution that you can add to your .emacs after your normal multiple-cursors set-up:

(setq mc/unsupported-minor-modes '(auto-complete-mode flyspell-mode jedi-mode))

After adding this, you can type dots to your hearts content, even when using jedi.el! Enjoy.

Yasnippet and auto-complete.el

While not mentioned explicitly here or in the previous post, Jedi.el uses auto-complete.el for its auto-complete interface. Sometimes you’ll want snippets to appear in the auto-complete list. By default, auto-complete.el is coded to not include a yasnippet source. A source is just an abstraction that provides suggestions to auto-complete.el, with the actual method of finding said suggestions hidden inside the source itself. With a bit of tweaking, we can have snippets appear in all auto-complete suggestions regardless of mode by forcing the yasnippet source to be loaded in all modes. This is typically desirable behaviour, because if we have snippets for a mode, we usually want to use them! To do this, you need to add a hook to the auto-complete-mode in .emacs:

(defun ac-always-add-yasnippet-source ()
(add-to-list 'ac-sources 'ac-source-yasnippet))

(add-hook 'auto-complete-mode-hook 'ac-always-add-yasnippet-source)

Alternatively, you can add hooks on a per Major mode basis instead. For instance, you can add the same function as a hook to Python mode by replacing auto-complete-mode-hook by python-mode-hook.

That about wraps it up for this post. If I find a really cool emacs package that I have to shout about, I’ll be sure to mention it. Otherwise, next time we’ll be looking at Rogue Detective (working title), my really-kinda-lame-but-I-like-working-on-it Python roguelike developed in emacs!

Getting Virtual with Emacs (Part 2)

It’s been just over a month since I started properly developing in emacs, both at work and at home. I think it’s about time I wrote a little about my experiences.

First of all, while I’ve used vim daily at work, I don’t claim to be a vim power-user by any stretch of the imagination. Sure, I know how to navigate around quickly, and how to perform some basic editing, but I’m nowhere near the proficiency required to consider oneself a true user. For instance, I’m pretty terrible at VimGolf! When doing some heavy duty coding, I worked in Sublime Text 2. Even in Sublime, for me it was more about the syntax highlighting and pretty interface than it was about its cool features (with the exception of multiple selections). Like many beginner programmers, I haven’t yet took the time to learn my environment. Or any environment, for that matter! After watching some cool emacs videos, I decided I wanted to properly ‘learn’ emacs, and not just use it in the same way I use vim (in that case, why even try emacs?).

The question was: Could using emacs help my productivity? Sure, it wouldn’t help me think up or reason about code faster, but when an idea finally does pop in to my head, the last thing I want is for my editor to get in the way. In fact, an editor that could help me express my ideas faster than other editors would be a boon.

My first experience with emacs was with the in-editor tutorial. It teaches you how to move the point (cursor) and how to perform simple editing. The tutorial barely scratches the surface of emacs, but it does ease beginners into editing with emacs in a gentle, easy-to-follow fashion. After blowing through that and wondering what all the fuss was about, I would find the real power of emacs comes from its extensibility.

Emacs extensions

While emacs is a powerful editor in its own right, the huge amount of community support means that regardless of what language you’re using, someone has written an extension to make coding in it easier. If they haven’t (they have), rolling your own is just a case of applying your Lisp knowledge to the emacs equivalent, elisp. Don’t know Lisp? After spending enough time with emacs, you’ll at least have a good enough grounding in elisp to understand the odd bit of proper Lisp code, and enough knowledge of elisp to be writing your own little bits of code to enhance emacs. I realised that all the cool things in those videos I was watching were often showcasing packages (extensions) for emacs.

My first big programming project using emacs is a Python roguelike that I’ll talk about in future posts. Having written my previous Python project in Sublime Text, my first aim was to be able to do in emacs what I could do in Sublime. This turned out be simple, given how I’d barely given Sublime a proper workout in the first place. I wanted half-way clever auto-complete functionality, multiple selections, and a colour scheme that wouldn’t melt my eyes. After getting the basics out of the way, it would be time to explore the larger world of emacs.

As of emacs 24, emacs comes bundled with a package manager. It turns out there’s more than a couple of repositories for emacs packages, and it’s simple to add them to your emacs package list:

Packages

Once that’s sorted, you can bring up the package manager by using the ‘list-packages’ M-x command and browse the huge variety of packages available at your leisure:

Yes, 2048 has even invaded emacs

Yes, 2048 has even invaded emacs

Jedi.el – Python Auto-complete

In my Python setup, the Jedi.el package provides auto-completion. It’s a really clever piece of kit. Jedi.el will not only offer auto-complete suggestions based off the history of the file (giving auto-complete options for words already typed), but thanks to integration with a Python server called Jedi, Jedi.el will also have knowledge of your classes and function definitions. For instance, if you’re writing a method inside a class and type ‘self.':

Jedi.el Autocomplete

Jedi.el and Jedi will be able to figure out your class’s variables and functions and offer auto-complete suggestions. This is seriously useful. If you’re writing code and forget what arguments to pass to a function, you’re also in luck:

Jedi.el Function Signature

So yeah! Overall, Jedi.el is a really neat package. It has a few other features I haven’t tried out too much yet (‘goto definition’ for instance), but I’m looking forward to exploring this package a bit more. Installation was a bit more involved that most emacs packages, requiring some pip action to get the external Jedi server installed. But in fact, Jedi.el came out with a new version that makes installation simpler than before, so even that’s not really a valid criticism. Here’s how Jedi.el is set-up in my .emacs. In the latest version, the setup-keys variable isn’t required.

JediSetup

multiple-cursors.el – Multiple Selections

multiple-cursors.el is written by the wonderful Magnar Sveen of Emacs Rocks!. He’s produced a video about it that can explain it far better than I could hope to:

Installation is as easy as downloading the package via the package manager and inserting a few key-bindings in to your .emacs:

Multiple Cursors Setup

Sublime Text and Vim look-and-feel

I really like how Sublime Text’s default theme looks. I also love the simplicity of the Vim interface. Emacs, out-of-the-box, looks positively crowded compared to the very clean, empty Vim interface, as demonstrated by the last article:

EmacsTerminalFresh2To be fair, that picture is also from the terminal version of emacs, but the point stands. What on earth is that menu bar doing up there? And in the X terminal version, there’s a scroll bar and a tool bar? Vim doesn’t need these things, and neither do I! They simply had to go:

GettingRidOfTheChaffAnd what of my wonderful Sublime Text colours? It turns out, someone had already made an emacs theme with pretty much the exact same colours as in Sublime Text’s monokai theme! They imaginatively called it monokai. I installed it via the package manager, added

(load-theme 'monokai t)

to my .emacs, and whoosh!

MonokaiThat is one sexy looking emacs set-up. Yes, there’s a couple of mistakes in my .emacs, but I’ll survive. Another little thing enabled here is column numbering in the status bar.

Those of you with eagle eyes will spot little things in the .emacs like yasnippet, magit and ace-jump-mode. Those are some very, very cool add-ons. I’d like to write a bit about those next time. For those who are really impatient, both yasnippet and ace-jump-mode have been subjects of Emacs Rocks! videos.

Getting Virtual with Emacs (Part 1)

A Quick Introduction

We’re taking a short (okay, long) break from the Hullet Bells posts. It’s already been a couple of months since I last updated, mostly because progress has been slow. I keep having ideas on how to refactor the fledging script system, but never get around to actually implementing them. So that’s on hold for now. In the meantime, I’ve been messing about with a couple of other things. Today, I’d like to write about my experiences with Oracle VirtualBox and Arch Linux. This isn’t going to be a tutorial on installing Arch on VirtualBox by any means. If you want that, you’d do well to check out Josh Braun’s excellent guide. When the ArchLinux beginner’s guide was not enough, Josh’s advice helped grease the wheels.

Oracle VirtualBox (or simply VirtualBox) is a virtual machine (or VM). For those unfamiliar with VMs, the concept is simple: Rather than install a second operating system via the traditional fashion of re-partitioning your hard-drive and modifying the boot loader so you can dual-boot, you run your standard operating system and boot the new one as a program inside it. The underlying operating system is called the ‘host’, and second operating system is known as the ‘guest’.

I’m running Windows (the host OS) with Sublime Text and VirtualBox open. Inside VirtualBox is an Arch Linux instance (the guest OS), running Openbox, Emacs, and Supertux. Why would anyone do this? Well, there’s plenty of legitimate reasons. For instance, as a developer you might want to test software on multiple platforms without having to drudge through rebooting or using a different machine every time you want to try something new. Or maybe you have some old applications that only run in an older version of Windows, and you’d like to use those without dusting off your ancient Dell. These are fine reasons, and there are many more. My reason was a little stranger..

I wanted to learn Emacs

Why learn GNU Emacs? Well, why not? I’m already pretty familiar with Vim, although I’m by no means a wizard. After watching a few videos of people working with Emacs, I figured maybe it was time to give it a go. I would be one of the brave, the few, to see what life was like on both sides of the editor war.

‘But Matthew,’ I hear you cry. ‘You don’t need to install Linux just to use Emacs!’ And you would be completely right. While I was toying with the idea of installing Emacs for Windows, I remembered another thing I wanted to get to grips with: Linux. I’ve had several abortive attempts over the years, usually lasting anywhere between a week to a couple of months. The high entry cost of having to leave behind Windows and its myriad useful applications ended up being too much to bear. But the future is here, and now I can run Linux in my Windows, so I can Linux while I Windows. So I decided to give both of them a go. But first I’d need a Virtual Machine to run Linux on..

Virtually a Machine

After Googling for all of thirty seconds, I came the open-source VM VirtualBox. There is an alternative in VMware Player. I picked VirtualBox simply because I had to pick between the two. It turned out to be a great choice, with almost no problems encountered at all.

VirtualBox

What’s that in the preview box?

The interface couldn’t be simpler. You can run as many virtual machines as your system can handle, and creating a new machine is as easy as clicking the New button and following the wizard. All you need to set up at first is the machine name, type (Linux, Windows, other), and version (Ubuntu, XP, OS/2, etc). Allocate it some amount of memory, specify some parameters about its virtual hard drive and bam! You’re done! Grab an ISO of your favourite Linux distribution, pop it in the virtual drive, boot up the machine..

ArchInstall

And install it just like you normally would, with a few special additions/omissions for doing it in VirtualBox. Again, I’m not about to provide a big ol’ tutorial. That’s already been done, and it’s been done very well. Some of the nice features you get when working with a supported VirtualBox OS:

  • Shared clipboard!
  • Ability to seamlessly move the mouse and keyboard between operating systems without having to explicitly regain control from the guest!
  • Other stuff I haven’t yet discovered!

It’s super simple and a pleasure to work with, at least for my current requirements.

Arch Linux

Why Arch? Well, I’m a big fan of lightweight stuff. Arch comes with just enough to get you going, and a great package manager (pacman) to help you get the stuff that you want. If you’re bit of a Linux penguin, you may understand my surprise at the fact I had to download sudo! That’s right, that’s how lightweight it is. Installation and initial set-up took me about half a day in total. This is keeping in mind I’m not very familiar with Linux installation, especially after such a long period between efforts.

One of the things I really like about running a virtual machine is that you don’t really have to worry about irrevocably screwing up your physical box. Things like disk partitioning are usually terrifying exercises in double, triple-checking your numbers, especially on a system that already has an OS installed. So without those worries, I was able to play about a bit and step out of my normal comfort zone. After that, it’s just going through the motions of installing the guest OS, enabling the network, setting the clock..

Then it was time to install Emacs. I took a deep breath and typed

sudo pacman -Sy emacs

and it was done!

Emacs on the Terminal

I ran through the Emacs tutorial in about an hour. I was amazed at how different the whole experience was from Vim. The look and feel were completely different, for one thing. A menu bar? Wow, I hadn’t seen one of those on a terminal editor since Edit! And the editing wasn’t modal like Vim: None of this switching between Normal and Edit mode. Of course, there were also little surprises. Yank means ‘paste’ in Emacs, while it meant ‘copy’ in Vim. There’s zero one-key short-cuts for things: Want to go to the start of the line you’re on? Ctrl+A will do it in Emacs, while ‘0’ will do it in Vim. Want to copy that line? ‘yy’ will do that for you in Vim. But you’ll need to either ‘mark’ (select) the line in Emacs and ‘kill’ (copy) it without deletion, or delete the whole line and ‘yank’ it back in, with the end result of the line being on Emac’s equivalent of the clipboard. The key sequences for these look like:

#1: Ctrl+a (go to start of line), Ctrl+Space (Place marker), Ctrl+e (Go to end of line), Meta+w (‘Kill’ without deletion)
#2: Ctrl+Shift+Backspace (‘Kill’ the line and delete it), Ctrl+y (‘Yank’ the line back)

Woah. All the simple things suddenly seemed very difficult. In reality, you get used to the extra keystrokes quite quickly. And if it really bothers you, there’s Emacs extensions and modes out there for making Emacs behave more like Vim! Which brings up a good point: Emacs is extensible. Like, crazy extensible. Thanks to its built-in language Elisp (Emacs Lisp, a Lisp dialect), it wouldn’t be hard to implement a single key-chord command that would perform one of the above methods. Indeed, the same thing could be accomplished with a macro (a recorded set of key presses), a bit of functionality common to both Vim and Emacs. In any case, moving on..

Here’s how Emacs looks on the terminal:

EmacsTerminalFresh2

Not super hot. I spent a lot of time trying to get custom themes working and looking good, but it’s not an easy task. Especially when Emacs only wants to work with 256 colours! After a lot of wasted time trying to get a full colour terminal (if you figure this out, let me know), I decided to just install Xorg.

Emacs in Openbox

Initially, I went with the safe choice: The Gnome Desktop Environment. I’d used it in the past, and it was pretty okay back then. But wow, does it take a long time to start-up? And you have to log-in, what a mess. I ditched that quickly and continued my adventure outside my comfort zone. I installed Openbox, a lightweight window manager. When I first booted it up, I didn’t realise it was loaded:

OpenBox

It’s very minimalist.

That’s it. That’s what Openbox looks like. A right click reveals a menu in which you can open up X programs normally. I’ll talk about Openbox a bit later, but first, here’s what Emacs looks like under X:

EmacsOpenbox2

Ah, much better

Quite different, right? If the colour scheme looks familiar, it’s because it was inspired by Sublime Text‘s monokai theme. Installing the theme was easy. I had to configure the in-built (as of March 2013) Emacs package manager to use the Melpa and Marmalade package repositories, and then just downloaded the theme via that! A quick config change in my .emacs file, and there we go. The comfortable look of Sublime Text, the power of Emacs.

Next time, I’ll be posting about my experiences with emacs so far. I’ll highlight a couple of interesting features and extensions that I like, and maybe even convince some of you to give emacs a shot!