Monday, April 28, 2008

Emacs Tip #19: Startup Options (-q)

The most useful two options I've found for starting up emacs are:

    -q
and
    -debug-init
The first disables the loading of your .emacs and default.el files, which is handy when you want to differentiate between problems in your .emacs file and problems outside (.Xdefaults, site specific .emacs, etc.).

The second gives you a backtrace when any error happens during loading of your .emacs file. While it doesn't often pinpoint the exact line where things went wrong, it usually gives you a clue as to where to start looking for problems.

For all options, read the manual here: Initial Options - GNU Emacs Manual

Monday, April 21, 2008

Emacs Tip #18: Keyboard Macros

If you find yourself doing a repetitive set of keystrokes, but don't want to bother with learning how to actually write a script for that, then record a keyboard macro.


C-x ( begins recording
C-x ) ends recording
C-x e executes the last keyboard macro
C-x C-k lets you edit the keyboard macro you just wrote

If find yourself using lots of macros, you can even save them to your
.emacs and name them (for later use).

For more documentation, see the info page:

C-h K C-x (

Or check out the Keyboard Macros section in the Emacs wiki.

Monday, April 7, 2008

Emacs Tip #17: flyspell and flyspell-prog-mode

Being in front of the computer hasn't helped my spelling because it's so easy to let the computer catch and fix the spelling errors for me. Of course Emacs has spell checking (M-x spell-{buffer,region,string,word}), but what is really handy is spell-checking as you type.

Enter flyspell.

flyspell is a minor-mode, so enabling it only causes spell-checking in that buffer, so it's handy to add the hooks to turn it on for the modes you deem appropriate (mail messages, text files, etc.). But wait, you do most of your work programming, right? Just turn on flyspell-prog-mode, and only your comments and strings will be checked for spelling errors.

It has the standard installation - only the major entry point is the routine 'flyspell-mode where the filename is "flyspell.el", so you need to tell Emacs to look for that appropriately. Here's what I use to enable flyspell and turn it on for a couple of different modes.


(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(add-hook 'message-mode-hook 'turn-on-flyspell)
(add-hook 'text-mode-hook 'turn-on-flyspell)
(add-hook 'c-mode-common-hook 'flyspell-prog-mode)
(add-hook 'tcl-mode-hook 'flyspell-prog-mode)
(defun turn-on-flyspell ()
"Force flyspell-mode on using a positive arg. For use in hooks."
(interactive)
(flyspell-mode 1))

Tuesday, April 1, 2008

Emacs Tip #16: check-parens

Ever had trouble finding a mismatched parentheses in a file? Of course not, you don't make mistakes.

But, if you do, you can use the built in functionality of M-x check-parens

The documentation is here Parentheses - GNU Emacs Manual

It doesn't pinpoint the problem as closely as I would have hoped in some modes, but it's better than nothing.

Monday, March 24, 2008

Emacs Tip #15: indent yanked code

I'm a bit apprehensive about this chunk of code, mainly because it facilitates cut/paste coding, which I abhor. Nevertheless, it does come in handy.

When you (shudder) cut/paste code, one of the first things you do is immediately indent the code appropriately. Well, why not have that done automatically for you? This chunk of emacs lisp does the trick rather nicely.

It will not indent regions that are too large (see yank-advised-indent-threshold) and given a prefix argument, it will not indent.


;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes '(emacs-lisp-mode
c-mode c++-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
lisp-interaction-mode
LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")

(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")

(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(indent-region beg end nil)))

(defadvice yank (after yank-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

(defadvice yank-pop (after yank-pop-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

Thursday, March 20, 2008

Interview-1

I've written before about interviewing, but I think there's a topic people have ignored, namely that people conducting interviews forget (in my vast experience of employment in 2 companies) is that a part of the purpose of the interview is to sell the group/company to the interviewee.

Shortly after the post in August, a friend of mine started looking for jobs, he interviewed at companies X and Y, and his experiences were very different.

Let me compare and contrast the two interviews:

At company X, he was underwhelmed by the technical lead for the job (the guy is nice, but somewhat soft-spoken, so not a lot of energy/excitement), and the questions posed by all the interviewers were pretty shallow. In other words, the questions were like, "write a routine to do sorting", which he did, and then they moved on to the next question - no further investigation of why he chose that, how might he optimize it, etc. etc. etc. It sounded kind of like watching an interview on TV where the questions were all 'yes' or 'no' questions.

At company Y, the questions were very often 'deep' questions, where the initial answer was picked apart and explored. The questions also seemed to be a little more interesting and challenging - one of the guys asked my friend how he might approach a problem that the guy had been working on that entire week.

Needless to say, my friend decided against working at company X and was very tempted by company Y.

I recall my interview at Mentor, and it was somewhat similar to my friend's at company X, only I wasn't asked to write any code. I did bring some samples of what I'd written and offered them to the hiring manager. I presume the interviewers liked what they saw in me, but I was concerned because I expected to be asked to show some level of competence in the job for which I was interviewing (a programmer).

I always figured that if you're interviewing for a job at an auto-repair shop, you'd be expected to poke around an engine and show some competence. The same thing should go for a programming job. And, wouldn't you be a little concerned if you knew your mechanic didn't require a competency exam before hiring his mechanics? (See raganwald's hiring a juggler post.)

So remember, the quality of the interview you provide influences how the interviewee views the company.

Monday, March 17, 2008

Emacs Tip #14: caps lock on windows

In my previous job, everyone had Windows laptops, and worked using VNC. But they never learned the trick to change the Caps Lock key into a Ctrl key.

From the GNU Emacs FAQ for Windows: "Caps"

Download caps-as-ctrl.reg to make CapsLock a Control key (leaving your original control keys as they were), or caps-ctrl-swap.reg to swap CapsLock and the left Control key. Once you've downloaded them, double-click on the .reg files in Explorer or "run" them from a command prompt to have them update your registry.
If you use Emacs and you are using Windows and your Caps Lock key is still functioning as Caps Lock, just download the registry change and install it. If you really think you need to type in all caps, learn M-x upcase-region