2008-12-29

running console programs inside emacs

Emacs comes with a nice terminal emulator called ansi-term. Using M-x ansi-term, you can run shells and other terminal programs from within emacs, and the emulation is good enough even for 'graphical' console programs. For example, I am using the mutt mailer and slrn news reader with ansi-term. I could of course use some native emacs program for news and mail (e.g,, gnus), but in this case, I prefer the external ones (I'll discuss that in some later entry). However, I really like these external programs to run inside emacs - it makes my task management easier. YMMV, of course.

Anyway, to easily use these external programs, I want some handy key bindings (shortcuts), and the following behaviour:

  • if the program is not yet running, start it in a new ansi-term-buffer;
  • if the program is already running, switch to that buffer instead.
To do that, I define a functions (disclaimer: I am no elisp-expert; there are probably better ways to do this. But this works-for-me at least): [Update: thanks to Ian and get-buffer (see comments), a shorter solution, using only one function:]
(defun djcb-term-start-or-switch (prg &optional use-existing)
  "* run program PRG in a terminal buffer. If USE-EXISTING is non-nil "
  " and PRG is already running, switch to that buffer instead of starting"
  " a new instance."
  (interactive)
  (let ((bufname (concat "*" prg "*")))
    (when (not (and use-existing
                 (let ((buf (get-buffer bufname)))
                   (and buf (buffer-name (switch-to-buffer bufname))))))
      (ansi-term prg prg))))
Using this function, it's easy to define key bindings, as explained in the key bindings entry. However, we can make it even easier using a macro:
(defmacro djcb-program-shortcut (name key &optional use-existing)
  "* macro to create a key binding KEY to start some terminal program PRG; 
    if USE-EXISTING is true, try to switch to an existing buffer"
  `(global-set-key ,key 
     '(lambda()
        (interactive)
        (djcb-term-start-or-switch ,name ,use-existing))))
Using this macro, we can now define these key bindings (shortcuts), for example like this:
;; terminal programs are under Shift + Function Key
(djcb-program-shortcut "zsh"   (kbd "<S-f1>") t)  ; the ubershell
(djcb-program-shortcut "mutt"  (kbd "<S-f2>") t)  ; mail client
(djcb-program-shortcut "slrn"  (kbd "<S-f3>") t)  ; nttp client
(djcb-program-shortcut "htop"  (kbd "<S-f4>") t)  ; my processes
(djcb-program-shortcut "mc"    (kbd "<S-f5>") t)  ; midnight commander
(djcb-program-shortcut "raggle"(kbd "<S-f6>") t)  ; rss feed reader
(djcb-program-shortcut "irssi" (kbd "<S-f7>") t)  ; irc client
And so on. Now, to summarize, if I press, say, C-f5 (shift-F5), I'll jump to a buffer with the midnight commander. If it's already running, I'll switch to that one, otherwise, a new mc will be started.

Final note: you might want to customize ansi-term a bit for your needs; EmacsWiki has some tips. You also might want to turn off hl-line-mode.

2008-12-26

showing and hiding blocks of code

When I'm writing some code, I find it often useful to hide most function bodies, and only see the one I am working on. This way, I can keep the overview, and concentrate on the part I am working on. Emacs has support for showing/hiding blocks of code, using the HideShow-package. HideShow works with C/C++, Lisp, Scheme, Java, Perl, PHP, TCL, VHDL and Fortran. It also seems to work with Javascript (at least JS2).

You can enable Hide-Show for C/C++ code by adding the following to your .emacs:

(add-hook 'c-mode-common-hook
  (lambda()
    (local-set-key (kbd "C-c <right>") 'hs-show-block)
    (local-set-key (kbd "C-c <left>")  'hs-hide-block)
    (local-set-key (kbd "C-c <up>")    'hs-hide-all)
    (local-set-key (kbd "C-c <down>")  'hs-show-all)
    (hs-minor-mode t)))
You can hide or show either the current block, or the whole file. To show the former, consider a C-function:
int my_function (int a, int b)
{
 return a + b;
}
Now, when calling hs-hide-block, we get:
int my_function (int a, int b)...
HideShow has one problem: the truly bizarre default key bindings, such as 'C-c @ ESC C-s' for hs-show-all. This is the reason I am adding these alternative key bindings in the add-hook above - 'C-c' and the arrow keys; alternatively, you could use the 'Super'-key instead of C-c.

If you'd like to hide everything by default when you open a file, you can do so by adding

(hs-hide-all)
to the above hook function.

Side-note: Even though there are many keys on your keyboard, there is a finite number of easy combinations, especially when the arrow-keys are involved. They are popular, and not just by emacs -- for example, window managers often use Ctrl-Alt-<arrow-key> for switching between virtual desktops. So, it's good to think a little bit where to 'spend' your easiest key bindings.

2008-12-25

opening emacsclient windows on the current desktop

Using emacsclient deserves its own entry, but let's assume you've already set that up. Basically, you'll put (server-start) in your .emacs, and emacs will run in server mode, which means you can open files in an existing emacs by using the 'emacsclient' command.

One small annoyance with this is that when using virtual desktop / workspaces, running emacsclient will open the file on a different virtual desktop. As with just about anything, there are some tricks to move your existing emacs to the current desktop; add the following to your .emacs:

;; move to current desktop 
(add-hook 'server-switch-hook
  (lambda ()
    (call-process
      "wmctrl" nil nil nil "-i" "-R"
      (frame-parameter (or frame (selected-frame)) 'outer-window-id))))
This works when using X (GNOME, KDE,...), and requires the wmctrl program. An alternative solution is to use a new window ('frame', remember emacs terminology) with the new buffer. I am not using that, because I prefer to use one single window. However, if you prefer to use seperate windows for each buffer opened with emacsclient, you can use this trick (courtesy of EmacsWiki) instead:
;; open a new frame
(add-hook 'server-switch-hook
   (lambda nil
     (let ((server-buf (current-buffer)))
       (bury-buffer)
       (switch-to-buffer-other-frame server-buf))))
Overall, emacsclient is a nice tool; as with many things about emacs, it does take a bit of work to make it work they way you want.

2008-12-24

some simple tricks: boxquote, footnote and snip

When writing e-mails or other text documents, you can use a couple of tricks that are best explained by example.

boxquote

Dave Pearson wrote boxquote.el; what it does is most easily explained with some examples.

Suppose we select ('mark') some random text:

This is just some random text.This is just some random text.This is just
some random text.This is just some random text.This is just some random
text.
With M-x boxquote-region we get:
,----
| This is just some random text.This is just some random text.This is just
| some random text.This is just some random text.This is just some random
| text.
`----
I use boxquote-region regularly, to quote some text, to clearly separate it from the surrounding text.

footnotes

Also, I am regularly using footnote-mode. Footnote mode allows you to create footnotes in text documents, and it tries to keep the numbering up to date. Again, and example will make this clear. Suppose we have some text:
After his demonstration of wireless communication (radio) in 1894 and
after being the victor in the "War of Currents"[1], he was widely respected as
one of the greatest electrical engineers who worked in America.
When have the cursor ('point') on the right side of "War of the Currents", we can do Footnode-add-footnode. This will add (obviously without the 'See: ...'-part):
Footnotes:

[1] See: http://en.wikipedia.org/wiki/War_of_Currents
If you add multiple footnotes, footnote-mode updates the number.

Footnode-mode is part of emacs, and you can simply use M-x footnode-mode to activate it.

snip

Finally, a little function for use in e-mails and other messages. For replying messages with questions or discussion points, it's good practice to answer them in-line (see the Trim-Posting Manifesto, for example), and to cut out or 'snip' parts that are no longer relevant. The following simple function may be helpful:
(defun djcb-snip (b e summ)
  "remove selected lines, and replace it with [snip:summary (n lines)]"
  (interactive "r\nsSummary:")
  (let ((n (count-lines b e)))
    (delete-region b e)
    (insert (format "[snip%s (%d line%s)]" 
              (if (= 0 (length summ)) "" (concat ": " summ))
              n 
              (if (= 1 n) "" "s")))))
When you select some text, and call the function (M-x djcb-snip or define a key binding), you're asked for a summary, and the text is replaced with something like:
[snip: irrelevant chatter (15 lines)]

2008-12-23

quickly switching between header and implementation

A trivial yet useful trick: when coding C/C++, you often jump from header file (.h, .hh etc.) to implementation file (.c, .cc etc.). Emacs has built-in support for this, using ff-find-other-file. We can add a key binding to .emacs:
(add-hook 'c-mode-common-hook
  (lambda() 
    (local-set-key  (kbd "C-c o") 'ff-find-other-file)))
Now, we can quickly switch between myfile.cc and myfile.h with C-c o. Note the use of the c-mode-common-hook, so it will work for both C and C++.

2008-12-22

working with rectangular selections

When editing text, sometimes it is useful to work with rectangular selections. Suppose we have some list of names and years of birth/death; however we want have a list of only the names. Normal selection of text won't work, because it selects full lines of text between the starting and finish points, something like this:
1. Isaac Newton          1643-1727
2. Carl Friedrich Gauss  1777-1855
3. Charles Darwin        1809-1882
4. Albert Einstein       1879-1955
5. Your Name             1972-?
example 1: normal selection

Clearly, that is not what we want. Instead, we'd like to do a rectangular selection, like this:

1. Isaac Newton          1643-1727
2. Carl Friedrich Gauss  1777-1855
3. Charles Darwin        1809-1882
4. Albert Einstein       1879-1955
5. Your Name             1972-?
example 2: rectangular selection

Emacs has been able to do rectangular selections since a long time; see the section in the GNU/Emacs manual. You can do something like C-x r k to cut ('kill') a rectangle, and then use C-x r y to paste ('yank').

However, one thing to remember is that the selection itself is done in the 'normal' way (see the entry on selections); i.e. you start the selection with C-SPC, and the selected area ('region') will be the anything between the starting point ('mark') and the current cursor position ('point'). Emacs does not distinguish the selection as being either a normal or rectangular -- it cannot read your mind. For example, C-w gives a 'normal' cut, while C-x r k cuts the rectangular selection.

This worked pretty well when Emacs selections were not visible (once more, see the entry on selections). You couldn't see any selection, so it was up to you to interpret it as either normal or rectangular. However, today we usually do have visual selections, which makes all selections, normal or rectangular, look like example 1.

To overcome the cognitive dissonance, there's the rect-mark-package, which makes rectangular selections visible as in example 2.

You can install the package in the normal way, and add some key bindings to your .emacs.

(require 'rect-mark)
(global-set-key (kbd "C-x r C-SPC") 'rm-set-mark)
(global-set-key (kbd "C-x r C-x")   'rm-exchange-point-and-mark)
(global-set-key (kbd "C-x r C-w")   'rm-kill-region)
(global-set-key (kbd "C-x r M-w")   'rm-kill-ring-save)
The keybindings are suggested by the package - you have to prefix the normal cut/copy/paste keybindings with C-x r. If you are a bit more adventurous, and willing to override some very basic key bindings, instead of the above, you could try:
(require 'rect-mark)
(global-set-key (kbd "C-x r C-SPC") 'rm-set-mark)
(global-set-key (kbd "C-w")  
  '(lambda(b e) (interactive "r") 
     (if rm-mark-active 
       (rm-kill-region b e) (kill-region b e))))
(global-set-key (kbd "M-w")  
  '(lambda(b e) (interactive "r") 
     (if rm-mark-active 
       (rm-kill-ring-save b e) (kill-ring-save b e))))
(global-set-key (kbd "C-x C-x")  
  '(lambda(&optional p) (interactive "p") 
     (if rm-mark-active 
       (rm-exchange-point-and-mark p) (exchange-point-and-mark p))))
This way, you only need to remember that C-x r C-SPC starts a rectangular selection, and after that, the normal keybindings (C-w, C-m and C-x C-x) work, and do the 'right thing', depending on whether you made a rectangular or normal selection. I don't really like overriding such basic key bindings, but in practice it seems to work quite well. A limitation is that this only works when using the key binding; if you copy-paste using the menu or click the icon, it won't work -- you'd need more intrusive changes for that.

You also might want to add the following key bindings to allow for rectangular selection with the mouse after pressing C-x r:

(global-set-key (kbd "C-x r <down-mouse-1>") 'rm-mouse-drag-region)

I think it would be useful if emacs included rectangular selection functionality on a bit deeper level, where all functions that work on selections would automatically use either the normal or rectangular version. Right now, rectangular selections are a more difficult to use than they have to.

2008-12-19

easy switching between visible buffers

It's common to split the emacs frame1, so we can see multiple buffers at the same time. For maximum efficiency, we'd like to use the keyboard (instead of the mouse) to switch the focus from one buffer to the next. The 'traditional' way is to use C-x o for that ('other-window'); it will take you to the next window (remember, 'window' may be what you think1); C-x o also accepts prefix arguments to go to the n-th next window.

However, its much easier to switch between visible buffers using the windmove-package (which is part of emacs):

(require 'windmove)
(windmove-default-keybindings 'meta)

After this, you can move the focus to some other window using Alt+arrow-key; so Alt-up (M-up) will go to the window above the current one, Alt-Left (M-left) goes to the left, and so on. That's a lot more intuitive C-x o multiple times to find the right window.

Note, by default, windmove uses the Shift-key instead of the Meta-key; but using the shift key conflicts with e.g. cua-mode. Because of that, I prefer the meta key; this is what the (windmove-default-keybindings 'meta) line is for. You can of course use another key if you're already using the Meta/Alt key for something else.

More recently, I have started to use the Windows-key, because org-mode already uses of meta + arrow keys for other things. Under X, you can usually find the Windows key as super key, so, you would use:

(require 'windmove)
(windmove-default-keybindings 'super)

That might not work in all places though - not on the console and, ironically, not on Windows.

Footnotes:

1 frame and a buffer are typical emacs-speak, see emacs-terminology for an overview)

prefix arguments

You can jump to the next line in emacs with C-n ('next-line'). However, how can we specify that we want to jump to the nth next (or previous) line? Or, more in general, how can we specify a (numerical) argument to an interactive function that support them?

The answer is that you can provide prefix-arguments to such functions; this can be done in two ways:

  • positive numeric arguments can be specified with M-<number>; that is, to use 5 as the prefix argument, you'd type M-5;
  • any numeric argument can be specified using C-u; so to set -3 as the prefix argument, you'd type C-u -3. Some functions also take C-u as a kind of boolean parameter; you set the parameter by specifying C-u without any parameter.
Using this, we can jump to the fifth next line with M-5 C-n, and to the third previous line with C-u -3 C-n. For details, see the emacs manual, and the documentation for the specific function (use M-x describe-function or C-h f).

Note: after ten years of using emacs, I discovered the M-<number> notation only when writing this entry...

2008-12-17

auto-detecting indentation style

When writing some C/C++-code, I usually follow a variant of the Linux coding style. Emacs understands this, if you put something like
(add-hook 'c-mode-common-hook
          (lambda ()
            (c-set-style "linux")))
in your .emacs. In practice, this means that e.g. pressing the Tab-key will do the right thing (there is a lot more to be said about that; but not here, not now).

However, sometimes I also need to edit other people's files, and they may use quite different settings - i.e., maybe they use spaces instead of tabs, or have the indentation set to 2(!). I may not like those settings, but I still want to respect them. If I would edit those files using my normal settings, I will likely screw up the existing code. It'd be nice if emacs would understand the existing settings, and use those instead of my own. Of course that is possible -- there are different ways to do that.

file local variables

First, the original author can explictly set the coding standard, i.e., if he or she puts something like
/* -*-mode:c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
in the first line of the file, and Emacs will use those settings. See Specifying File Variables in the Emacs Manual for details. File-local variables can be useful sometimes, but they are not a very complete solution. They require original to actually add them, and changes are they don't; especially if they're not using emacs.

dtrt-indent

What I'd like instead is some way to auto-detect the settings for a file, and use those. And that is exactly what Julian Scheid's dtrt-indent does; add the following to your .emacs (if unclear, see the entries on hooks and external packages):
(add-hook 'c-mode-common-hook 
  (lambda()
    (require 'dtrt-indent)
    (dtrt-indent-mode t)))
With this, emacs will automatically update the indentation settings whenever you open a C/C++/Java/...-file. Some notes:
  • In addition to C-alike languages, dtrt-indent also supports shell, Ruby, and Perl, so you could add a hook to sh-mode, ruby-mode and perl-mode/cperl-mode as well;
  • dtrt-indent wisely lets itself be overridden by abovementioned file local variables, and will not change settings if it cannot determine the settings of the current file with some degree of reliability;
  • dtrt-indent currently only detects the indentation-style, not the coding style, ie. it does not detect if you want to put the '{' on the same line as the 'if', or on the next one. In practice, this turns out not to be a big problem, although it would be nice-to-have.
Anyway, a quite useful package, that I've been happily using for a while.

2008-12-16

showing line numbers

Updated After the discussion of modes and installing packages, we can give some direct practical trick, courtesy of M-x all-things-emacs; note, I have updated this since, to work with Emacs 23.

Sometimes it is very useful to see the line numbers in a file – that is, on the left side of each line, the line number is shown. A use case for this is when showing something to a group of people. In that case, zooming and full-screen mode might also be useful.

The traditional way in emacs is to use setnu.el, but it's quite slow for big files. Fortunately, there is linum-mode; in your .emacs, add:

(autoload 'linum-mode "linum" "toggle line numbers on/off" t) 
(global-set-key (kbd "C-<f5>") 'linum-mode)    

Now, you can quickly show/hide linenumbers by pressing C-F5 (Control-F5).

You can also turn it on for specific modes; for example, to turn on line numbers when editing Perl code:

(add-hook 'perl-mode-hook
  (lambda() (linum-mode 1)))

installing packages

One of the great things about emacs is the availability of external packages. Whatever you want to do with emacs, there is a good chance that someone already solved the problem, and made a package out of it. Emacs comes with many packages, but there are still many more that it does not include. In the entry about modes, we already looked at various file-type specific packages that are available for emacs. Howeve, we did not discuss how to install external packages/modes. So, how does one install an external package? Assuming, of course, that you know what package you would like to install. This blog tries to help with helping you to identify useful ones.

The first step is, not surprisingy, to get the external package. Linux distributions like Fedora and Debian/Ubuntu have many of them pre-packaged, that is, you can get them from their repositories. This is often the easiest way to get the packages, so it's good to first check if your distribution offers the package. For example, in Debian/Ubuntu, there's emacs-goodies-el which includes a lot of useful things.

If your distribution does not include the desired package, there are some different options:

  • First, your emacs might include some kind of automatic package retrieval system; XEmacs does, and for GNU/Emacs there is ELPA. I have not used it, so I won't go into that here.
  • Second, there are bigger packages that come with their own installation procedure, documentation etc., and which are usually shipped as .tar.gz-files. An example of this is the VM (ViewMail) package, which provides an email client for Emacs. It comes with documentation and installation instructions. Another one is magit, for using the git with emacs. Magit is (at this moment) only available by getting it from its repository, but the same rule applies: just follow the installation instructions.
  • Finally, there are many single .el-files that you can find, for example, in the Emacs Lisp List. You can download such a file, and put in some directory where emacs can find it. I made a directory ~/.elisp/ for this. And at the top of my .emacs, there is something like:
    ;; my elisp directories
    (defvar elisp-path '("~/.elisp/" "~/.another-path")) 
    (mapcar '(lambda(p) (add-to-list 'load-path p)) elisp-path)
    This code creates a variable elisp-path with some directories, and uses mapcar to add each of them to load-path, which is the variable that emacs uses to determine where it can find packages. Emacs actually adds a lot of directories to load-path already before considering your .emacs; you can see the full load-path with describe-variable, or C-h v.

Now, in order to actually use a package, we have to ask for it in our .emacs. There are various ways:

  • Frequently, you need to require such a package, say:
    (require 'magit)
    This will load the package. Emacs will give an error if it cannot find the package (i.e., if it cannot find (provide 'magit) in the package), so if you are using your .emacs in different places, you might want to use the require-maybe function.
  • Another way is to use autoload. The idea is that we define a function, and the package is not loaded until we actually call that function. This can be helpful to speed up loading your emacs, and save some memory: we will only load packages that we actually use, and not before we need it. A useful pattern here is to use autoload together with auto-mode-alist:
    ;; post mode (used when editing mail / news)
    (autoload 'post-mode "post" "mode for e-mail" t)
    (add-to-list 'auto-mode-alist 
                 '("\\.*mutt-*\\|.article\\|\\.followup" 
                    . post-mode))
    This does two things: first, we use autoload to define a function post-mode; if this function is called, it will load "post", which contains the real definition of that function. Second, we use auto-mode-alist to tell emacs that whenever we open a file which name matches some pattern, call the function post-mode.

2008-12-15

modes

Emacs has special modes for many different types of files; a mode might colorize the file in some useful way, or add some functions or keybindings to easily manipulate them. Each buffer has one specific mode called the major mode. In addition, each buffer can have multiple minor modes, which enhance the major mode in some way. You can get information about the major and minor modes for the current buffer with M-x describe-mode or C-h m.

As a practical example, let's look at text files. Text files are (by default) opened in text-mode; we can add some useful minor modes to that, by using a hook in your .emacs:

(add-hook 'text-mode-hook
  (lambda()
    (blink-cursor-mode -1) ; don't blink the cursor
    (set-fill-column 78)   ; buffer-local variable; wrap at col 78
    (auto-fill-mode t)))   ; wrap around automagically
This will activate two minor modes when we use text-mode. First, we turn off cursor blinking with (blink-cursor-mode -1) (because, obviously, we don't want a blinking cursor when editing text files...). Second, it activates auto-fill-mode, a minor mode that makes emacs automatically wrap long lines ('filling' is emacs terminology for wrapping). Neither of these two minor modes is specific for text-mode, and can be used with other modes as well.

Emacs recognizes many files already, but sometimes, you need to tell it to use some specific mode for a file. To do this, there is auto-mode-alist. Suppose, we want to use text-mode for all files with extension .foo or .bar; we add to our .emacs:

(add-to-list 'auto-mode-alist
        '("\\.foo$\\|.bar$" . text-mode))
Now, whenever we open a file which matches that pattern (ends in .foo or .bar, text-mode will be activated. Please refer to the emacs documentation for details on the regular expression (the "\\.foo$\\|.bar$" pattern).

Some final notes:

  • For more information on installing modes (and other packages), see the installing packages-entry;
  • with some tricks you can actually have multiple major modes in a buffer at the same time; this can be useful for example when editing web documents that contain HTML and Javascript; see MultipleModes. It can be a bit tricky to set up though;
  • not all modes are buffer-specific; for example tool-bar-mode and menu-bar-mode (which determine whether your emacs window has a toolbar, menubar, respectively) are global for your emacs-session;
  • an interesting question is "where do I find interesting modes?"; well, you can search the Emacs documentation, or look through the Emacs Lisp List. And of course in this blog I am trying to show which ones I found particularly useful.

2008-12-14

showing trailing whitespace

When writing Makefiles, it's easy to accidentaly have some trailing spaces. This can then lead problems with lines ending in "\", for example:
  SOURCES = foo.c \
            bar.c
will ignore 'bar.c' if there is a space right of the '\'. I've bitten by this a couple of times, and it's not always directly clear that the reason for some linker errors is a superfluous space in the Makefile.

Some other editor makes those spaces/tabs visible; but by default emacs doesn't. However, it's easy to add a Makefile-hook to your .emacs:

(add-hook 'makefile-mode-hook 
  (lambda()
    (setq show-trailing-whitespace t)))
This sets the buffer-local variable show-trailing-whitespace, only for Makefiles (including Makefile.am and Makefile.in), and makes those trailing spaces visible.

Some people turn it on for all buffers, but I personally find that quite annoying: usually trailing whitespace does not mean anything.

Note that if you want to see all whitespace/tabs etc., you can use whitespace-mode (in recent emacs versions, use M-x whitespace) - which can be useful at times.

2008-12-13

emacs terminology

The first emacs versions appeared in 1976, in a world quite different from the current, in particular when it comes to computing. For this reason, emacs uses a lot of terminology that is a bit foreign for people who learned computers in later years. This presents a challenge when trying emacs itself, or its extensive documentation. Here, I try to list the emacs terminology and the 'translation' in current computer terms. I expect to be continually updating this.

As with much of what is in this blog, for just about everything a footnote could be added with some exception/alternative explanation/..., but I will resist the urge to do so.

The following picture shows some of the important concepts to keep in mind... especially when reading emacs documentation. Of course, many of the visual details depend on your color settings, operating system, emacs-version and so on...

The following table is another way to help you understand the emacs jargon:

emacs terminology common terminology
bufferdocument/workspace
evaluate expressionsinterpret/execute some code
font lockingsyntax highlighting
fillingline wrapping
framewindow
killcut
markbeginning of the selected area
mode linestatus bar
pointcursor position
regionselected area; the area between 'mark' and 'point'
visit a fileopen a file
windowwindow pane
yankpaste

2008-12-12

cycling through your buffers with Ctrl-Tab

Emacs-users tend to have many buffers open, -- maybe some source code files, an email message, a short note etc.There are many ways to switch from one buffer to another, but I like the quick and easy way of cycling through them with Ctrl-Tab, just like tabs in Firefox.

It's quite trivial to add this keybinding to your .emacs:

;; cycle through buffers with Ctrl-Tab (like Firefox)
(global-set-key (kbd "<C-tab>") 'bury-buffer)
If you want a more advanced version that ignores certain buffers, you could take a look at ControlTABbufferCycling in EmacsWiki.

remapping caps-lock

The Caps-Lock key on most keyboards is much bigger than its relative unimportance justifies. Personally, I hardly ever use the key at all. So can't we do something more useful with the Caps-Lock key than not using it?

Yes we can. Sascha Chua mentions a little trick to put in your .emacs to bind Caps-Lock to M-x:

;; bind Caps-Lock to M-x
;; http://sachachua.com/wp/2008/08/04/emacs-caps-lock-as-m-x/
;; of course, this disables normal Caps-Lock for *all* apps...
(if (eq window-system 'x)
    (shell-command "xmodmap -e 'clear Lock' -e 'keycode 66 = F13'"))
(global-set-key [f13] 'execute-extended-command)
First, it remaps the Caps-Lock key to F13 using xmodmap, and then binds that to 'execute-extended-command', a.k.a, M-x. It's important to note that this will remap Caps-Lock for all your programs, not just emacs.

Also note that the above only works on X-Windows (Linux/Unix); on MS-Windows you can use Lennart Borgmans tip instead:

(setq w32-enable-caps-lock nil)
(global-set-key [capslock] 'execute-extended-command)
As people have mentioned, you can of course map Caps-Lock to other keys instead, for example the Control key.

2008-12-11

highlighting lines that are too long

When writing code, I don't like my lines to be too long; the Linux Coding Style famously asserts that:
The limit on the length of lines is 80 columns and this is a hard limit.
In this case (and many others), I can heartily agree with this coding style. Longer lines are hard to understand (I use a limit of 100 for C++/Java).

As a small variation on the trick for highlighting "TODO", "FIXME" and friends, we can also highlight lines that are too long:

(add-hook 'c-mode-hook
  (lambda ()
    (font-lock-add-keywords nil
      '(("^[^\n]\\{80\\}\\(.*\\)$" 1 font-lock-warning-face t)))))

selections and cut-copy-paste: make it work like elsewhere

Most computer users today are used to a certain kind of behaviour when it comes to selecting text and cut/copy/paste:
  1. when I make a selection, I can see the select part with some different color;
  2. when I have selected some text, and I type something, it replaces the selection.
This is true for Windows, MacOS, GNOME, KDE and many other systems. But it's not necessarily true for emacs, although it seems that (1) has become the default for the yet-unreleased emacs version 23.

I prefer the behaviours (1) and (2) over the defaults. Fortunately, it's really easy to enable them by putting the following in your .emacs (see also the general discussion of modes):

(transient-mark-mode t)         ; make the current 'selection' visible
(delete-selection-mode t)       ; delete the selection area with a keypress
Normally, you can start a selection ('mark') with C-SPC (Ctrl-Space); the selection ('region') will be the area between this position and the cursor. However, transient-mark-mode also allows you to use the 'Windows-style' shift + arrow-key selections - at least in recent versions. I think these should be made the defaults, but defaults change only slowly in emacs-land.

Cutting text ('kill') can be done with C-w, and copying with M-w. After that, we can paste ('yank') the text with C-y. If you are used to Ctrl-X/Ctrl-C/Ctrl-V for cut/copy/paste, you could of course redefine these keys. However, especially Ctrl-X (C-x) is used in many places in emacs, and this would probably lead to tears.

However, there is cua-mode, which enables you to do that; it's smart in the sense that C-x works as 'Cut' in other systems, but it does not affect the normal usage of C-x in emacs. To enable cua-mode, add the following to your .emacs:

(cua-mode t)
Side-note: it's called 'cua-mode', because of the CUA-standard defined by IBM.

2008-12-10

highlighting "TODO", "FIXME" and friends

When doing some programming with emacs, I like to highlight certain words that are not language keywords, but still have some special significance to me.

The emacs manual mentions this really useful trick:

(add-hook 'c-mode-common-hook
               (lambda ()
                (font-lock-add-keywords nil
                 '(("\\<\\(FIXME\\|TODO\\|BUG\\):" 1 font-lock-warning-face t)))))
This adds a hook function, a function that will be called when we open a C-file, so whenever there is a 'FIXME:', it gets a different color, so it stands out:
/* FIXME: check for foo */
Some notes:
  • I am using c-mode-common-hook instead of the c-mode-hook (as the emacs manual does); this means that this will work for all C-mode languages - C/C++/Java/Objective-C/...
  • I also added some extra keywords (TODO, BUG); you can of course add your own keywords there as well. It might help to use M-x re-builder (the Emacs regular expression builder), as Emacs regular expressions can be quite tricky to get right...

hooks

When customizing emacs, often you might want to do something when some event occurs. For example, in the automatic timestamps entry, we used
(add-hook 'write-file-hooks 'time-stamp) ; update when saving
to automatically update the file timestamp whenever we save the file.

The mechanism (as the example suggests) is called a hook: you can add a 'hook-function' for some event, and it will be called if the event takes place. This turns out to be very useful. It is a very common way to customize emacs when some specific mode (there is also an entry about modes) is activated. For example, I like to have some specific settings when I am using text-mode, which is the mode for normal text files, and can add the following to .emacs:

(defun djcb-text-mode-hook ()
  (set-fill-column 79)                    ; lines are 79 chars long ...         
  (auto-fill-mode t)                      ; ... and wrapped around automagically
  (set-input-method "latin-1-prefix")     ; make " + e => &euml; etc.
and
(add-hook 'text-mode-hook 'djcb-text-mode-hook)
Note, in the example we define a new function djcb-text-mode-hook; however, you can also define the function in-line in add-hook; such a nameless function is called a lambda-function. Using a lambda-function, we could also write the above example as:
(add-hook 'text-mode-hook
       (lambda()
         (set-fill-column 79)                ; lines are 79 chars long ...         
         (auto-fill-mode t)                  ; ... and wrapped around automagically
         (set-input-method "latin-1-prefix"))) ; make " + e => &euml; etc.   
These things are equivalent; the only difference is that you could use djcb-text-mode-hook elsewhere as well, because we can refer to it by its name.

You can call add-hook multiple times for some event (hook); when the event occurs, the hook will be called in reverse order of adding them. It's best not to depend on that, as the emacs manual explains.

You should be able to find the hooks that some package provides from its documentation; just search for 'hook'...

highlighting the current line

When editing text, I find it quite nice to highlight the current line, that is, the line where the cursor is, gets a different color. This can be easily done with emacs, by putting global-hl-line-mode (more about modes) and the following in your .emacs:
;; highlight the current line; set a custom face, so we can
;; recognize from the normal marking (selection)
(defface hl-line '((t (:background "Gray")))
  "Face to use for `hl-line-face'." :group 'hl-line)
(setq hl-line-face 'hl-line)
(global-hl-line-mode t) ; turn it on for all modes by default
What this does:
  1. first, we define a special font ('face' in emacs-jargon) and we call it hl-line; there is a special format for that, which is discussed in detail in the ELisp Manual;
  2. second, we assign this 'face' to the hl-line-face-variable; this determines the face that will be used for highlighting;
  3. third, we turn the highlighting on for all modes.
That is all. Now, some notes to this:
  • You can of course experiment with the font/face: you can change the foreground and background colors, make things bold or italic, bigger or smaller, etc. -- personally I like to keep things a bit subtle;
  • In some emacs-versions, hl-line-mode may not be available; if you need to use your .emacs which such an emacs, the entry on using functions only if they are available might come handy.

2008-12-09

.emacs

Many programs have start-up settings, which they read from a configuration file or from some database. Emacs is no exception: when it starts, it reads a file called ".emacs" from your home directory. However, the big difference is that .emacs does not consists of simple "key=value"-pairs. Instead, your .emacs is an Emacs-Lisp (elisp) program itself. At startup, Emacs interprets ('evaluates') your .emacs, and, hopefully, give you the desired results. One of the goals of this blog is to show you how.

Through .emacs, you have an almost unlimited power to make emacs do what you want. You can change the colors or the keybindings, but you can also make much deeper changes. And you can make it depend on the phase of the moon, if you want.

The flip side of all this power is that editing your .emacs is a bit harder than clicking through some 'Preferences'-dialog; this is a price that the typical emacs user gladly pays, but it does make learning emacs a bit harder. I should mention that actually, there is a way to configure emacs by clicking through some dialogs; go to Options/Customize Emacs. I find the system a bit clunky, and I don't use it; but you can give it a try. Any customizations you do that way will be written to the end of your .emacs-file.

Let's look at writing a simple .emacs by hand. It could look something like this:

;; a simple .emacs

;; don't show startup messages
(setq inhibit-startup-message t)          
(setq inhibit-startup-echo-area-message t)

(column-number-mode t)  ; show column numbers
This sets two variables (with 'setq') to true (t), and activates 'column-number-mode' (also, see modes), which gives you the column number in the mode-line (the information bar in the bottom of your emacs window). Everything on a line after a ';'-character is considered a comment.

NOTE: you are not required to have a .emacs-file at all; if you don't have one, emacs will start with its defaults. In fact, it's a good idea to start with an empty .emacs, and add things as you go -- if you don't like some default, or want to things in a different way.

If you made some error in your .emacs, emacs might complain. To find the exact problem, it can be useful to start emacs as:

  $ emacs --debug-init
Also, it might be useful to start without evaluating your .emacs:
  $ emacs -q
You could also try an alternative .emacs to test things out:
  $ emacs -q -l dot-emacs-2
(in this last example you would have an alternative .emacs in a file called dot-emacs-2).

Many of the tricks and tips discussed here in emacs-fu are about adding lisp-expressions to your .emacs-file, which will then influence the behaviour of emacs in some way - maybe some external package is loaded, some keybinding is set up, some color is changed, and so on. You can find many .emacs-files used by other people on-line (mine is here). However, as I've written elsewhere, I do not recommend you to copy large parts of people's .emacs without understanding what it does. It easily leads to a strange-behaving emacs, with no idea how to fix it.

2008-12-02

automatic timestamps

Sometimes, it's nice to have a timestamp in your files; i.e. some date that is automatically updated when you save the file. For this, emacs provides timestamps. They can easily be enable in your .emacs:
(setq 
  time-stamp-active t          ; do enable time-stamps
  time-stamp-line-limit 10     ; check first 10 buffer lines for Time-stamp: 
  time-stamp-format "%04y-%02m-%02d %02H:%02M:%02S (%u)") ; date format
This tells emacs that we'd like to use timestamps, and how they should format the date. By default, to add a timestamp in a file, you'd add
Time-stamp: <>
at the top of your file, which you could then fill by calling M-x file-stamp, which would give us something like:
Time-stamp: <2008-12-02 20:35:47 (djcb)>
With 'djcb' replaced with your username. And probably with a different date....

We might want emacs to update this time stamp automatically whenever we save the file; this can be done by add a hook, ie. specifying a function that will be called when something happens; in this case we can put the following in our .emacs:

(add-hook 'write-file-hooks 'time-stamp) ; update when saving
And emacs will obediently update the time stamp whenever we save the file.

binding keys

In quite some of the blog entries we define key bindings (or keyboard shortcuts, as they are called in other places). Key bindings are an essential part of what makes emacs such a productive editor.

Key bindings are discussed in a lot of detail in the Emacs documentation; but what about the very useful little subset we need here for defining keybindings? Let's see…

kbd

When defining key bindings, it's important to understand the emacs-notation for them. Emacs allows for different ways to specify key combinations, but the easiest way is to use the kbd-macro. We're using this macro in the examples below.

To figure out how to find the notation for some key we want to bind, we can use describe-key, or C-h k (Ctrl-h k), and type the key you want to use. Emacs will tell you if it's already bound, and the representation of the key. So, for example, pressing C-h k and then Ctrl-Shift-Return shows the representation for that as <C-S-return>.

binding a function

Once we have figured out the notation for the keybinding we'd like to use, the next step is mapping it to some function. If the function takes no arguments, it's easy (see the examples below). But how to call a function that requires an argument? Suppose,we want to insert the word "hello" whenever we presses C-c h. For that, we need a lambda-function:

(global-set-key (kbd "C-c h") '(lambda () (interactive)(insert "hello")))

Also note the (interactive), to tell emacs that this is an interactive function, because we want to have some output to the current buffer. Lambda-functions require some more explanation, which deserves its own entry…

keymap

So, we have seen how we can figure out a keybinding, and how we can bind it to some function. The final important concept ist that of a keymap, which is a set of keybindings. In emacs, you have multiple keymaps, because in different situations, we'd like to use different (combinations of) keymaps. You can define keybindings that apply throughout emacs (in the global keymap), in specific modes, or in the current buffer (in the local keymap). Let's see how this works in practice.

In this entry we define a global keybinding for toggling full-screen mode; in .emacs:

(global-set-key (kbd "<f11>") 'djcb-full-screen-toggle)

Because there are so many functions available in emacs, it is impossible to have global key bindings for everything that are still easy to remember (a keybinding you have to look up is rather useless). Also, many bindings only make sense in a particular mode.

Because of that, we can have mode-specific keybindings. For example, the command ff-find-other-file (to jump from .c-file to .h-file and vice-versa, as mentioned here), is only useful in c-mode (and derivatives), so:

(define-key c-mode-map  (kbd "C-c o")    'ff-find-other-file)

Finally, to set a key only for the current buffer, we could use:

(local-set-key (kbd "<f11>")  'djcb-full-screen-toggle)

But note that usually, all buffers in a certain mode share their keymap. This means that adding a keybinding to the keymap for the current buffer will likely add it to all buffers of that type.

Conclusion

This should be enough to get you started; the big challenge now is to come up with a set of intuitive key bindings for your most-used function. That is not so easy…

2008-12-01

running emacs in full-screen mode

Sometimes it's nice to run emacs in full-screen mode; for example if you don't want to be distracted by your pretty desktop wallpaper etc. When we're using the X-Window system, we can ask the wmctrl-program to make our window full-screen by adding the following to .emacs:
(defun djcb-full-screen-toggle ()
  "toggle full-screen mode"
  (interactive)
  (shell-command "wmctrl -r :ACTIVE: -btoggle,fullscreen"))
This requires the wmctrl program, for which there is (hopefully) a package in your distribution.

We can make a keybinding for this to, say, F11, which is the same one that e.g. FireFox use:

(global-set-key (kbd "<f11>")  'djcb-full-screen-toggle)

Note that instead of shell-command we could use the djcb-shell-command-maybe function instead, and get a warning if wmctrl is not installed.

zooming in/out

In some situations is nice if you can increase your font size in emacs. For example, when doing a presentation. This can be made very to do with emacs, with inspiration from blog.febuiles.com, which is no longer active (add to .emacs):
(defun djcb-zoom (n)
  "with positive N, increase the font size, otherwise decrease it"
  (set-face-attribute 'default (selected-frame) :height 
    (+ (face-attribute 'default :height) (* (if (> n 0) 1 -1) 10)))) 
It's more useful if we also have some keybindings for that. Let's make it the same one that compatible with FireFox and some other programs use; so, Ctrl-+ for zoom-in and Ctrl-- for zoom out, both on main keyboard and the numerical island. We add:
(global-set-key (kbd "C-+")      '(lambda nil (interactive) (djcb-zoom 1)))
(global-set-key [C-kp-add]       '(lambda nil (interactive) (djcb-zoom 1)))
(global-set-key (kbd "C--")      '(lambda nil (interactive) (djcb-zoom -1)))
(global-set-key [C-kp-subtract]  '(lambda nil (interactive) (djcb-zoom -1)))
Easy yet very useful; something like should be part of emacs... until, these simple functions will do.

running some external program only if it exists

You can easily run external programs from emacs; one way to do it is by using the shell-command-function. However, as discussed here, if you are using emacs in different environments, you might want to check first if the program is available, and give a warning if it's not there.

One simple helper function you can use in your .emacs for this, could be something like:

(defun djcb-shell-command-maybe (exe &optional paramstr)
  "run executable EXE with PARAMSTR, or warn if EXE's not available; eg. "
  " (djcb-shell-command-maybe \"ls\" \"-l -a\")"
  (if (executable-find exe)
    (shell-command (concat exe " " paramstr))
    (message (concat "'" exe "' not found found; please install"))))
Of course, this function is not very useful by itself, but we'll use it in later entries, and of you can use it in your own scripts.

using packages / functions only if they are available

If you use your .emacs in many places (different machines, different versions of emacs), you cannot always be sure if certain packages or functions are available. The following macros for your .emacs allow you to do things only if the package or function is available. Note: these macros are not very useful by themselves, but we will use them in later entries.
(defmacro require-maybe (feature &optional file)
  "*Try to require FEATURE, but don't signal an error if `require' fails."
  `(require ,feature ,file 'noerror)) 

(defmacro when-available (func foo)
  "*Do something if FUNCTION is available."
  `(when (fboundp ,func) ,foo)) 
If you place the macros somewhere in the beginning of your .emacs, you can use them as follows (just some examples):
;; change cursor color based on mode (insert/overwrite)
(when (require-maybe 'cursor-chg)  ; Load this library
  (change-cursor-mode 1) ; On for overwrite/read-only/input mode
  (toggle-cursor-type-when-idle 1)) ; On when idle
and
(when-available 'set-fringe-mode  ; emacs22+ 
  (set-fringe-mode 2))            ; don't have too much space left of col1
For more information on installing modes (and other packages), see the installing packages-entry.

welcome to emacs-fu

Welcome to Emacs-Fu!

(If you want to read emacs-fu through your RSS feed reader, please read this)

This is my blog discussing little (and not so little) tweaks to make working with the Emacs Text Editor even nicer. I have been an emacs-user for the last decade or so. I spend a lot of time with it. Emacs allows near-infinite customization, and I am always trying to improve things.

Over the years, I found many little tricks & tips. With this blog, I'm trying to share these with others. I am not claiming I came up with all these things myself. Quite the contrary - most things are inspired by things I found while foraging through the web. Also, I am definitely not claiming that the way shown here is the only way or the best way. I am not an emacs guru - I don't even wear a beard. I do welcome comments with suggestions and improvements.

Now, regarding the content: I don't want to limit myself too much; but the goal is to have something short you can read in a few minutes and pick up something useful, maybe while reading it in a feed reader. While emacs' documentation is very extensive, it can also be a bit intimidating. I'd like to offer bite-size chunks that are directly useful (e.g. running emacs in full-screen mode), or sometimes some background information (e.g., about binding keys or .emacs itself). Also, an over overview of emacs terminology may be quite useful. I'll be updating older posts whenever that makes sense.

Sometimes, I might write a bit longer, if the subject requires it.

About the target audience: the tips here probably make little sense for people who have never used Emacs before; and they might be trivial for the true Emacs wizards. So, the target audience is somewhere in between.

The kind of tips here are meant to be small additions to your .emacs (or ~/.emacs.d/init.el/. See the ever-useful EmacsWiki for some general information about this. After you've added or changed a function in .emacs, you can 'activate' it by either restarting emacs, or calling M-x eval-buffer. Of course, you can find my full .emacs.

However, I would definitely not recommend copying my or other people's .emacs without understanding what things mean. It's much better to start from scratch, and organically developing your very personal .emacs so things work exactly the way you like it. This blog is really about identifying small, understandable and useful nuggets that you can re-use.

I am using the as-of-yet unreleased Emacs version 23; most things should work just fine with the released versions. XEmacs-users might face some more problems. But again, please leave a comment if something is not working for you.

There are many other excellent resources with emacs information available; I already mentioned EmacsWiki, and many other emacs blogs are aggregated in Planet Emacsen. If you're just starting, you can try the built-in tutorial you can start in emacs with C-h t (ie. press Ctrl-H and press 't'). I am not sure if I can recommend it – many things seem a bit outdated (i.e., using C-v instead of simply PgDown…). But I digress.

Some practical points: I try to prefix the functions/macros I define with djcb-. That's not just because I am so pretentious, but also to clearly separate them functions that come with emacs, or a part of other packages. Another practical point is that all the code snippets here can be used for any purpose, i.e. they are in the public domain. External scripts have their own licenses, which you should check before using or distribution them.

Finally: I hope this blog provides some useful information to Emacs-users everywhere. If you have an emacs tip you'd like to share with your fellow emacs users, please let me know.

You can contact me through e-mail (GPG-key); I am djcb in IRC and Twitter.