M-x all-things-emacs

Keyboard Macros in the Wild: The Mundane SQL Fix

April 5th, 2007 by Rob Christie · 120 Comments

When you first start using emacs, there is normally someone who says something like keyboard macros in emacs rock. At that moment in time, they show you some quick little example, it looks neat but it probably doesn’t register with the true magnitude that it should. This series shows examples of macros “in the wild”. I hope it helps reinforce the value of the keyboard macro. [Read more →]

→ 120 CommentsTags:kb-macros · tips

IBM Emacs Tutorial, Part I

April 2nd, 2007 by Ryan McGeary · 107 Comments

Michael Stutz recently wrote Part I of a new new series at IBM called Emacs editing environment. The first is Learn the basics of Emacs.

This first tutorial is definitely geared toward the emacs newcomer. There is a lot of overlap with the built-in emacs tutorial (C-h t), but different mediums are good for teaching different kinds of people. Also, reinforcement of the basics is good for a budding emacs hacker.

Note: IBM requires free registration to read the tutorial.

→ 107 CommentsTags:newbie · news

Quick Tip: Line Numbering

March 29th, 2007 by Rob Christie · 172 Comments

linum I frequent the gnu.emacs.sources news group as one route to finding out about new packages. linenum.el came across the list recently. The package displays line numbers on the left side of your buffer. The author indicates that it is an alternative to setnu.el with the benefit that it works incrementally and can handle large files. It seems like one of those packages that is nice to have in your bag of tricks. Of course, you may just want the current line number in your modeline by using the M-x line-number-mode command. You can set it by default with the following line in your .emacs file.

(line-number-mode 1)

And in case you forgot (or didn’t know), M-g M-g (or M-g g) is the key binding for M-x goto-line in Emacs 22.

→ 172 CommentsTags:quick · tips

Newbie Tip: isearch Word Yank

March 26th, 2007 by Ryan McGeary · 124 Comments

Our newbie tips are our way of expressing emacs features we might otherwise take for granted. We hope they help beginners climb the learning curve faster. This particular tip is one that I wish I knew far sooner when I first started using emacs.

Incremental searching with isearch-forward and isearch-backward (C-s and C-r) is crucial in day-to-day and even minute-by-minute emacs usage. Maybe you want to find all uses of a particular variable name, or maybe you want to make sure you’re not repeating the same word in a piece of literature. Let’s dive in with an example:

Suppose we want to search for the word “lorem” in the folowing paragraph:

Step 1: Incremental Search Yank

We could type C-s l o r e m, but seeing as our point is already at one particular instance of “lorem,” we can instead enter C-s C-w. The result is the same. When in isearch-mode, C-w yanks the next word in the buffer and appends it to the end of the current search string:

Step 2: Incremental Search Yank

Now suppose we want to search for instances of “lorem ipsum.” Just hit C-w again:

Step 3: Incremental Search Yank

Awesome, we just saved 9 key presses — a productivity boost.

C-w is part of the isearch-mode-map. There are many isearch modifiers. Here are some that I use most frequently:

  • C-s – to search again forward
  • C-r – to search again, but backward
  • C-w – to yank next word or character in buffer onto the end of the search string, and search for it
  • M-y – to yank last killed text onto end of search string and search for it
  • M-r – to toggle regular-expression mode
  • M-e – to edit the search string in the minibuffer

For a full listing of available isearch modifier keys, review the help for isearch (C-h f isearch-forward). It is also possible to add your own functions to the isearch-mode-map. Last month, we referenced a way to add occur to isearch.

→ 124 CommentsTags:isearch · newbie · quick · tips

Bookmark Mania

March 22nd, 2007 by Rob Christie · 111 Comments

Bookmarks are one of those features that I have used, but have been on my list to learn more about. This week I decided to bump it off my list and pass along my notes. There are a number of different packages that can be used for bookmarking. I will discuss three. [Read more →]

→ 111 CommentsTags:reviews

Quick Tip: set-goal-column

March 17th, 2007 by Ryan McGeary · 245 Comments

On the surface, it might not sound like editing columns of text is something that happens a lot, but I find myself doing it fairly often. A quick way to enter different values on each line down a column is to use set-goal-column (bound to C-x C-n).

Let’s say we have a block of text like the following comma-separated list. We’d like to enter a different value in the second field of each line. First we position the point on the first line between the two commas. This is at column position 6 (emacs starts counting columns at zero).

Step 1: set-goal-column

Now let’s input our first text field and hit C-n to go to the next line.

Step 2: set-goal-column

It does what you might expect. Because our first field pushed us out to column position 23, when we went to the next line our point was placed at the end of the second line. To enter a new field on line 2, we’ll have to first jump back to column 6 manually. That’s no fun. Let’s start over and use set-goal-column instead.

Step 1: set-goal-column

Now, before we enter our first field, we’ll first hit C-x C-n. Setting a goal column sets the current horizontal position as a goal for C-n and C-p. Let’s see what happens when we re-input our first field and hit C-n.

Step 3: set-goal-column

Perfect. We’re right were we need to be. Quickly, we finish our text entry down the column:

Step 4: set-goal-column

To turn off the goal column, pass a non-nil argument to set-goal-column (i.e. C-u C-x C-n).

Note: When you run set-goal-column for the first time, you’ll receive a warning that the function is disabled. You can either follow the built-in instructions and customizations to enable the function, or you can add the following to your emacs initialization.

(put 'set-goal-column 'disabled nil)

This exercise provided a nice way to manage a column of text with different values on each line. When the task is more repetitive, emacs rectangles come in handy. We’ll make rectangles a future topic of discussion.

→ 245 CommentsTags:quick · tips

Quick Tip: re-builder

March 15th, 2007 by Rob Christie · 176 Comments

Have you ever wanted to test out regular expressions in emacs? Until I found out about re-builder I used to run M-x isearch-forward-regexp (also bound to C-M-s) in a buffer until I figured out the expression I needed. This works but just having M-x re-builder in your bag of tricks helps. In the words of the author, Detlev Zundel

When I have to come up with regular expressions that are more complex than simple string matchers, especially if they contain sub expressions, I find myself spending quite some time in the `development cycle’. `re-builder’ aims to shorten this time span so I can get on with the more interesting bits. With it you can have immediate visual feedback about how well the regexp behaves to your expectations on the intended data.

re-builder supports a number of different input forms for the regular expression. The input type can be changed by C-c TAB when you are in this mode.

Read Regexp Syntax in re-builder

re-builder read syntax example

Another nice feature is the highlighting of the different subexpression matches. Xemacs has added perl regular expression support to re-builder as noted on the emacs wiki. I hope this gets added back into GNU emacs.

→ 176 CommentsTags:quick · tips

Tab Completion Everywhere

March 12th, 2007 by Ryan McGeary · 93 Comments

I am addicted to tab completion in the minibuffer and in shells like bash — so much so that I want it everywhere. By default, emacs comes with dabbrev-expand (bound to M-/). When invoked after typing the first few letters of a word, dabbrev-expand first searches the current buffer and then other open buffers for possible completions. Completions are expanded inline on each subsequent call. This is pretty close to what we want, but it’s not the TAB key. We could rebind dabbrev-expand to TAB, but then how would we indent a line? Let’s apply a little bit of elisp smarts.

(defun indent-or-expand (arg)
  "Either indent according to mode, or expand the word preceding
  (interactive "*P")
  (if (and
       (or (bobp) (= ?w (char-syntax (char-before))))
       (or (eobp) (not (= ?w (char-syntax (char-after))))))
      (dabbrev-expand arg)

If we’re at the end of a word boundary, this function invokes dabrev-expand; otherwise, indent-according-to-mode. To avoid errors, the bobp and eobp calls are there to make sure we’re not looking for characters before or after the buffer’s boundary. An interactive prefix argument is optional and is passed directly to dabbrev (C-h f dabbrev-expand for more info).

A positive prefix argument, N, says to take the Nth backward distinct possibility. A negative argument says search forward.

I find that this makes for some wicked-cool behavior. For me, it seems intuitive and natural. But wait, we haven’t bound this to TAB yet. Let’s do that now.

(defun my-tab-fix ()
  (local-set-key [tab] 'indent-or-expand))
(add-hook 'c-mode-hook          'my-tab-fix)
(add-hook 'sh-mode-hook         'my-tab-fix)
(add-hook 'emacs-lisp-mode-hook 'my-tab-fix)
;; more mode hooks, yada yada, etc ...

To avoid strange behavior in the minibuffer and other special buffers, it is necessary to rebind TAB only in local modes. I’m sure there is a smarter/cleaner way to do this by instead excluding only the problematic buffers or modes, but I’ll leave that as an exercise for the reader (please post a comment if you have a solution though).

Credits: While I’ve had this nugget in my .emacs for a very long time, I cannot take credit for it. I’ve seen similar solutions in several places, and while I tweaked it a bit myself, I originally stole it from someone else. Emacs-Rails takes this approach one step further by also including snippet support.

Hippie Expand Alternative

We used dabbrev-expand above, but I am considering changing that to hippie-expand instead. Hippie Expand is more powerful, but might require some customization to suit to your liking. I’m not ready for groovy vans or tie-dye, so we’ll have to talk about the details of Hippie Expand sometime later.

→ 93 CommentsTags:elisp · tips

Quick Tip: scroll-all-mode

March 8th, 2007 by Ryan McGeary · 108 Comments

I don’t use this minor mode very often, but in rare cases, I find it valuable. When you have your frame split with multiple windows, sometimes it’s nice to scroll all the windows in unison. Try running M-x scroll-all-mode. Afterwards, scrolling commands entered in one window apply to all visibile windows. This works especially well when you have two windows split horizontally (C-x 3, side by side). To turn the behavior off, just run M-x scroll-all-mode again.

P.S. If it weren’t for Ediff, I’d probably scroll-all more often.

→ 108 CommentsTags:quick · tips

Recent Features in Carbon Emacs

March 5th, 2007 by Rob Christie · 83 Comments

It’s always nice to delete code or configuration files while maintaining or increasing functionality. The 2007-01-06 Carbon Emacs build now imports paths from the user’s shell (bash, tcsh, zsh). Note: The latest available build is from 2007-01-21, and includes a few more goodies. This added functionality allowed me to delete a few setq exec-path and setenv "PATH" statements from my .emacs.

Carbon Emacs includes a number of bundled lisp packages. Since my switch to Mac OS X and Carbon Emacs last year, I started removing duplicate lisp packages from my load path. The decision of whether to keep a package in my site lisp or utilize the bundled package was initially hard. I am a pack rat by nature, and initially I didn’t want to give up control. Eventually, I decided to use the bundled packages for all but the modes that I already update directly from version control repositories. My rationale is that if I am not actively updating a package, then it’s better to just clean up my site-lisp folder and reduce the clutter.

→ 83 CommentsTags:misc · news · osx