Emacs Redux

Return to the Essence of Text Editing

A Peek at Emacs 24.4: New String Manipulation Functions

| Comments

Emacs has often been criticized for failing to provide a more extensive string manipulation API (compared to that of programming languages like Ruby and Perl, for instance). As many programs (extensions) running on top of it are doing quite a lot of string manipulation, having a good string API is important. To compensate the lack of certain primitives in Emacs itself a lot of package authors are using these days packages like s.el or simply adding the string functions they need directly to their packages (to reduce the number of third-party deps).

In Emacs 24.4 finally the situation is improving. Finally, we’re getting string-suffix-p, which was mysteriously missing even though string-prefix-p has been part of Emacs for years:

1
2
3
4
(string-suffix-p "test" "my_test")
; => t
(string-suffix-p "tester" "my_test")
; => nil

More importantly, Emacs 24.4 ships with a new built-in library called subr-x, which features a bunch of other string manipulation functions:

  • string-blank-p
  • string-empty-p
  • string-join
  • string-reverse
  • string-trim-left
  • string-trim-right
  • string-trim
  • string-remove-prefix
  • string-remove-suffix

Here’s a brief demo of them in action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;; all functions in the library are defined as inline, so you don't
;; need to require the library at runtime
(eval-when-compile (require 'subr-x))

(string-empty-p "")
; => t
(string-empty-p "  ")
; => nil
(string-blank-p "  ")
; => 0 (#o0, #x0, ?\C-@)
(string-reverse "Batman")
; => "namtaB"
(string-join '("one" "two" "three"))
; => "onetwothree"
(string-join '("one" "two" "three") ",")
; => "one,two,three"
(string-trim "   Peter Parker ")
; => "Peter Parker"
(string-remove-prefix "Mr. " "Mr. Smith")
; => "Smith"
(string-remove-suffix "Smith" "Mr. Smith")
; => "Mr. "

Sure, subr-x is not as extensive as s.el (and will never be), but I think that it’s a big step in the right direction. It’s likely that subr-x will be extended in subsequent Emacs versions and some of the functions from it will be promoted to built-in.

That’s all I have for now. Until next time!

A Peek at Emacs 24.4: Auto-indentation by Default

| Comments

I’ve written in the past about electric-indent-mode, which was added in Emacs 24.1. In Emacs 24.4 one of the most prominent user visible changes is that it’s enabled out-of-the box. That’s a huge step towards the “modernization” of Emacs and one of the bigger changes to the defaults in recent times. Let’s review briefly how the mode works with a couple of Ruby examples (| signifies the cursor position). Without electric-indent-mode:

1
def something|

After you press Return you’ll get:

1
2
def something
|

With it:

1
def something|

After you press Return you’ll get:

1
2
def something
  |

Nice, ah?

One problem with electric-indent-mode is that it doesn’t play nice with some (mostly third-party) modes (yaml-mode, slim-mode, etc). I guess the situation will improve over time, but for now you can simply disable the mode in such problematic cases:

1
(add-hook 'yaml-mode-hook (lambda () (electric-indent-local-mode -1)))

Note that electric-indent-local-mode was introduced in Emacs 24.4.

If you want to make a major mode electric-indent aware, have a look at the documentation of electric-indent-functions and electric-indent-chars.

P.S.

Dmitry Gutov recently wrote more on the topic in the context of ruby-mode in Emacs 24.4.

Ido-vertical-mode

| Comments

If you like ido, but don’t like the way it displays the available completion candidates you might want to have a look at ido-vertical-mode.

I won’t duplicate here the installation instructions, but I’ll show you how ido-vertical-mode displays the completion candidates:

Is this better than the regular ido? Depends on your personal preference I guess. But it’s good to have options, right?

That’s all for today, folks! Until next time.

A Peek at Emacs 24.4: sRGB Colours on OS X

| Comments

A while ago I wrote about a colour theme problem specific to OS X. Thankfully in Emacs 24.4 the OS X (a.k.a. NS) port of Emacs uses sRGB colours by default (at least on OS X Lion (10.7) and newer), so you’ll no longer need to install Emacs from homebrew (or apply an sRGB colour support patch manually). If for some reason you want to stick to the colours add this to your Emacs config:

1
(setq ns-use-srgb-colorspace nil)

I doubt that anyone would want to do that, but the option is there for those of you who want it.

A Peek at Emacs 24.4: Rectangular Selection

| Comments

I’m extremely fond of rectangle editing (the ability to select a rectangular region and apply editing commands only to it), but I’ve always disliked the fact there was no way to visually highlight the exact rectangle region you’ve currently selected. Let’s delete some text in a rectangular region (with C-x r d), so you can see where the problem lies:

It’s not immediately clear where the rectangle bounds are (it’s clear where the rectangle region starts, but it’s not so clear where it ends), which is a source of constant confusion for many people.

Emacs 24.4 finally fixed this with the introduction of rectangle-mark-mode (bound by default to C-x SPC). Let’s see it in action:

Perfect!

A Peek at Emacs 24.4: Smarter Show-paren-mode

| Comments

Some time ago I wrote about highlighting matching delimiters with show-smartparens-mode. In Emacs 24.4, the built-in show-paren-mode is capable of highlighting more complex paired delimiters as well (like do/end in Ruby for instance). There is one limitation to show-paren-mode (compared to show-smartparens-mode) - it requires that the major mode, that it’s used together with (e.g. ruby-mode), is implemented in terms of the new(ish) SMIE (Simple Minded Indentation Engine). SMIE has been around since 23.3, but hasn’t seen much adoption until quite recently. Prior to Emacs 24.4 very few built-in modes were using it (and just about no third-party major modes). In Emacs 24.4, however, a lot of modes were updated to use SMIE (like ruby-mode), so you’ll be able to enjoy the show-paren-mode improvement with them.

Here’s a glimpse of the enhanced show-paren-mode in action:

Long term, I still think that betting on smartparens-mode is a good idea, but if you prefer to stick with built-in modes - show-paren-mode is now more capable than ever.

A Peek at Emacs 24.4

| Comments

Emacs 24.4 is now feature frozen and its release is (probably) just a few months away at this point. This makes it a good time to start talking about some of the exciting and not so exciting changes that will be introduced in it.

Emacs 24.4 is a special for me, since it’s the first Emacs release to which I contributed code. On a related note - I’ve tracked its development much closer than I used to in previous years and I feel I have lots of interesting things to share with you.

In a series of smallish posts I’ll highlight some of the many new features that are coming in 24.4. Keep in mind that since we’re talking about unreleased code, there’s the chance of changes in the final version of 24.4.

By the way, if you’re looking for a brief overview of what to expect in Emacs 24.4 you might have a look at Mickey Petersen’s article on the subject.

The first article in the series will be arriving momentarily!

Articles in the Series

P.S. Emacs 24.4 is fairly stable at this point, so can start using it today. I’ve been using it exclusively for about half an year now and I’ve rarely come across any issues whatsoever. If you’re a Prelude user - it is 24.4 compatible.

A Crazy Productivity Boost: Remap Return to Control

| Comments

People have always complained about the awkward positioning of the two Control keys on modern keyboards. That’s a fact! Effective Emacs usage is heavily dependent on the Control keys (yep, both of them). That’s also a fact!

A great many Emacs users remap the infrequently used CapsLock key to Control to alleviate partially the problem with the accessibility of the control keys. That, while useful, is not sufficient for the optimal typing experience, since that way you’re breaking the key symmetry on both sides of your keyboard. Also - your right pinky has to go much further than your left one, while you’re typing. Many people seem to be using only the left Control and I guess they’re not particularly bothered by this, but touch typists like me are generally quite bothered by such things.

A much better idea would be to leverage a little knows capability of keyboard input systems and map the Return key to Control only when it’s held down (it will behave like a normal Return key in all other situations). This sounds a bit crazy, but please bear with me for a while.

This radical approach has several advantages. First and foremost - it’s much easier to hit Return with your right pinky than it is to hit the regular right Control (especially if you’re using a US layout keyboard - these have long single row Return keys, compared to the short 2 row Returns found on European keyboards). Second, if you’ve already remapped CapsLock to Control (like you should have done) you’re getting a pretty symmetrical mapping on the opposite side of your keyboard. Last, but not least - it’s a great option for people using compact keyboards with no left Control key.

Obviously you’ll need some keyboard remapping software to make this trick work. OSX users can use KeyRemap4MacBook to do that. In its settings look up the Change Return section and under it Return to Control_R (+ When you type Return only, send Return). As far as I know this remapping can be done on GNU/Linux systems with xcape (though I haven’t checked that myself), but I have no idea if it’s a viable option for Windows users.

All in all - remapping Return to Control should be a big productivity win for some (the touch typists) of you and should make your right pinky’s life easier.

Highlight Matching Delimiters With Smartparens

| Comments

Some time ago I wrote about highlighting matching parentheses with show-paren-mode. This is definitely useful, but it’s kind of restrictive, since parentheses are just a specific kind of paired delimiter. Single and double quotes are also paired delimiters (at least in most programmming languages). In the Ruby programmming language, for instance, do and end also constitute paired delimiter.

smartparens offers extremely customizable handling of paired delimiters and comes with an extra minor mode called show-smartparens-mode to highlight them. This mode totally replaces show-paren-mode and language specific modes like hirb (which highlights Ruby blocks).

Assuming you’re already using smartparens, enabling show-smartparens-mode is trivial:

1
(show-smartparens-global-mode +1)

Here’s a glimpse of it in action:

In Prelude show-paren-mode was replaced by show-smartparens-mode some time ago.

Instant Access to Your Shell Init File

| Comments

A while ago I showed you a way to instantly edit your Emacs init file. Today we’ll adapt the original idea for shell init files like .bashrc and .zshrc. The code required is fairly short and simple:

1
2
3
4
5
6
7
8
9
(defun find-shell-init-file ()
  "Edit the shell init file in another window."
  (interactive)
  (let* ((shell (car (reverse (split-string (getenv "SHELL") "/"))))
         (shell-init-file (cond
                           ((string-equal "zsh" shell) ".zshrc")
                           ((string-equal "bash" shell) ".bashrc")
                           (t (error "Unknown shell")))))
    (find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))

The shell init file is deduced from your SHELL env variable. While there are different shell init files for most shell (e.g. .bash_profile, .zshenv, .zprofile), here we’re assuming you’re using the most commonly used files. find-file-other-window will open the file in a window adjacent to the one you’re currently in.

1
(global-set-key (kbd "C-c S") 'find-shell-init-file)

find-shell-init-file is available in Prelude(but with a prelude- prefix).