Emacs Redux

Return to the Essence of Text Editing

Pimp My Minibuffer Evaluation

| Comments

In Emacs you can evaluate Emacs Lisp pretty much anywhere - even in the minibuffer. Writing Emacs Lisp in the minibuffer, however, is not exactly fantastic experience out-of-the-box - there’s TAB completion, but what about eldoc and paredit for instance?

If only there was a way to enable them… I suggested one trick in my original post on eval-expression, but Emacs 24.4 made things even easier by adding eval-expression-minibuffer-setup-hook. To enable eldoc for minibuffer evaluations use this snippet:

(add-hook 'eval-expression-minibuffer-setup-hook #'eldoc-mode)

For paredit you can use this one:

(add-hook 'eval-expression-minibuffer-setup-hook #'paredit-mode)

Obviously you can do the same for any other minor mode you might need.

The best thing about this setup is that it will work with tools like CIDER and SLIME as well (they have similar commands which allow you to evaluate Clojure & Common Lisp code and those command trigger eval-expression-minibuffer-setup-hook).

Look Up the Keybindings for Some Command

| Comments

If you know the name of some command, but you’ve forgotten its keybinding(s) there are 3 common options to look the keybinding(s) up.

  • C-h f command-name

This will display some information about the command in question in a help buffer. Important bits of this information include where is the command defined, what are its keybindingings if any, and its documentation.

  • C-h w command-name

This will display the keybindings of the command in the minibuffer. If you’re interested only in the keybindings you should prefer this option over C-h f.

  • M-x command-name

After you invoke some command using M-x you’ll see a suggestion to use its keybinding instead in the minibuffer.

That’s all for now, folks!

Disable Annoying Audio Notifications

| Comments

By default Emacs has some pretty annoying audio notifications for certain events (e.g. trying to move past the end of a buffer). You’ve got two options to get rid of them. Option 1 is to replace them with visual notifications (the Emacs frame will flash):

(setq visible-bell t)

This doesn’t work well on OS X and is just as annoying (if not even more), so I’d suggest going with option 2 instead - disable those notifications completely:

(setq ring-bell-function 'ignore)

At last - some peace and quiet!

Auto-indent Your Code With Aggressive-indent-mode

| Comments

One of the things I hate the most while programming, is having to manually adjust the indentation of some code, after I’ve moved or renamed something in it. While it’s pretty easy to do such re-indent operations using commands like crux-indent-defun or advices like crux-with-region-or-buffer (you remember, crux, right?), there’s an even more efficient way to tackle the issue at hand. Enter aggressive-indent-mode.

aggressive-indent-mode’s name is a bit of a misnomer - it should probably have been named auto-indent-mode, as this is what it does. When you edit your code it will adjust the indentation automatically. It’s easier to show this than to explain it.

Here’s one example showing agressive-indent-mode enabled in emacs-lisp-mode:

And another example using cc-mode:

Provided you’ve installed the mode, enabling it for particular major modes is a piece of cake:

(add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
(add-hook 'clojure-mode-hook #'aggressive-indent-mode)
(add-hook 'ruby-mode-hook #'aggressive-indent-mode)

If you want to enable it in all major modes you can do this as well:

(global-aggressive-indent-mode 1)

Note that this is not going to work well with modes like python-mode and haml-mode where the proper indentation can’t be reliably determined. When global-aggressive-indent-mode is enabled it will not affect major modes listed in aggressive-indent-excluded-modes.

For more info - head over to the project’s readme.

Display the Keybinding for a Command With Substitute-command-keys

| Comments

If you ever need to show the keybinding for a particular command to the users of your package (e.g. you’re adding some tips functionality), you should avoid resisting the urge to write something like this:

(message "Press <C-c p p> to switch between projects.")

Why is this a bad idea? Because you might change the keybinding of the command in question (e.g. projectile-switch-project, but you might forget to update messages like this. Is there a better way? substitute-command-keys to the rescue:

(message (substitute-command-keys "Press <\\[projectile-switch-project]> to switch between projects"))

This will produce exactly the same message as before and you’re guaranteed the keybinding will always be in sync with the command.


P.S. If you want to check interactively the keybinding of some command use C-h f (describe-function). Here’s an example - C-h f RET projectile-switch-project RET will produce this:

projectile-switch-project is an interactive compiled Lisp function.

It is bound to C-c p p, s-p p, <menu-bar> <tools> <Projectile> <Switch
to project>.

(projectile-switch-project &optional ARG1)


You can also check which command is bound to some key with C-h k.

Use Tab to Indent or Complete

| Comments

By default in Emacs the Tab key does only indentation. If some major mode provides completion of some form, you normally have to trigger it with M-Tab. In most window managers, however, this keybinding is used to switch between open windows, which makes it a bit hard to use out of the box.

There’s a simple trick to augment the default Tab behavior. Just put this in your Emacs config:

(setq tab-always-indent complete)

Now, when you press Tab one time it will indent and if you press it again you’ll get completion candidates. If the indentation at point is already correct you’ll get the completion candidates right away. As an added bonus - you don’t really need M-Tab anymore.

Simple and neat! One really has to wonder why this isn’t the default behavior.


| Comments

For the longest time Prelude included the function prelude-goto-symbol (bound to C-c i). It basically allowed you to jump to any definition in the current source file using imenu behind the curtains.

Recently I’ve found an even better option - the package imenu-anywhere. It works in a pretty similar manner but gives you the ability to jump to any definition in any currently open buffer. That’s quite handy and it greatly reduces the need to use something like etags.

As an added bonus - imenu-anywhere features helm integration.

This is a very handy package and I encourage you to give it a go!

P.S. Prelude users should simply upgrade to the latest version of Prelude (it already uses it).

My Personal Emacs Configuration

| Comments

From time to time people ask me about my personal Emacs configuration. Other just assume that I use Prelude. For a very long time my personal configuration was pretty similar to Prelude - in a way it was a staging ground for things to go into Prelude eventually (although changes would travel both ways when Prelude users suggest some cool things).

Recently I’ve decided that in the future I want to do a few things with Prelude:

  • extract as much functionality from it as possible into reusable packages (e.g. super-save and crux)
  • adopt there use-package
  • improve the support for Windows (because now I have Windows computer)

As part of these efforts I reworked my personal config into something pretty simple (it’s a single init.el file) and I’ve started experimenting with ideas for the future. Stay tuned for the results!

The config is available here. Perhaps some of you will find something useful there.

Remap Return to Control in GNU/Linux

| Comments

A long time ago I wrote about remapping Return to Control in OS X. This was the best productivity boost for my Emacs experience ever!

Recently I’ve bought a Windows ultrabook (wanted something as light as MacBook Air, but more powerful and versatile) and I’m doing most of my work there in a Xubuntu VM. The first thing I did while setting up Xubuntu was to figure out how to do the aforementioned remapping.

In my original post some people suggested the tool xcape, so I took a look at it. The tool can certainly use some documentation improvements (and pre-built packages), but it gets the job done. After you’ve installed it you just need to add the following to your login shell’s init file (e.g. .bash_profile) and you’re in business:

xmodmap -e "remove Control = Control_R"
xmodmap -e "keycode 0x69 = Return"
xmodmap -e "keycode 0x24 = Control_R"
xmodmap -e "add Control = Control_R"

xcape -t 10000 -e "Control_R=Return"

Obviously the first time around you should source .bash_profile after updating it:

$ . .bash_profile

This is definitely a lot more work than just clicking in the GUI of the wonderful Karabiner, but it yields the desired results and that’s what’s important at the end of the day.

Now if only there was a way to achieve the same result in Windows…

P.S. vim users will love xcape. Its default behaviour is to generate the Escape key when Left Control is pressed and released on its own.


| Comments

I’d often blog about some useful utility functions here. Pretty much all of them get included in Emacs Prelude, but I’ve decided I can do one better and extract those of them which are most useful/universal into a separate package.

This package is crux - a Collection of Ridiculously Useful eXtensions for Emacs.

You can install it from MELPA and MELPA Stable. Once this is done you just have to pick keybindings for the commands shipped with crux. I’ve suggested some keybindings here. And here’s a small configuration snippet showing how to actually bind keys to some of crux’s commands:

(global-set-key [remap move-beginning-of-line] #'crux-move-beginning-of-line)
(global-set-key (kbd "C-c o") #'crux-open-with)
(global-set-key [(shift return)] #'crux-smart-open-line)
(global-set-key (kbd "s-r") #'crux-recentf-ido-find-file)
(global-set-key (kbd "C-<backspace>" #'crux-kill-line-backwards))
(global-set-key [remap kill-whole-line] #'crux-kill-whole-line)

crux also ships with some handy advises that can enhance the operation of existing commands.

For instance - you can use crux-with-region-or-buffer to make a command acting normally on a region to operate on the entire buffer in the absense of a region. Here are a few examples you can stuff in your config:

(crux-with-region-or-buffer indent-region)
(crux-with-region-or-buffer untabify)

So, this is crux for you - simple and neat! I’d love it if you contributed more useful commands to it, so we can make it even more versatile!