Wednesday, February 25, 2004

Emacs & XEmacs Guide

GNU Emacs is a free, portable, self-documenting, customizable, extensible and real-time text editor, Emacs was originally written by Richard Stallman, the founder of the GNU Project, and James Gosling, the creator of the Java language. XEmacs has evolved from the original Emacs, with more GUI and content-based formatting support. For the user-friendly interface reason, I stick to Xemacs all the time.

I found Xemacs was not convenient to use at the beginning, since I was used to the vi and it's shortcuts. However, when I became more experienced I found Xemacs much powerful than vi in most aspects. (X)Emacs is the monster of all editors! It is more than just a text editor. It is a huge and complete system for development, communications, file management, and things you wouldn't even imagine. I only know a very limit features of Xemacs.

Vi (vim) and Emacs (Xemacs) have a very different look and feel, but that's not a real reason for choosing one over the other. Vi is much smaller and loads much faster, while Xemacs is a powerful tool for complex files and software development. They were designed for different jobs, they are better at different things, and I use both of them depending on the job.

Xemacs automatically saves the files you are editing by default. The name of the autosave file is the same as the name of the file you are editing, with a sharp (#) added to the beginning and to the end. When you do a file save, Xemacs creates a backup file of name with a tilde (~) added to the end. When Xemacs starts up, it reads the file ~/.emacs or ~/.xemacs/init.el to customize the Xemacs. You could modify this file by your favorite with Lisp format, e.g. if you like your Xemacs working as a pure txt editor, add the following in the .emacs file:

(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
(setq-default transient-mark-mode t)

There are several basic concepts (environment) of Xemacs: file, buffer, window, frame, echo area (mini-buffer) and mode line. When you are editing a file on disk, Xemacs read a copy of it to initialize a buffer and write a copy of a buffer out to a file to save it; a buffer is the basic editing unit hoding the text you actually edit. You can have multiple buffers but can only edit one buffer at a time; A window is the view of a buffer while frame is the whole screen of Xemacs with all buttons at the top. You could loosely call frames as windows; the echo area is the area at the very bottom of the XEmacs screen and is used to interact with you; the mode line is above the echo area on the XEmacs screen, showing the status and what is happening in the current window.

Some tips about Xemacs:

  • without X server, "Xemacs -nw" (no window mode) will open in the terminal, "Ctrl-z" switch back to the shell and type "fg" to go back Xemacs
  • command-handling "M-x" or "Alt-x" is not available in some cases, use "Esc x" instead
  • using tab or hitting space half will try to complete the command automatically
  • "M-x indent-buffer (indent-region)" will indent the buffer (region) contents
  • "M-x set-variable RET c-basic-offset RET " can set the indent value
  • a simple .emacs file with c/c++ style setting and my simpler .emacs file
  • remote access by Ange-ftp & EFS (Emacs File System):
    Ctrl-x d RET /huang@gate.csd.ca:source/mpi.cc RET
    Ctrl-x d RET /gaul.csd.uwo.ca: RET
  • remote access by Tramp (Transparent Remote Access, Multiprotocols):
    C-x d RET /[huang@gate.csd.ca]/csd/grad/huang RET
    C-x d RET /[ssh/greatwhite.sharcnet.ca]/ RET

You can do most jobs from the menus of Xemacs. But the more efficient way for editing is using shortcuts. Below are some most commonly used commands or keystrokes:



























































































































































































































Mode




Keystroke & command




Description




basic




Ctrl-x Ctrl-f




open a file




Ctrl-x d




DirEditting mode, identical to Alt-x dired




Ctrl-x Ctrl-c




quit a file and the Xemacs




Ctrl-x Ctrl-s




save a file




Ctrl-x Ctrl-w




write to a new file




Ctrl-x Ctrl-h




get help




Ctrl-h f (i, t, k, v)




get help for functions (info, tutorial, key variables, etc..)




Alt-x speedbar




pop up a speedbar




Alt-x shell (term)




switch to a shell (terminal)




buffer & window




Ctrl-x Ctrl-b




list buffers




Ctrl-x o




switch to a buffer




Ctrl-x k




kill a buffer




Ctrl-x 3




split the window vertically




Ctrl-x 2




split the window horizontally




Ctrl-x 1




make into one window




motion




Ctrl-v




Page down




Alt-v




Page up




Ctrl-p (n)




move to Previous (Next) line




Ctrl-a (e)




move to the start (end) of the line




Ctrl-f (b)




move Forward (Backward) one character




Alt-<(>)




move to the beginning (end) of the frame




Alt-x goto-line




go to certain line




editing




Ctrl-g




abort (quit) current command




Ctrl-x u




undo




Ctrl-_




more convenient undo




Ctrl-Space (Ctrl-@)




set a mark




Ctrl-x h




select the whole frame




Ctrl-w




cut the selected area




Esc w




copy to where the mark ends




Ctrl-y




yank (paste) back whatever you copied




Ctrl-d




kill one character




Ctrl-k




kill to the end of the line




Ctrl-a Ctrl-k Ctrl-k




kill an entire line




Ctrl-u 3 Ctrl-k (d)




kill the next three lines (characters)




search & replace




Ctrl-s




search




Ctrl-r




search backwards




Esc %




begin a search and replace




!




replace all occurrences at once




Space




replace




n




skip the replace




Esc




stop the search and replace




Esc Ctrl-c




continue with the search and replace




mode




Alt-x text-mode




text-mode




Alt-x c-mode


(c++, java, tex...)




programming mode




Alt-x makefile-mode Enter




makefile mode




Alt-x overwrite-mode Enter




overwrite mode




Alt-x font-lock-mode




activate color highlighting




Alt-x auto-fill-mode




auto wrapping




Alt-x auto-save-mode




auto save




Alt-x blink-cursor-mode




blinking cursor