Beginning Emacs


Starting Emacs

To start emacs, simply enter emacs in an xterm or on the command line if using a console. This will load the default emacs.

Debian Slink includes two versions of GNU Emacs - 19 and 20. To start emacs19 enter:

emacs19

Emacs Terms

Buffer: A buffer is a structure which contains a copy of the file or text that is being edited. When text in a buffer is saved in emacs, it writes the text to the file. Thus, emacs does not directly edit or change the file which is on disc, but makes changes to a copy of that file held in an emacs buffer. This has many advantages when editing text.

Mini-buffer: The mini-buffer is the line at the very bottom of the screen where emacs commands can be entered. This buffer can be opened by a keystroke such as M-x when a cursor will appear, or when the F10 key is pressed. Pressing F10 opens another buffer, and shows a list of options.

Mode Line: This is the line near the bottom of the screen (usually in reverse video) which gives information about the major and minor modes (see below) currently being used, the name of the file in the buffer, and the line number where the cursor is.

Major and Minor Modes: Emacs has several Major modes of operation. For instance, it can function as a text editor (text-mode), an html editor (html mode) and an editor for several computer languages (lisp mode; fortran mode; C mode and so on). A Minor mode can be understood as modifying the behaviour of a Major mode - that is, getting that mode to work in a certain custom way. Thus, the Minor mode auto-fill can be used with text mode to do what is commonly called a word-wrap: that is, to automatically wrap lines of text to fit the screen.

Window: A Window is used to view a buffer on screen. When you read text in a buffer, you are reading it in a window. Several windows can be open in emacs at the same time to view different buffers. Note that closing (deleting) a window does not close (kill) the buffer that was being viewed: the buffer is still open, and remains so until it or emacs is closed (killed).

Emacs Keys

Emacs requires you to use the Control (Ctrl) and Alt (or Meta) keys in combination with other keys. Thus to quit emacs:

C-x C-c

which means hold down Control key and x key at same time, and then the Control key and the c key. The Alt (Meta) key is symbolized by M.

General Key Sequences:

C-g    stop current command/action

C-x  C-d    list directories (default goes to user's home directory: ~/ )

C-x  C-f    open file (enter name for new file)
  note that just pressing the Return/Enter key   shows the directories and files in ~/)

C-x  C-s    save file in current buffer

C-x  C-w    save current file with new name

C-z    background (i.e. suspend) emacs and give a command prompt
  (note: to return to emacs, enter fg then Return)

C-x  k    kill buffer (will give a prompt asking which buffer)

C-x  0    delete this window

C-x  1    delete other window(s)

C-x  C-c    exit emacs

Cursor:

C-n    move to beginning of next line

C-a    move to start of line

C-e    move to end of line

C-p    move back to previous line

C-x  o    move to other open window

Scroll Page:

C-v    forward one screen

Alt-v   back one screen

Delete Text:

C-d    delete what is under cursor

Alt-d    delete word under cursor

Undo:

C-x  u    undo last action
  (Note: emacs has a multiple undo)

Multiple Windows:

C-x  1    delete other window(s)

C-x  0    delete this window

C-x  2    split window in two vertically

C-x  3    split window in two horizontally

C-M-v    scroll other window

C-x o    move cursor to other window

Copy and Yank:

Yank is the emacs term for moving selected text to a particular place (that is, to paste it).

Although the following may sound complicated when read, once the basic key strokes are learnt it is actually much easier than using the mouse in X (or any GUI) to copy and paste.

To copy a region of text and place it elsewhere the selected text is defined by a region:
1) Place cursor at start of text, then enter C-Shift  @   which will set the mark
2) move cursor to end of selected text; this defines the region: the text between the mark and the point (cursor)
3) C-w   will kill the selected text (copy it to the kill file)
4) C-y   will copy text back to original position
5) Move cursor to desired position for insertion of selected text
6) C-y   will copy text from kill file to insertion point marked by cursor.

To copy a word:

1) Put cursor over word, then M-d
2) C-y   to kill word and copy to kill file
3) C-y   to return word
4) Move cursor to required position
5) Yank word from kill file

To copy a line:

As above but use C-k

To copy a sentence:

As above but use M-k

.emacs file:

This resides in the user's home directory and in written in lisp. It enables the user to customize emacs. Here is a very simple .emacs file for emacs19 (note that in lisp comments begin with ; ).

;; -*- lisp -*-
;;disable menu bar
(menu-bar-mode nil)

;;disable scroll bar
(toggle-scroll-bar nil)

;;use F3 key to kill buffer
(global-set-key [F3] 'kill-this-buffer)

;;set text mode as default mode
(setq default-major-mode 'text-mode)

;;set cursor to line (only in X):
;(setq initial-frame-alist '((cursor-type . bar)))

;;set width of bar, in pixels (only in X)
;;(setq initial-frame-alist '((cursor-type . (bar . 3))))

;;always auto-fill in text mode.
(add-hook 'text-mode-hook 'turn-on-auto-fill)

;; Turn off the bell
;;(setq visible-bell t)

;; next line loads custom .el files from ~/emacs
;;(load "~/emacs/file")

;;set custom colors - but only works in X not on console
(setq default-frame-alist
     (append default-frame-alist
     '((foreground-color . "green")
     (background-color . "black")
        ;;(cursor-color . "red"))))
     (cursor-color . "blue"))))

Setting Modes:

To set a Major Mode - for example html mode - enter M-x followed by name-mode. Thus, for html mode:

M-x    html-mode

To set a Minor Mode, for example, auto-fill:

M-x    auto-fill-mode


Copyleft 1999 by Space-Time Systems. This document is free; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the license, or (at your option) any later version.


This document is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details.
Should you be lacking a copy of this, look at www.gnu.org/copyleft/gpl.html.

This document was written on the console (Debian GNU/Linux 2.1) using emacs19, with html generated using the texi2html translator version 1.54 and final edit done with emacs19 via lynx.