Advice from Emacs wizards


.Profile

If you use zsh, bash, ksh, or similar shell, you can edit your history of commands with: set -o emacs
I used to have the following in my unix $HOME/.profile. You may have to put this in a .zshrc | .bashrc | .kshrc, as Emacs starts an interactive shell. EMacro now supports ansi-color.el, which allows colorized ls inside M-x shell. However, you may freely use this sample to disable or enable functionality, only when your terminal is an Emacs term. See also Shell Commands M-x shell brings up a shell inside Emacs. Control Up Arrow allows you to recall previous commands.


#-*-Shell-Script-*- #Hint for emacs to open file in shell script mode
#use source, setenv for c shell
EDITOR=emacs		#Make Emacs your default editor
export EDITOR

#Bourne shell (sh) does not support alias
#Redefine ls for emacs, which cannot handle ansi color escapes
#Emacs sets the $EMACS environment variable for `M-x shell`
#This fancy notation sets $EMACS to "f", if it is unset:
if [ ${EMACS:=f} = t ]; then; alias ls='/bin/ls -ACF'	#for emacs

#otherwise, we exploit our fully functional terminal:
else;	alias ls='/usr/local/gnu/bin/ls -ACF --color'; fi
export ls

Align On Tabs

You can align text on a character, such as a tab. First, mark (i.e. select) your region to be aligned. You can drag your mouse over the region accomplish this. Next,
M-x
align-regexp
C-q TAB

Here is an example:

Emacs		$0.00
Word		$199.99
Oracle Studio	$30,000.00

Appointments

You can add quick appointments in Emacs with:

(appt-add "13:00" "Dentist")

Note that this appointment will be lost, when Emacs is closed. Send me an email, if you are interested in an easy menu interface, for appointments.


Apropos Regular Expressions

You can use regular expressions to search help. If you want to learn how to stop duplicated commands in Emacs shell history, you would say, "Search for shell OR comint, followed by dup" or, in Emacs speak:

M-x apropos RET \(shell\|comint\).*dup RET


Big Brother Database

Loading EMacro's inet.el enables BBDB. You can activate it in a supported mode, such as GNUS (Internet Emailer and Usenet newsreader) by hitting ":", when the cursor is near an address. This adds the address to the BB DataBase, and brings it up in a buffer at bottom, where a right mouse click brings up a popup menu.


Cache of EMacro

locate-library() is one of the slowest commands in elisp. EMacro optimizes this command, by saving its results in ~/emacs/preferences/e-cache.el (or e-xcache.el for XEmacs). This also has the side effect, that when you've installed http://tiny-tools.sourceforge.net or browse-url.el, and conditions work right, that you can click on the links to download these packages.

There are far more packages in [X]Emacs, than the EMacro team can support, but all the supported libraries, not bundled with [X]Emacs, are listed in the cache[s]. EMacro currently does not automatically refresh the cache, so you must delete them, when you change supported packages in your load-path. This is what happens, when you run ~/emacs/bin/[os]/e-refresh. EMacro will automatically repopulate any missing items in the cache.


Closure Programming

Here is a demo of closure, where we define a print function. Converse to Object Oriented Programming, which puts functions inside data structures, closure puts data structures inside functions, in this simple case, a string of chars

(defun make-print-function (str)
  `(lambda ()
     (print ,str)))


;;Alternatively:
;;The advantage of this version is that the lambda can be byte compiled.
(require 'cl)

(defun make-print-function (str)
  (lexical-let ((str str))
    (lambda () 
      (print str))))

Code & Word Completion / Intellisense

Emacs lets you hit TAB, when in the minibuffer, to complete command or file names. This technology can also be used inside the editor. Type a partial word, then S-C-/ (Control ?), and EMacro uses dabbrev to search the buffer to complete the word.
See also resources.html for JDEE & CSDE, which provide this in their Java & C# development environments.
See http://cedet.sourceforge.net/intellisense.shtml for a tutorial on Semantic
See also resources.html for flyspell.el and flymake.el


Completion Undo

If you lose your place when opening a file with C-x f Find File, by hitting // or TAB, you can always hit Undo C-x u.


Controlling Commands


Configuring Emacro

Before you make changes to EMacro, you should read INSTALL.txt's section on upgrading. Rather than simply uncommenting code, copy your uncommented code to postload.el, (or a file you call from it). This file is meant to be modified.


CUA.el and Clashing Commands

Say you are running cua-mode, and you wish to use the command shortcut for M-x comment-region. First, you mark the region by dragging the mouse, or shift-arrow. You type C-c (Control C) and then C-o. To your surprise, it copies text to the clipboard, then tries to open a file. The trick is to execute these commands rapidly together: C-c C-o. If you are in a programming mode (modeline at bottom says c-mode or emacs-lisp-mode, etc), then the selected region will be commented out.


Disk Diet For A Smaller Emacs

You probably shouldn't use Emacs (nor XEmacs, nor EMacro) if you are starved for disk space. Your best bet is to use an Emacs clone, such as Jed or Jove. See also the Unix man page for strip. Following are some Emacs files that are not critical. We will use the word move, to free these files. Contact your system administrator, as to whether you should move them to another partion or system (and link), compress, or, if needed, delete. "~emacs" refers to the main emacs directory. "~lisp" means the main distibution of Emacs lisp files
Proceed with CAUTION! We are not responsible for this advice! Further, this advice has not been tested, and could lead to Emacs misbehaving!


Edit Unusual Files

Using the variable file-name-handler-alist, you can execute arbitrary code when opening a file. This is used by dired and ange-ftp / efs. You can adapt this to other things, such as seamlessly editing text stored in a database, where (X)Emacs silently performs the select/update.


Elisp Coding

You can always hit C-x e after a closing parenthesis, to execute the preceding lisp function. GNU Emacs also supports interactive interpretation: try M-x ielm

Once you've written code into a *.el elisp file, you can check your work with the following tools:


Find Functions & Variables

You can type C-h v, or C-h f then tab, to have the complete list of possible variable or function names, respectively. Then click in the *completion* buffer (that should have appeared) and do an i-search C-s using for your keyword. This also lets you inspect what values are currently set. C-h a is also useful here.


Indent All C++ Files

This shows how to run Emacs in batch mode against several files. See also ~/emacs/bin/unix/e-batchcompile for another example.

for i in *.[ch]*		#.cpp, .h, .hxx, ...
do
    fcommand="$fcommand --find-file $i"
done

emacs -nw --batch --load ~/tabify-c++-buffers.el $fcommand --execute '(progn (tabify-all-c++-buffers) (kill-emacs 0))'

Gdb Debugger Tutorial

From: Zhongtao Zhu
(1) Write the hello.c program and compile it with the -g option.
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
#include <stdio.h>
main()
{
printf("Hello, world.\n");
}

=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

Now you have the executable hello in your working directory.
(2) Load the hello.c source code in a buffer and vertically split the window by C-x 2.
(3) Execute gdb by M-x gdb and in the mini-buffer there should be a line gdb --fullname hello displayed, you just press ENTER for it. Now a new *gud-hello* buffer is shown and ready for use.
(4) Switch to the source code buffer and move cursor to the line of printf(...), then set a breakpoint by C-x SPACE at the line.
(5) Switch back to the *gud-hello* buffer and input run ENTER, the program hello will run and stop in the breakpoint you set. For this example, in the source code buffer you will find a => mark pointing to the printf(...) line.

=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
#include <stdio.h>

main()
{
=> printf("Hello, world.\n");
}

=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

(6) In the *gud-hello* buffer, C-h m will show you all the commands of interest.


ISpell

If you're running NT Emacs and want to use the Cygwin port of ispell[1] together with the German and Swiss dictionaries by Bjoern Jacke (he got them from Heinz Knutzen), here is the info you need.
The key is to get the binary distribution and install it, then get the source distribution, add #define MASKBITS 64 to local.h and run make programs. If the other targets fail, you'll need the binary distribution as well. Copy the programs into whatever directory you keep the stuff from the binary distribution.
Now you can get the German and Swiss dictionaries by Bjoern and run make there (no problem as soon as you have the MASKBITS stuff), and copy the .hash and .aff files into wherever the binary distribution puts them.
Into you .emacs, put the following lisp code. It is copied from the deutsch8 entry. If you want other command line options and word boundaries, just take another entry from ispell-dictionary-alist as a template.

(add-to-list 'ispell-dictionary-alist
	     '("german"
	       "[a-zA-Z\304\326\334\344\366\337\374]"
	       "[^a-zA-Z\304\326\334\344\366\337\374]"
	       "[']" t ("-C" "-d" "german") "~latin1" iso-8859-1))
(add-to-list 'ispell-dictionary-alist
	     '("swiss"
	       "[a-zA-Z\304\326\334\344\366\337\374]"
	       "[^a-zA-Z\304\326\334\344\366\337\374]"
	       "[']" t ("-C" "-d" "swiss") "~latin1" iso-8859-1))

For the Portuguese dictionary for ispell from http://fmg-www.cs.ucla.edu/geoff/ispell-dictionaries.html you may need to add the following entry to ispell-dictionary-alist:

(add-to-list 'ispell-dictionary-alist
	     '("portugues"
	       "[a-zA-Z\301\302\311\323\340\341\342\351\352\355\363\343\372]"
	       "[^a-zA-Z\301\302\311\323\340\341\342\351\352\355\363\343\372]"
	       "[']" t ("-C" "-d" "portugues") "~latin1" iso-8859-1))

Japanese

If you want to display Japanese fonts, you should have MULE & LEIM support compiled in to Emacs. It may help to look at japanese.el & canna.el. You will also need to have Japanese fonts installed.
You can put -*- coding: foo; -*- in the first line of the file (into a LaTeX comment, for instance), for the right value of foo.
You can use file-coding-system-alist to tell Emacs about the right coding system based on the file name.


Jump Back

You can jump back to your previous cursor position, by hitting C-u C-SPC


KDE Colors

Edit $KDEDIR/apps/kdisplay/app-defaults/Emacs.ad


Keyboard Performance

See "AutoRepeat" in XF86Config file, if you use XFree86 (Linux). See also resources.html


Killing Buffers

You can select and remove several buffers with C-x C-b to show list, then cursor to each, and type "d" to delete. Hit "x" to exit.

You can lock a buffer, so that it cannot be killed, by

(require 'emacs-lock)   ;;add to e-postload.el (or .emacs)

M-x toggle-emacs-lock


MAC OS Classic (v7 - v9)

You may see a dialog comes up saying that some settings have to be initialized the first time emacs is started, and that emacs may have to be restarted. If you see this again on the restart, set Preferences... -> Cross Platform -> Convert text files to Macintosh format to Never. Note that EMacro Help only supports Netscape browser. If you are are good at Applescript, you can help the EMacro project improve this!


Open New Window

To see 2 windows in your emacs editor, try C-x 4 b. Also see the split-window-recent() function in functions.el


Oracle

You can get query results in emacs sql-mode on one line with 'set linesize 9999'. See also auto-fill-mode & hscroll to adjust line wrapping.


Other Configuration Files

Note that ~ = $HOME, the directory in your HOME environment variable.


Performance Improvement

You can improve performance, by setting fonts in .Xdefaults, instead of in Emacs. To change .Xdefaults, i.e. your fonts, with restarting Emacs, you would run
xrdb [-merge] $HOME/.Xdefaults
EMacro sets fonts using Emacs, for portability, so this performance trick would require code changes.


Permissions: Su For Higher Authority

You can use tramp.el, or possibly Ange-ftp (Emacs) or EFS (XEmacs) to edit files with another user's permissions. This may require the FTP service to be running. Simply open a file using this syntax:
C-x f
/su:root@localhost:/etc/hosts
or, more simply,
/su::/etc/hosts
You can also try this older syntax:
C-x f
/root@localhost:/dir/filename
Or, for XEmacs: C-x f
/[su/root@localhost]/dir/filename


Remote Printing

Windows users can simply give a UNC name to their printer, e.g. \\remote\hp6. Windows users might also wish to use "print", when EMacro prompts the first time for the print command. This is saved in preferences.el as
(setq lpr-command "print")
If your Linux system administrator hasn't set up /dev/rp0, you might be able to EMacro's first time prompt with something like
rsh server lp -d hp-printer
This is saved in preferences.el as
(setq lpr-command "ssh")
(setq lpr-switches '("print_server" "lp" "-d" "printer_name"))


Repeat Commands

vi users who love the . command, which repeats commands, should enjoy all the options in Emacs. The closest match is M-x repeat, which is bound to C-x z. Just hit z to keep repeating. See also M-x repeat-complex-command as well as C-u


Replace Text In A Subdirectory

If you want to search & replace "foo" with "bar" in all your C files (which have names like "file.c" or "file.h"):


.screenrc Sample

From Daniel Jensen

I have not tested this

markkeys "0=^A:\$=^E:h=^B:j=^N:k=^P:l=^F"


Search String Editing

You can edit the search string, in the middle of performing an isearch, by typing C-^
You can even paste, using C-y
When you're ready to carry on searching, just type C-s (or C-r ) again.


Searching EmacsWiki

You need to escape special characters, so to search for c++, you would need to enter   c\+\+   in the HTML search box.


Shell Commands

See also .Profile

Emacs emulates many unix commands. Examples: M-x cd, M-x dir, M-x grep

The following shells support smart things like arrows & color, and may work better on versions of XEmacs or Emacs, older than v21.x. If EMacro detects ansi-color.el in the load-path, that will help.

Try them, if you have problems with the standard

In M-x shell, C-up gets you the previous command.

Also try C-u M-| (shell-command-on-region) which runs a command such as grep or sort, using the selected text

Here is an example of using Emacs in a shell script

#Indent all C++ files

for i in *.[ch]*   #.cpp, .h, .hxx, ...
do
fcommand="$fcommand --find-file $i"
done
#The above is simply `fcommand="**/*.[ch][px]*"` in zsh (and modern bash?)

emacs -nw --batch --load ~/tabify-c++-buffers.el $fcommand --execute '(progn (tabify-all-c++-buffers) (kill-emacs 0))'


Superscripts

C-x 8 ^1 in Emacs.
On some keyboards it is AltGr+Shift 1 There's also footnote.el which has more features.


TrueType Fonts

From: Peter Baumgartner
Enable TrueType Fonts under XWindow by running the xfstt font server. Extend the font path by
xset fp+ inet/127.0.0.1:7101
This line typically goes into ~/.xinitrc. Port number 7101 is the default.
Now you can use X-font specifications for ttf-fonts in the usual way. For instance, in ~/.Xresources, set the default XEmacs fonts with

XEmacs.default.attributeFont: -ttf-verdana-medium-r-normal-*-17-*-75-75-*-*-iso8859-1

Tweaking: Changing Emacs Options

In Emacs, you can enter M-x customize or, more simply, choose Options->Customize Emacs from the menu.

Some settings must be adjusted in your ~/.Xdefaults file. Add the following line to it.

Emacs*menubar*Font: -*-helvetica-bold-r-*-*-*-100-*-*-*-*-iso8859-*

Now the JDEE menu should fit within 80 columns. See more examples at http://petaxp.rug.ac.be/~erik/xemacs/sample.Xdefaults


Unicode

From Jasper Harder
This is an X Window (only) technique to check if a particular Unicode glyph is supported. You can determine which font Emacs will use with
internal-char-font
You could then call
xlsfonts -lll -fn FONT
and parse the output to get the font metrics. If the metrics for a particular code point contains all zeros, say
0x009f (159) 0 0 0 0 0 0x0000
then the glyph doesn't exist in this font.


Word Wrap

Emacs calls "Word Wrap" as fill mode. EMacro lets you type M-x auto-fill-mode to turn this on and off. Emacs does one better, where you can hit C-x . to indent plain text, after, for example, an * used as a bullet. M-q will insert a new bullet at the beginning of the current line. XEmacs lets you turn this on with M-x filladapt-mode


XEmacs .emacs Upgrade

When you upgrade your version of XEmacs, you may be prompted to migrate EMacro's .emacs file to ~/.xemacs/init.el


XEmacs Toolbar Adjustment

M-x edit-toolbar
default-toolbar
EMacro already loads user defined toolbars in ~/.xemacs/.toolbar


XML Remote DTD

EMacro has code in e-xml.el to load a DTD via an URI. For this to work, you must tell e-prefs.el that you connect to the net, with a fast connection (no modem). The following elisp libraries must also be installed:

Finally, nsgmls must be in the PATH. Make sure to e-refresh e-[x]cache.el.


GLOSSARY


Links to More Emacs Tips

http://www-numi.fnal.gov/fnal_minos/computing/emacs_primer.html

Valid XHTML 1.1! Valid CSS!