Introduction to EMACS and XEMACS CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes. Emacs Background Emacs is a powerful and extensible text editor. Emacs originally was an acronym for Editor MACroS. The first Emacs was a set of macros written in 1976 at MIT by Richard M Stallman. Emacs has become a standard editor used by programmers worldwide. There are several varieties of Emacs, by typing emacs at the command prompt, you are throw into one of two modes: The terminal-mode (from an ssh connection or without X Window): This mode has no toolbars, and all commands are given via keyboard. Do not think that this is a useless mode, as sometimes the only connection that you may have to a machine is remote. The graphical-mode (requires X Window): It looks similar to the terminal-mode, but with some added features for X. There is a toolbar across the top of the window, from which many common tasks can be accessed. You may wish to type emacs& (note the &), as this will free up the shell for other tasks (such as compiling). XEmacs This version of Emacs that has many enhancements specifically for X Window and should be launched locally. There is a graphical toolbar (like MS Word) for basic options as well as a text driven toolbar for other options. To invoke XEmacs just type xemacs. If you do not want to tie up the shell, use xemacs&. Creating and Opening Files To create a file, type the name of the file that you wish to create after the emacs command. Example: emacs foo.c & or xemacs foo.c & (& optional). To open a file, type the name after the command, just as above. Tutorial and References Emacs comes with a built in tutorial. To access it type F1 then T. The tutorial covers many of the basic tasks, such as cursor movement to more advanced skills such as searching and replacing text. References - from basic commands to the full manual A BASIC EMACS Reference Card A much more EXTENSIVE EMACS Reference Card The Complete Manual Online - at GNU.org XEmacs XEmacs is a graphical variant of Emacs. It too is freely available for download (http://www.xemacs.org/) and is also licensed under the GPL license (http://www.gnu.org/licenses/licenses.html#GPL). XEmacs for Windows For those of you who use Windows, that there are MS Windows ports of XEmacs. XEmacs Commands Mostly all of the Emacs commands are available to you under XEmacs One useful XEmacs command: Alt-G - Goto Line number .emacs Configuration File When Emacs (or XEmacs) begins, it looks for and reads a file named .emacs in your home directory to obtain configuration information. Save this .emacs file (note the leading "dot") in your UNIX home directory to get the following features (you will need to restart emacs to invoke these options): Automatic formatting - in accordance with coding style standards of many CMSC classes - (ALWAYS check with your instructor as to specific standards) A working backspace key - backspace will now delete to the left of the cursor instead of invoking "help". A more logical help key - It remaps CTRL-X? to be the new "help" key. Customizing Emacs The .emacs file contains user specific information and customizations to the XEmacs/Emacs editors. You can set up this file to do all sorts of customizations from building additional menu bars, to highlighting, to utility functionality. There are 2 ways of going about customizing XEmacs/Emacs: Manually edit this configuration file - this requires some knowledge of the language used to configure the editor and requires the user to manually edit the ".emacs" file. Save configuration changes from within the editor - or you can save the changes from within the editor itself, and it writes out settings automatically to the ".emacs" file. I find that I tend to use a mixture of both manual and automatic editing of the ".emacs" file. Sample CMSC 201 .emacs File (custom-set-faces) (global-set-key "\C-h" 'delete-backward-char) (global-set-key "\C-x?" 'help-command) ;; set style to "Ellemtel" for C++ (add-hook 'c++-mode-hook '(lambda () (c-set-style "ellemtel"))) ;; set style to "Ellemtel" for C (add-hook 'c-mode-hook '(lambda () (c-set-style "ellemtel"))) ;; special effects for 'C‘ (add-hook 'c-mode-hook 'turn-on-font-lock) Another .emacs File ;; no tabs please (setq-default indent-tabs-mode nil) (setq default-tab-width 4) ;; line & col nums (setq line-number-mode t) (setq column-number-mode t) ;; font coloring (setq font-lock-auto-fontify t) (setq font-lock-mode-maximum-decoration t) (require 'font-lock) ;; my email (custom-set-variables '(user-mail-address "your_username_here@umbc.edu" t) '(query-user-mail-address nil)) ;; some key bindings (global-set-key [(f1)] (lambda () (interactive) (manual-entry (current-word)))) (global-set-key [f2] 'ispell-word) ;; map that mouse wheel (global-set-key [button4] 'scroll-down) (global-set-key [button5] 'scroll-up) ;; automatic indentation upon return (global-set-key [(return)] 'newline-andindent) ;; stop beeping (setq visible-bell 1) ;; make sure that every file has a newline (setq require-final-newline t) Emacs Modes C Mode: Emacs supports C programming with a variety of options in C mode. C -> Comment Out Region C -> Uncomment Region C -> Indent Line or Region C -> Foreword Statement C -> Backward Statement C++ Mode: C++ mode also supports the same functions as under C mode, but under the C++ menu. HTML Mode Lisp Mode Compiling in XEmacs One of the nice features of XEmacs is the ability to compile source code from within it. Do the following to compile code from within XEmacs: Click on the Compile button Edit the prompt in the window that appears that shows you what the current command is to compile. The Compilation Output – this is where XEmacs is really useful. By default XEmacs suggests the command "make -k". This may or may not be what you want. If you do not have a makefile (or don't know what one is) then you can change the command to something like "gcc -Wall -ansi *.c" by clicking on "Edit command". If you have errors, gcc (for example) will report file and line numbers that the errors occurred on. XEmacs goes a step further and hyperlinks that error containing the file and line number. So when you click on it, it will open that file in the current buffer and jump to that line number. XEmacs will also make sure that you saved all open files, to avoid compiling the sources without compiling them first.