Skip to content

Converting between tabs and whitespace

Dec 2 10
by mickey

It’s not an uncommon occurrence to find yourself editing a file that uses tabs instead of whitespaces, or vice versa. Thankfully, Emacs has facilities in place that make it possible to easily convert between tabs and spaces.

The commands tabify and untabify do just that; they convert the region to tabs or whitespaces. When you untabify or tabify, Emacs is smart enough to realign your code, so it should look the same after the replacement has taken place. When you use either command, the variable tab-width is also used to determine the indentation level.

One important point, though, is that tabify and untabify does not discriminate when it replaces tabs with spaces and vice versa, so that means tabs or whitespaces in strings may suffer. Be careful.

Evaluating Elisp in Emacs

Nov 29 10
by mickey

There are several ways of evaluating elisp code in Emacs, and picking the right approach will help you get your job done faster and more efficiently. If you’re new to elisp you will quickly realize that Emacs has many shortcuts and features that makes writing, inspecting or debugging elisp code very easy.

read more…

Disabling Prompts in Emacs

Nov 14 10
by mickey

I find prompts in Emacs very annoying and in-my-face, so I have gone out of my way to remove or minimize any interaction I have with them.

Let’s start out by getting rid of the “yes or no” prompt and replace it with “y or n”:

(fset 'yes-or-no-p 'y-or-n-p)

Next up is the annoying confirmation if a file or buffer does not exist when you use C-x C-f or C-x b.

(setq confirm-nonexistent-file-or-buffer nil)

If you use ido-mode I recommend disabling the prompt that asks you if you want to create a new buffer if you enter a non-existent buffer in C-x b. You can replace always with never which does the opposite: disables new buffer creation in ido’s switch buffer routine. Setting it to never is an exceptionally bad idea as creating buffers on-the-fly is a very useful thing to do if you want a quick throw-away buffer.

(setq ido-create-new-buffer 'always)

You can also rid yourself of the splash screen and the echo area message:

(setq inhibit-startup-message t
      inhibit-startup-echo-area-message t)

And finally, the recently-added prompt in Emacs 23.2 that asks you if you want to kill a buffer with a live process attached to it:

(setq kill-buffer-query-functions
  (remq 'process-kill-buffer-query-function
         kill-buffer-query-functions))

Making CamelCase Readable with Glasses-Mode

Nov 10 10
by mickey

If you’re working on a codebase that makes use of CamelCase identifiers LikeThis or evenWorseLikeThisAbstractFactoryFactory you can make them readable by splitting up the words with M-x glasses-mode. glasses-mode will insert a virtual underscore separator between the conjoined words, so fooBarBaz will look like foo_Bar_Baz.

The changes are not permanent and Emacs will keep track of the virtual separators and ensure they are never accidentally saved to disk.

When glasses-mode is enabled, you should continue following the CamelCase style as Emacs will automagically insert the virtual separator, as needed, when you type a capitalized character.

There’s a handful of configurable options you can tweak to personalize glasses-mode. To access them, type M-x customize-group RET glasses.

Running Shells in Emacs: An Overview

Nov 1 10
by mickey

To use Emacs effectively, you must learn to use all that Emacs has to offer. One of Emacs’s strongest selling-points is its shell integration and terminal emulation. If you come from other editors or IDEs you’re probably using an external terminal window (like xterm), a terminal multiplexer (like GNU Screen) or console window (in Windows) and switching back and forth. But there’s a better way…

read more…

Maximizing Emacs on startup

Oct 18 10
by mickey

Until recently it was impossible to start Emacs in maximized mode in X, but that changed with the release of Emacs 23.2. Now you can force Emacs to start in maximized mode with the command line switch --maximized or -mm.

In Windows you have to use a bit of elisp and Win32 magic to get it to work.

Add this to your .emacs file to make Emacs start in maximized mode in Windows:

(defun maximize-frame ()
  "Maximizes the active frame in Windows"
  (interactive)
  ;; Send a `WM_SYSCOMMAND' message to the active frame with the
  ;; `SC_MAXIMIZE' parameter.
  (when (eq system-type 'windows-nt)
    (w32-send-sys-command 61488)))
(add-hook 'window-setup-hook 'maximize-frame t)

The code will only execute on Windows, and it works by sending a WM_SYSCOMMAND window message to itself, telling it to maximize. The magic number 61488 is a constant declared as SC_MAXIMIZED.

Making tooltips appear in the echo area

Oct 15 10
by mickey

By default Emacs will display its tooltips in a separate frame. If you want to force Emacs to use the echo area exclusively, you can do that with this handy code snippet:

(tooltip-mode -1)
(setq tooltip-use-echo-area t)

Olé! Diacritics in Emacs

Oct 13 10
by mickey

If you’ve ever had to write the word résumé or über or smörgåsbord and, like me, lacked the keyboard character set to do it, you’ve probably reached for an external program to type those characters. That’s completely unnecessary though, as Emacs has complete support for Unicode and has several input methods that makes Emacs act like a bilingual keyboard but without the hassle of having to change your keyboard character set.
read more…

Highlighting by Word, Line and Regexp

Oct 13 10
by mickey

With Emacs’s Hi-Lock mode you can highlight by word, line or regular expression with ease. The highlighter functionality in Emacs is particularly helpful when if you are trying to do complex code refactoring or if you are fixing data or text by hand.
read more…

Introduction to Ido Mode

Oct 10 10
by mickey

There are many ways of improving your productivity when you use Emacs, and Ido (or “Interactively DO things”) is one of those packages that you enable and then never, ever turn off again. It’s simply that useful. By super-charging Emacs’s completion engine and improving the speed at which you open files and buffers, you will significantly cut down on the time spent doing these menial tasks. Think about how often you switch buffers, open files or navigate directories in Emacs.

read more…