Emacs

From Wsms

Jump to: navigation, search

Note: This page should be titled emacs (all lowercase). It is Emacs due to technical limitations of Mediawiki.

emacs is a text editor.

Contents

common usage

Start emacs in a console, instead of a new X window:

[ggeller@ws05 ~]$ emacs -nw

Tips

To run within a console window, rather than opening a new X window use the -nw option:

[ggeller@arthur Desktop]$ emacs -nw notes

Advanced command:
Esc-M query-replace-regex
The regex's in emacs are sort of like the ones in perl. So, ^./ would match ./ at the beginning of a line, like you usually get with a find command.

major modes

To use a particular major mode, just type its name, like CPerl, in the emacs command area.

To specify a major mode, make the first line of your file something like this:

-*- mode: outline -*-

.emacs

Specify your own options in the file ~/.emacs. This is George's .emacs file as of November 2007:

;; .emacs

;;; uncomment this line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)

;; turn on font-lock mode
(when (fboundp 'global-font-lock-mode)
  (global-font-lock-mode t))

;; enable visual feedback on selections
;(setq transient-mark-mode t)

;; default to better frame titles
(setq frame-title-format
      (concat  "%b - emacs@" (system-name)))

;; default to unified diffs
(setq diff-switches "-u")

;; always end a file with a newline
;(setq require-final-newline 'query)

;; GGG
(setq-default scroll-step 1)
(setq auto-mode-alist (cons '("\\.pl\\'" . cperl-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.pm\\'" . cperl-mode) auto-mode-alist))

;; GGG 20070526 from http://perso.tls.cena.fr/boubaker/distrib/mode-compile.el
(setq load-path (append load-path (list "~ggeller/.emacs.d/lisp")))
(autoload 'mode-compile "mode-compile" "Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile" "Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)

;; GGG 20070611 from http://en.wikipedia.org/wiki/Wikipedia:Wikipedia-mode.el
(autoload 'wikipedia-mode "wikipedia-mode.el" "Major mode for editing documents in Wikipedia markup." t)
(add-to-list 'auto-mode-alist '("\\.wiki\\'" . wikipedia-mode))
(add-to-list 'auto-mode-alist '("en\\.wikipedia\\.org" . wikipedia-mode))
(add-to-list 'auto-mode-alist '("mozex.\\.*" . wikipedia-mode))
(autoload 'longlines-mode "longlines.el" "Minor mode for editing long lines." t)

;; GGG 20070618 Kill the startup message.                                          
(setq inhibit-startup-message t)

;; GGG 20071024                                                                 
(autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
(setq html-helper-do-write-file-hooks t)

(setq html-helper-do-write-file-hooks t)
(setq html-helper-build-new-buffer t)
(setq html-helper-address-string
  "<a href=\"http://sdlinuxguy.com\">George Geller <george_geller@speakeasy.\
net></a>")

;; GGG 20071111                                                                 
(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))

;; Org-mode settings 20071210
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-font-lock-mode 1)

;; 20071215
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))

Set the font size

M-x set-face-font RET default RET tab

Then you get a list of possible completions. Use Ctrl-x, Ctrl-o to navigate to the pane with the choices. Position the cursor on your new choice and hit enter:
Image:20070501-010-emacs-set-font.png
See http://xemacs.org/Documentation/21.5/html/xemacs-faq_5.html#SEC159 for how to set the font at startup.

ispell and recursive edit

You can spell check your document with M-x ispell. During the ispell you can escape to do editing with C-r. Use ESC C-c C-c to return to your ispell session.

using run in the CPerl mode

When use use the CPerl mode, you get a menu that looks like this. Note that the Run item under Perl is greyed out:
Image:20070526-020-emacs-cperl-menu.png
If you look at http://www.emacswiki.org/cgi-bin/wiki/CPerlMode it says:

To be able to use the grayed out commands (run etc…) you must install ModeCompile.

I went to http://www.emacswiki.org/cgi-bin/wiki/ModeCompile, where it says to get mode-compile.el from http://www.tls.cena.fr/~boubaker/Emacs/. I downloaded http://perso.tls.cena.fr/~boubaker/distrib/mode-compile.el and put it in ~/.emacs.d/lisp Then I added the following to .emacs:

(setq load-path (append load-path (list "~ggeller/.emacs.d/lisp")))
(autoload 'mode-compile "mode-compile" "Command to compile current buffer file \based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile" "Command to kill a compilation laun\ched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)

The next step was to visit mode-compile.el in emacs and byte compile it with ESC x bite-compile-file RETURN. Then I quit emacs, a relaunched it with a perl script. On executing C-c c I got to see:
Image:20070526-030-emacs-perl-run.png

save a shell session

Use Alt-x shell to start the shell session. When you want to save it use C-x C-w and specify a filename. See: http://www.debian.org/doc/manuals/reference/ch-tips.en.html

disable welcome screen

Starting with emacs-22 the default behavior is to show a welcome message when starting emacs:
Image:20070618-010-emacs-welcome.png
The welcome screen is OK for beginners. When you get tired of it, disable it with this in your .emacs file:

(setq inhibit-startup-message t)

modes

wikipedia mode

See http://en.wikipedia.org/wiki/Wikipedia:Wikipedia-mode.el The setup is pretty complicated. You download the mozex firefox addon from http://mozex.mozdev.org/ The lines I added to ~/.emacs were:

;; GGG 20070611 from http://en.wikipedia.org/wiki/Wikipedia:Wikipedia-mode.el
(autoload 'wikipedia-mode "wikipedia-mode.el" "Major mode for editing documents in Wikipedia markup." t)
(add-to-list 'auto-mode-alist '("\\.wiki\\'" . wikipedia-mode))
(add-to-list 'auto-mode-alist '("en\\.wikipedia\\.org" . wikipedia-mode))
(add-to-list 'auto-mode-alist '("mozex.\\.*" . wikipedia-mode))
(autoload 'longlines-mode "longlines.el" "Minor mode for editing long lines." t)

I also saved wikipedia-mode.el and longlines.el to ~/.emacs.d/lisp, installed the development version of mozex from mozex.mozdev.org, and configured it from Tools->Add-ons like this:
Image:20070611-030-firefox-mozex-textarea-config.png
After all this, here is the payoff. Edit a wiki page in Firefox. Type Ctrl-9 in the text area, and Emacs magically pops open:
Image:20070611-050-firefox-emacs-external-editor.png

Unfortunately mozex does work correctly if you are connected using nx. emacs opens, but it doesn't find your tmp file. Instead you get an error of: "Wrong type argument: stringp, nil".

html-helper-mode mode

See http://www.santafe.edu/~nelson/tools/

Download http://www.santafe.edu/~nelson/tools/html-helper-mode.el

ggeller@harrison:~/Desktop/tmp$ mv html-helper-mode.el /home/ggeller/.emacs.d/lisp/

Add to ~/.emacs

  (autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
  (setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))

python mode

Get the emacs python download from http://sourceforge.net/projects/python-mode/ Put the python-mode.el in ~/.emacs.d/lisp/ and make the changes shown above to .emacs.

org-mode

The December 2007 issue of Linux Journal has a good article about it. It is not obvious how to make tables work as advertised. One trick is to use := in the cell's formula.

Here is an example of a table with a valid formula:

| Column 1   | Column 2   | Column 3   | Numbers |
|------------+------------+------------+---------|
| Cell one   | Cell two   | Cell three |     121 |
| Cell four  | Cell five  | Cell six   |     200 |
| Cell seven | Cell eight | Cell nine  |    3002 |
|------------+------------+------------+---------|
|            |            |            |    3323 |
#+TBLFM: @5$4=vsum(@2..@4)

To get that, you have to type :=vsum(@2..@4) into the cell getting the sum.

There is a nice article on how to use emacs org mode for David Allen's Getting Things Done method: members.optusnet.com.au/~charles57/GTD/orgmode.html

see also

The #xemacs and #emacs channels on IRC at Freenode.net
Writing "Learning PHP 5" by David Sklar http://www.onlamp.com/lpt/a/5400
PHP Code Generation with Elisp by Zachary Kessin http://www.onlamp.com/pub/a/php/2005/11/23/php_elisp.html
Emacs/Howto_edit_po_files
Emacs/Outline_mode
zile a light-weight editor with emacs key bindings
e3em a light-weight editor with emacs key bindings included in puppy linux.
http://www.linuxjournal.com/article/6242 - Getting Started with Emacs by Charles Curley
http://edward.oconnor.cx/elisp/ from "Ted" at barcampsd 2007
gconf-editor Set the gtk_key_theme to Emacs so that applications like Firefox use emacs key bindings

Personal tools