This is essentially clw from a previous post, but here we’re defining a new unit of editing. This lets you:

(define-key evil-operator-state-map (kbd "lw") 'evil-little-word)

And then you’ve got clw and dlw which delete the “one” in “oneTwoThree” or in “one_two_three”. I use this all the time.

Here’s something even more useful: Ever boil over because in emacs (and vim for that matter) every text deletion pulls that text into your kill-ring (register)? I sure do. Sometimes, I just want something gone, and I never want to see it again!

More often, I want to paste something from my clipboard over something else, which requires that my clipboard not be messed with by the pasting process.

(Emacs translation: More often, I want to yank something from my kill-ring…)
(Vim translation: More often, I want to put something from my default register…)

There is some unfortunate copy and paste code going on here, and I’d be happy to hear about how to eliminate it, but in essence this is just the d operator (delete), but without any clipboard operations going on. (Note the updated, cleaner implementation, inspired by Russell’s comment.)

You can then bind this to something, I use X, and utterly and irrevocably DESTROY some text. But that’s not end game. Next step is this:

This will replace a bit of text with whatever’s in the clipboard without altering the clipboard, and I use this all the time.

I bind it to q, which, yeah, is supposed to be for evil macros, but I’ve got emacs macro bindings, so that’s fine.

This works with motion and text objects, so you can do things like yi" to copy between double-quotes and then qi" to paste inside different double-quotes (assuming you’ve bound to q). qi(, qi' and qi{ all also work as you’d expect.

What does this do? Only the best thing ever! With regular, pull-into-the-clipboard deletion, things work as usual. However, should you delete a blank line (or any whitespace-only region), the clipboard is left alone.

Ooooooh lordy, that’s nice. No more pushing blank line deletions into your clipboard! Who wanted that anyway?

(If anyone knows how to build a similar solution for vim, post to the comments.)

Prelude

Every few months or so, I get an episode of .emacs.d OCD, where my setup suddenly seems hopelessly disorganized and non-optimal, and I can’t do another thing until I fix it. I went through such an episode recently, causing me to spend hours looking throughvarioussetupstrategies, but ultimately not finding that je ne sais quoi I was looking for.

The whole exercise made me step back and reflect on what it is that causes me the most trouble when trying to organize an emacs config, and after a moment of reflection, I came up with 1) Where should I put things? 2) How should I init things?

It’s Load Time!

Where to put things is a problem that spans well beyond emacs configuration, and may well be the third-hardest topic in computer science, so I’ll get back to it. Number 2, however, is a little more particular to emacs. The issue is this: If you’re like me, you’re using dozensofemacsplugins to optimize your environment, and layering a handful of personal customizations and settings on each. That’s great. That’s what emacs is all about. But then, there’s autoloading.

We emacsians spend a good bit of time biting our nails over our favorite editor’s startup time, and make tremendous efforts to do as little as possible at startup time, instead letting packages load as needed.

Problem is, some package configurations require packages to be loaded while others don’t. Some packages don’t do autoload right. Sometimes you configure an autoloaded package in a running emacs session and it breaks emacs the next time you start it. And sometimes autoloading itself is unwanted sitting and staring and waiting time. (The package initialization section at http://emacswiki.org/emacs/ELPA is illustrative.) Most of these issues can be overcome, but the whole situation is messier than I’d like.

What’s more, there’s a very simple solution, and since I’ve discovered it, everything has just gotten a lot simpler. It’s not a package or a plugin or a software solution at all. It’s an attitude shift, and it struck me like a lightening bolt.

I DON’T CARE HOW LONG IT TAKE EMACS TO START UP.

What’s that?

I DON’T CARE HOW LONG IT TAKE EMACS TO START UP.

Yeah, ok, vim starts up like that! But here’s the thing. I don’t use emacs like I use vim (which I do sometimes). I start emacs up on my local machine maybe a couple times a week, and leave the session running for days. So even if it took 30 seconds to load (it takes 3.5), that’d be, like, a minute a week. I’ve already made that time up.

This is a pretty mundane realization, perhaps, but I no longer have to care about where and when things get loaded, about configuring things in the right place and time, about autoloads and eval-on-loads and related concerns. I just throw in a require and I get on with my life.

Getting On

So already in an iconoclastic state of mind, I was well positioned to tackle issue 2: Where to put things?

See, some things were in bindings.el, others in defuns.el, settings.el, [package-name-here]-config.el, and on and on and on. Where do I put hooks that depend on custom defuns? Bindings for plugins? Third-party package configs vs. built-in package configs? What’s worse, directories were nested three, four levels deep, and if I had to find something, I’d be acking all up in my .emacs.d and it was a mess. And I bet you do the same thing.

This was causing me pain, and then I remembered an incident.

The Incident

I was working on an iOS project, and it had a ton of files, and I was working in Xcode, and I hated Xcode, but needed the smart completion, which is a weak area for emacs. But I downloaded Code Pilot, and that made things a little better. And I used CTRL-6 to navigate to methods in a class (think imenu), and used project search a lot, and I started not caring about where things were, because as long as I new part of the name, I was there.

It took some work to get there in Xcode, but this is the sort of setup I strive for, whatever I’m developering, this is the reason I emacs, and this is key to productivity and happiness for my money.

My colleagues in the meantime where busy nesting directories and arranging things just so and painstakingly putting files in alphabetical order because seemingly Xcode won’t do that for you. And not only did they spend a lot of time and energy worrying about where to put things, they had to think a lot when they wanted to find them again.

For me, one of the biggest enemies of flow, and more generally, productivity, is the need to think about a thing that is not the thing you’re thinking about to solve the problem at hand. So if a particular task calls for jumping around to several files, any time and effort spent thinking about where some file is, or even just going through the mechanics of navigating directories, is likely to dissipate my fragile flow, kill my productivity, raise my hackles, and generally make me grumpier.

In It

So that’s the incident I remembered, and I realized that if you have good navigation support, it doesn’t matter where you put things. And if it doesn’t matter where you put things, why not take the simplest, stupidist, most obvious approach? The approach you probably took the first time you wrote a bit of emacs configuration code.

So where to put things? How about init.el? What things?

ALL THE THINGS!

I was initially incredulous of my own bat-shit idea, but I went through with it anyway. And it has been liberating.

You see, when everything you care about is in one place, you no longer have to think about where you put stuff. Where are my bindings? init.el. Hooks? init.el. Configuration for package X, Y, and most especially Z? init.el. Then fold in tools like occur and isearch and imenu and then do something like:

The downside is that dependency is now handled by order of code (from top to bottom) rather than the more robust require system. But it’s a small downside, which hasn’t caused me much pain, and one that can be remedied as needed by pulling any of your more library-ish stuff into a separate file and requiring it when desired.

(Probably obvious, but I’m talking about settings and hooks and bindings and one-off functions here. My plugins are not inlined in my init.el. That would be craaazy.)

You?

I challenge you to challenge yourself to consider why you care about emacs startup time. Is it the same reason I cared? Because all the emacs literature told you you cared?

Or perhaps someone out there has a head-slappingly simple system for auto-loading and configuring everything with no added complexity, in which case, I’m very curious to hear about it.

I’m also interested how folks with non-trivial emacs configurations are organizing them. Or if you adopt the one-big-ass-init-file approach, I’m curious to hear how your milage varies.

So I’m admittedly a total newb in Clojure, but in the spirit of expanding my repertoire, I like to tackle problems in it now and again. So here’s what I want from you rare folks who somehow find these posts: enlighten me. Here’s my attempt at solving a trivial problem in what is surely not idiomatic Clojure. So why not shine a little light my way and tell me how you’d do it.

The Challenge

I’m in a biking-for-beer competition and the side game this month is bike bingo. All bikers know the potential tiles (which come in such flavors as “Bike at least 30 miles in one day” or “Pass someone on a tall bike”), but we don’t know the board layout. We spend the month completing as many of the tiles as possible, and the person with the most bingos at the end of the month wins beer. And sweet, sweet, arrogant pride.

So now we need a board. I figure we only need one board, because we’ll all have earned different tiles (opposite of standard bingo where everyone plays with the same numbers but different boards). The board should be somewhat random, but we’d also like to reward those who completed the hardest tiles.

;; As promised `anything-project-files'. This smattering of code
;; allows you to define and load projects. Projects, once loaded, will
;; be indexed for all interesting files, which will let you make an
;; `anything' command to rule them all.
;;
;; Note: This is not intended to be a robust Emacs package, just an
;; example. Aw-right? Good. Let's start.

anything-project-files

;; The Common Lisp library for Emacs Lisp gives us keyword arguments
;; for defun*
(require 'cl)
(defvar projects (list) "This keeps track of all available projects.")
(defvar project (list) "And here's our current project.")
(defvar project-index (list) "This will store the project index of files.")
(defvar project-default-filetypes
'("*.el" "*.rb" "*.py" "*.rb" "*.clj" "*.php" "*.js" "*.html")
"Files for indexing.")
;; Before we can build `anything-project-files', we need to have an
;; idea of what a project is. For the purposes of this post, a
;; project will have a name, a root directory, and a list of filetypes
;; we want to index. Change this default file list to suit your own
;; needs.
;;
;; By the way, `&key' gives us keyword arguments, you can use a symbol
;; or a list of the form `(keyname default-value)' to designate a
;; keyword.
;;
;; Anything candidates can be a list of `(DISPLAY . REAL)' pairs, so
;; we throw the name up front to serve as the `DISPLAY' component."
(defun* project-create (name &key root (indexed-files project-default-filetypes))
"Add a project to the list or projects."
(add-to-list 'projects
(cons name
`((:name . ,name)
(:root . ,root)
(:indexed-files . ,indexed-files)))))
;; The handy, dandy function below makes it real easy to get a project
;; component in the form of `(project :root)' or `(project :name)'.
;; (Elisp is a Lisp-2, so function can have the same name as
;; variables.)
(defun project (key)
(cdr (assoc key project)))
;; Here's the code that indexes our project.
(defun project-reindex ()
"Update your projects index of files."
(interactive)
;; Travel to the project root
(cd (project :root))
;; Using our `indexed-files', create a string that we can toss into a
;; find command. It'll look like "'*.el' -or -name '*.rb'...".
;;
;; For a big project, we don't want to be stuck waiting while it
;; indexes, so we have to do a little jig-hoolery to make this work
;; async. Firstly, that means keeping track of the current buffer,
;; and switching to a temporary one in which to do our work.
(let* ((b (current-buffer)))
;; And here's our temp buffer.
(switch-to-buffer "*project-index*")
;; I'll use `tramp-handle-shell-command' as it returns a process
(let ((process (tramp-handle-shell-command
"find . -type f -name '*.el' &" (current-buffer))))
;; And that lets me set a sentinel (callback) for when the
;; process is complete
(set-process-sentinel process 'project-load-index))
(message "Indexing...")
;; The work has been started, so we switch back to where we were
;; when we called the command.
(switch-to-buffer b)))
;; Here's my callback function. `p' is the process and `s' is a
;; message string, which I'll ignore here.
(defun project-load-index (p s)
;; Now we'll just split-string on newline for our process buffer.
(setq project-index
(split-string
(with-current-buffer (process-buffer p)
(buffer-string))))
;; Goodbye
(kill-buffer (process-buffer p))
(message "Indexing complete."))
;; We load the project by selecting from a list an anything
;; buffer. Nice.
(defun project-load ()
(interactive)
(setq project
(anything
'((name . "Load Project")
(candidates . projects)
;; `anything' usually wants to do something with our selected candidate,
;; but here I'm just going to return it.
(action . (("Return" . identity))))))
;; The project is set, let's index it.
(project-reindex))
;; And finally, the payload.
(defun anything-project-files ()
(interactive)
(anything
'((name . "Project Find File")
(candidates . project-index)
;; `display-to-real' will take the filename selection and add our
;; project root before any action is performed on it.
(display-to-real . (lambda (c) (concat (project :root) "/" c)))
(type . file))))
;; Now we'll set up our `emacs-config' project, accepting defaults for `:indexed-files',
;; though we could certainly use `'("*.el")' if we wanted to.
(project-create "emacs-config"
:root "~/.emacs.d")
;; And there you have it. Load your `emacs-config' project, give it
;; the old `M-x anything-project-files', and you're off to the races.
;;
;; Next steps:
;;
;; Once you've got project context, you can let your imagination run
;; wild coming up with project-related tasks Emacs could be doing for
;; you.
;;
;; Also `project-load' could do more, like tagging the project. Hell,
;; you could create an `anything-project-tags' command, which would
;; rock out pretty hard.
;;
;; Indexing (and tagging) might take a long time on large projects and
;; may not be necessary on every project load. One could consider
;; saving a project index file and loading from that until you
;; explicitly reindex. It'd be pretty simple, but I'll leave that up
;; to you.
;;
;; Until next time, buenos dias! Buenos Aires!

anything-other-buffer is a simplified version of anything that takes a list of sources for its first argument and a buffer name as its second. So M-x my-anything now gives you an anything selection buffer for all them sources there. You’ll find a wealth of pre-baked sources to choose from in anything-config.el.

The next level of awesomeness comes when you create your own source, which is cheese easy. Do a C-h v on anything-sources. Here you’ll get a lengthy description of how to set one of these buggers up. I’ve made the world’s simplest, most contrived, and most stupidly named source below.

There are three mandatory keys: name, candidates, and either action or type. Here I’m pretending to be looking at a list of files, so my type is file. Types provide common actions for things you’ll commonly be working with. They’re a shortcut to actually writing your own actions.

Now that we’ve got a source, let’s make an anything command that uses it.

“Say it!” is the default action, it’s what happens when we hit return. “Spray it!” is an alternate action, which we can select by hitting tab on an item. Remember that this action selection screen is another anything screen, so you can arrow and C-n, C-p through it, but you can also just start typing what you want.

Spray it!

Yes!

Next steps

Now these examples are collosally stewpid. What’s more, they use only a tiny fraction of the shite tonne of source options anything‘ll take. If your curiosity is tingling, C-h v anything-sources and prepare to have your amazement blown.

And if you’re really and truly rapt, tune in next time, when we’ll make a for real anything-project-files source that lets you fly around the files of your gigantic, deeply nested project like an airbus on an eightball. It’ll be fun.

Actually it’s not even $10. It’s free, both as in speech and as in nachos. So in the spirit of fanning both sides of the fire, here’s a quick look at anything.el for (gasp) Emacs.

Anything.el is most certainly one of Emacs’s killer features. It’s like ido-mode on steroids, but without the rage or shrinking testicles.

It’s been described as Quicksilver for Emacs, which I guess is kinda true, but that’s not the way I use it.

The documentation for the anything function says it all:

Select anything. In Lisp program, some optional arguments can be used.

Tomas has a poet’s soul, and what he means when he says “some optional arguments can be used” is that you can go bat-shit crazy with what goes in, what gets displayed, what comes out, what happens then, where am I, why am I naked?

But before we get there, let’s cover the basics, shall we?

Installation

M-x auto-install-batch is one option. auto-install.el is here and it’s super handy. Ask for the “anything” package, and you’ll get anything.el, anything-config.el, and anything-match-plugin.el. You’ll want all three. Of course you can just follow the links and install the files yourself if that’s your thing.

Now we’re in the *anything* buffer, and you can have a merry time typing away at any part of the filename or filepath you’re after. Behold as anything.el starts chipping away at the options. C-n and C-p behave as expected. Left and right arrow keys move you to different groups of things.

Because you’ve got the match plugin installed, you get some bonus features. Say you start typing your filename, and you’ve still got 50 matches. Add a space and you’re in a whole new pattern group. So now you start typing something unique in the path, and viola, you’re looking at a full-on double rainbow.

Once you’ve found your victim, hit enter to do the default thing, which is usually the thing you want. If you’ve got eccentric taste, hit tab instead for a list of other things you can do with the thing you’ve selected. Thing.

More Youthage

Things get real crazy when you crack open anything-config.el. Go ahead and anything-imenu up in there and type in “anything”. Here’s a list of custom anything.el commands you get for free. Who needs nachos? You’ll have a riotous time mapping keys to the bazillion-ish anything.el commands the anything-config.el crew have put together for you.

For my part, C-x b is anything-mini, which serves up a combination of buffers and recent files. C-x C-i is anything-imenu, which is awesome wearing a hat. C-x f is anything for recent files, and C-x C-f is ido-find-file, which still trumps anything.el for navigating about file systems.

But wait, there’s more!

You remember that part about going guano building your own who-knows-whats on top of anything.el? It’s anything.el‘s finest suit, but alas, a subject for another post.