March 29th, 2007 by Rob Christie · 5 Comments
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.
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.
Tags:quick · tips
March 26th, 2007 by Ryan McGeary · 3 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:

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:

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

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.
Tags:isearch · newbie · quick · tips
March 22nd, 2007 by Rob Christie · 12 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 →]
Tags:reviews
March 17th, 2007 by Ryan McGeary · 7 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).

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

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.

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.

Perfect. We’re right were we need to be. Quickly, we finish our text entry down the 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.
Tags:quick · tips
March 15th, 2007 by Rob Christie · 5 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

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.
Tags:quick · tips
March 12th, 2007 by Ryan McGeary · 10 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.
1
2
3
4
5
6
7
8
9
| (defun indent-or-expand (arg)
"Either indent according to mode, or expand the word preceding
point."
(interactive "*P")
(if (and
(or (bobp) (= ?w (char-syntax (char-before))))
(or (eobp) (not (= ?w (char-syntax (char-after))))))
(dabbrev-expand arg)
(indent-according-to-mode))) |
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.
1
2
3
4
5
6
7
| (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.
Tags:elisp · tips
March 8th, 2007 by Ryan McGeary · No 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.
Tags:quick · tips
March 5th, 2007 by Rob Christie · No 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.
Tags:misc · news · osx
March 3rd, 2007 by Ryan McGeary · No Comments
If you are the type who likes to do everything within emacs, check out g-client. For a full writeup, read An Emacs Client For Google Services to learn how to integrate Google’s Blogger, Reader, and Calender with emacs.
Tags:misc · news
March 1st, 2007 by Rob Christie · 1 Comment
Java .properties files are normally formatted using conf-javaprop-mode. Recently, I was frustrated because a single quote in a property was highlighted such that multiple lines were colored like a string until there was another quote on another line. I decided I was going to fix it… It turns out that I didn’t have to look far. C-h f conf-javaprop-mode led me to read about conf-mode which led to finding the command conf-quote-normal. This command sets the syntax of ‘ and “ to punctuation. This is the joy of using emacs. It’s the little things. My new .emacs snippet follows:
(add-hook 'conf-javaprop-mode-hook
'(lambda () (conf-quote-normal nil)))
Before

After

Tags:java · quick · tips