Moving to Emacs (for programmers)

I'm a Emacs newbie!!! This is what I'm learning right now.

Moving to Emacs from vi? See this.

To make a smooth transition you can consider using viper, by including this in your .emacs file:
(setq viper-mode t)
(require 'viper)
This enables vi key bindings, at different levels. From level 2 Ctrl-Z switches vi-Emacs modes. I quickly found it annoying, I think it is better to go directly to the Emacs approach.

Basic Emacs key-bindings and other tips for programmers

This launches Emacs in a text shell, not a X window:
emacs -nw

If you prefer a X window, you can still get nice black background reverting the colors with
emacs -rv

This enables mouse interaction even in a text shell:
M-x xterm-mouse-mode

In bold, the actions I use most:
Cancel (stop)C-g
OpenC-x C-f
SaveC-x C-s
Save as...C-x C-w
Save all C-x s
Go to bufferC-x b
Kill C-x k
Make read only C-x C-q
Write C-x C-w
horizontal splitC-x 2
vertical splitC-x 3
switchC-x o
delete currentC-x 0
delete othersC-x 1
scroll otherESC C-v
find file in new windowC-x 4 f
Begin selectionC-SPACE
Select paragraphESC h
Copy ESC w
Kill (cut)C-w
yank (paste)C-y
Paste former copiesESC y
kill lineC-k
kill rectangle <br /> defined by selectionC-x r k
yank rectangleC-x r y
repeat next commandESC (number)
repeat four times next commandC-u
last complex commandsC-x ESC ESC
transpose characterC-t
transpose wordM-t
complete textM-/
undo lastC-/
normal searchC-s RET
search word under cursorC-s C-w RET
Query-replace ESC %
Regular expression search ESC C-s RET
Regular expression <br /> query-replace query-replace-regexp
grepM-x grep RET
CreateC-x r m
Go toC-x r b
External commands
shell commandESC !!!
shell command on selectionESC |
shell modeESC x shell
terminateC-c C-c
lastESC p
begin defC-x (
end defC-x )
runC-x e
appendC-u C-x (
nameESC x name-last-kbd-macro
saveC-x C-f filename RET <br /> ESC x insert-kbd-macro RET <br /> macroname RETURN <br /> C-x C-s
loadESC x load-file
indent-regionESC C-\
join linesESC ^
go to 1st colC-a
go to last colC-e
go to first charESC m
c-begining-of-statementESC a
beginning-of-defunESC C-a
c-fill-paragraphESC q
add commentESC ;
compileESC x compile RET
nect-errorC-x `
find tagM-.
find nextC-u M-.
previousC-u - M-.
find-tag-other-windowC-x 4 . tag RET
find-tag-other-frameC-x 5 . tag RET
find with regexC-M-. pattern RET
nextC-u C-M-
complete tagESC TAB
helpC-h ...
describe-keyC-h k
command-aproposC-h a

Setting tags tables

My tags tables are in different places, depending on the code I'm working with. I found it easy to have an environmental variable $DEVELOP pointing to the directory I'm working in, which has TAGS for Emacs and tags for vi.

In my crontab I have things like this:

20 3 * * * /usr/bin/ectags -R -f ~/210/tags ~/210/*
25 3 * * * /usr/bin/ectags -R -e -f ~/210/TAGS ~/210/*

The first one makes tags for vi, the second one for Emacs, using Exuberant ctags (http://ctags.sourceforge.net/). (Notice that my binary was renamed to ectags, I think the default one is just ctags).

When working on the directory named 210, for example, I export DEVELOP=~/210. To load the correct tags file, I included this in my .emacs:

(setq tags-file-name (concat (getenv "DEVELOP") "/TAGS"))

Alternatively you can use

(setq tags-table-list (list (getenv "DEVELOP")))

which will allow you to add extra alternative locations if necessary (notice that with this syntax the tags file is expected to be named TAGS).

Multiple tags files

That approach of having just one tags file worked nicely while I used vi, which can smartly handle a big file. Emacs, on the other side, searches for tags in a slower way and the single-file approach becomes a nightmare.

For emacs I generate tag files per directory by using the following scripts. I manually call the scripts from time to time only, I do not include them in the crontab anymore, but I think that would be also OK. (This follows this).

This is the main one, which I call ~/bin/tag.
# This script works together with dirtags.

if [-z "$DEVELOP"]; then
    echo define DEVELOP
    exit 127
find * -type d -exec ~/bin/dirtags {} \;
ectags --file-scope=no -Re

It calls ~/bin/dirtags, that is as follows:
IFTEST=`echo $1|grep dist`
IFTESTB=`echo $1|grep CVS`

cd $1
rm -f TAGS

if [-z "$IFTEST" -a -z "$IFTESTB"]; then
    ectags -e *
# Now append the non-static tags from descendent directories
    find * -type d -prune -print|ectags -eaR --file-scope=no -L-

For this approach to work, my ~/.emacs config file does not have any reference to tags-file-name anymore, and it uses
(setq tags-table-list (list "./" "./../" "./../../" "./../../../" (getenv "DEVELOP")))

This goes much faster.

Configuring xterm under Mac OS X

Option key as META

This is at the price of disabling nice shortcuts like copy and paste, so you decide. I think I preferred leaving that alone and use the ESC for META commands.

You should configure X11 for ease of use with Emacs and SSH. Choose Preferences from the X11 menu, and select the Input tab. Uncheck the box labeled Enable key equivalents under X11. This allows the Command (or Apple) key to work as a meta key in Emacs. Check the box for Emulate three button mouse if necessary, and read the description of that option. It means that holding down the Option key while you click will cause X clients to see the click as a mouse-2 (middle button) event. And the Command (Apple) key will produce mouse-3 (right button) events in a similar way. For both the terminal window and Emacs under X, you'll sometimes need to hold down Control and either Option or Command while clicking, to produce C-mouse-2 or C-mouse-3. (Well, at least it works.)

... And if you issue the command M-x xterm-mouse-mode in a non-X Emacs session, it will even respond to some mouse events. You'll need to experiment to find out which things work.

CS50 Emacs Setup Guide

Unmap C-h

C-h was mapped to DEL, making difficult not only getting help (C-h) but also executing more complex things like M-C-h. To solve this I tried different suggestions (like this and this) and changed the X11 terminal (xterm-color) settings, at a shell, with
sudo stty erase ^\?

In this other place it is suggested to tweak the terminfo, thus I also tried
infocmp > xterm-color.info

editing that file to change kbs to something else (-+\177+- or ^?) and then save that with
sudo tic xterm-color.info

(by the way, I had to delete the nxterm alias on the second line, because that caused an error due to duplication when doing tic), but I now think that this is redundant or useless, it seems to work with the stty change above.

Make mouse wheel work

Enable mouse interaction for xterm mode, and add this to .emacs:
(mouse-wheel-mode t)

(This does the same, but with more complexity!!!)

Other information sources

Link Disclaimer. External links are provided only as an informational resource. Please note that many of the links provided are not on one of the Scientific Volume Imaging servers and are not maintained by us or affiliated with any services provided by us. These links are provided simply as a service, and it should not be implied that we recommend, endorse or approve of any of the content at the linked sites.