NOAA

Geophysical Fluid
Dynamics Laboratory

Skip to: [content] [navigation]
If you are using Navigator 4.x or Internet Explorer 4.x or Omni Web 4.x , this site will not render correctly!

gfdl homepage > people > v. balaji's homepage > this page

balaji's emacs setup

Why would anyone ever do their typing in anything else?

I do still use vi an awful lot, and haven't lost vi finger-memory...

And let's not forget that ed is the standard text editor.


My emacs setup

Here is my emacs setup...

Users with access to GFDL filesystems can examine or copy my /home/vb/.emacs.

I run emacs in server mode: this means there is a single session running at any time. Anytime I open a new file in emacs from the command line, it's actually a client connecting to a single emacs session. This speeds up emacs startup by a lot. If there is a running emacs in server mode, you can start up emacs with the command emacsclient, which will connect to an existing "server".

These lines in ~/.emacs put emacs in server mode:

; use emacs-server, all subsequent edits on same host become clients
;     working in own frame
; set up server to start each new client on its own frame
(add-hook 'server-visit-hook 'make-frame)
; switch the parent frame back to its own buffer
(add-hook 'server-switch-hook
          '(lambda ()
             (server-switch-buffer (other-buffer))))
(server-start)

; frames
(setq frame-title-format "%b")		; use buffer name for title
(setq display-buffer-reuse-frames t)    ; no new frame if already open

I use the shell alias:

alias e 'emacsclient -a emacs \!* &'

This always tries to start a client first... emacs is only started when emacsclient is unable to find a server to connect to.

The recommendation in emacs is to attach custom keybindings to the C-c table: for instance I've bound C-c g to the function goto-line. I have many keybindings. You may find these particularly useful:

; interchange LFD and RET so that RET auto-indents where applicable
(global-set-key (read-kbd-macro "RET") 'newline-and-indent)
(global-set-key (read-kbd-macro "LFD") 'newline)

Auto-indentation works very well for typing code, bulleted lists, and so on.

If you have a mouse with a scroll wheel, you may like these:

;; Wheel-mouse actions, from http://koala.ilog.fr/colas/mouse-wheel-scroll/
(defun up-slightly () (interactive) (scroll-up 5))
(defun down-slightly () (interactive) (scroll-down 5))
(global-set-key [mouse-4] 'down-slightly)
(global-set-key [mouse-5] 'up-slightly)

(defun up-one () (interactive) (scroll-up 1))
(defun down-one () (interactive) (scroll-down 1))
(global-set-key [S-mouse-4] 'down-one)
(global-set-key [S-mouse-5] 'up-one)

(defun up-a-lot () (interactive) (scroll-up))
(defun down-a-lot () (interactive) (scroll-down))
(global-set-key [C-mouse-4] 'down-a-lot)
(global-set-key [C-mouse-5] 'up-a-lot)

"Font-locking" is the emacs way of higlighting keywords based on the major mode of the file you're editing: there are modes for most languages including the C family, various fortrans, scripting languages like perl, python, and the various shells, text-processing modes like LaTeX, and so on. You turn on keyword highlighting with:

(global-font-lock-mode t)		; use font-lock to highlight all modes

flyspell-mode is an on-the-fly spell-checker, which will catch and highlight words it thinks you've misspelled, as you're typing. It's pretty useful and not too obtrusive or intrusive.

(require 'flyspell-mode nil t)          ; won't fail if no flyspell

turns this on, if it exists on the system. You can then choose when flyspell-mode is invoked. For example,

(add-hook 'text-mode-hook '(lambda ()
 			     (flyspell-mode t)
			     (auto-fill-mode t)))

turns it on for plain text mode. (auto-fill-mode is another minor mode, which automatically adds linefeeds while you type continuously, and always at a word boundary. You can also fill paragraphs by hand, by hitting M-q while the cursor is inside the paragraph you want formatted).

eshell-mode is a powerful interactive shell, invoked with M-x eshell. Among the useful settings for eshell mode:

(setq eshell-rm-interactive-query t)    ; like rm -i

woman is a nice mode for looking at manpages. Set it up with:

(autoload 'woman "woman" "Decode and browse a UN*X man page." t)
(autoload 'woman-find-file "woman"
  "Find, decode and browse a specific UN*X man-page file." t)

then, when you want to look at a manpage, type M-x woman.

I used to use ange-ftp for looking at remote files (which were transparently fetched by ftp). As ftp becomes obsolescent, it's been replaced by tramp, which does the same using ssh and scp.

This sets up tramp, and also sets up emacs to beep when it has finished importing or exporting a remote file.

; tramp
(require 'tramp nil t)			; won't fail if tramp missing
;    (setq tramp-debug-buffer t)
(setq tramp-verbose 0)			; set to 10 when debugging
(setq tramp-default-method "scp")
(setq tramp-auto-save-directory "/tmp")
(defadvice tramp-handle-write-region (after make-tramp-beep activate)
  " beep after tramping."
  (interactive)
  (beep))
(defadvice tramp-handle-do-copy-or-rename-file (after make-tramp-beep activate)
  " beep after tramping."
  (interactive)
  (beep))
(defadvice tramp-handle-insert-file-contents (after make-tramp-beep activate)
  " beep after tramping."
  (interactive)
  (beep))

With tramp correctly installed, user foo can edit files on the remote machine bar.edu, where she has ssh access, as follows:

C-x C-f /foo@bar.edu:/a/b/c

and it will be as though she were editing the file /a/b/c locally. All the normal keybindings will work, e.g C-x C-s will save the file to the remote machine transferring it via scp. With a properly configured ssh connection (using keypairs) she'll never even type any passwords.


Using emacs tags to navigate a source tree

Modern editors such as emacs and vi have many features built upon some ability to parse the syntax of the file that's being edited. For instance, syntax highlighting of source code is a great editing aid. Another feature of syntax-sensitive editors is to enable the traversal of a complex source tree. This is done by generating a set of tags that bookmark key spots in the code, such as the first line of every procedure. Thus if you find call foo() while editing Fortran source, and would like to examine subroutine foo but have no idea which source file it's found in, the editor would use its tag or bookmark for foo to open the right source file and take you there.

Tags for emacs are generated by running etags <list of source files>. This creates a file called TAGS containing a complete list of cross-references and bookmarks across the entire source tree in the list you gave it. (The equivalent feature in vi is a file called tags (lowercase) which is generated by running ctags <list of source files>. I'm told that other editors like nedit also understand vi's tags file, but can't vouch for it.)

If you used mkmf to generate your Makefile, you can just type make TAGS to create the TAGS file: mkmf is tags-aware. (Similarly, make tags for vi tags.)

Once you have a valid TAGS file, here's how you use it.

  • Position yourself (your cursor, that is...) on the word foo in call foo(). Then M-x find-tag (which is bound by default to M-.) will immediately place you on the first line of subroutine foo. That's all there is to it! To go back to where you came from, type M-*.
  • The first time you run find-tag, emacs asks you which TAGS file you want to use: just give it the path to it. You won't need to do this again unless you want to use a different TAGS file (in which case, type M-x visit-tags-table).
  • If you're looking for a string that isn't actually a routine name, you can still search for it across your entire source tree with M-x tags-search. While this tag search is in progress you can keep skipping to the next instance by typing M-, which is the shortcut for M-x tags-loop-continue.

There are many more tags-related emacs commands, but these are the basic ones, and the most useful. You'll never be cd'ing up and down your source tree again.

I'm not exactly sure how to achieve the same functionality in vi or nedit but I'm sure the same set of functions exists.

A good place to begin exploring these issues at GFDL might be at the vim wikipage. There also appears to be a mailing list for vim users at oar.gfdl.vim@noaa.gov.


The LaTeX Beamer class is a spectacular method for producing presentation material. Please don't use PowerPoint anymore!

I've structured my beamer example in the form of a presentation. Here's the associated LaTeX source, and you may also need to download this example movie.


emacs-muse-mode created by v. balaji (balaji@princeton.edu) in emacs using the emacs-muse mode.
last modified: 03 June 2007
this page visited: times
smaller bigger reset