1. Rusi Mody
  2. VIT-Projects

Wiki

Clone wiki

VIT-Projects / emacs

Basic Setup

Set up files

  • Init file: Emacs can pick up its setup code in the file ~/.emacs

    [~ is your home directory ie $HOME ]

    Alternatively it can take its setup from ~/.emacs.d/init.el

    I STRONGLY recommend the second ie init.el inside .emacs.d

    But for this to work you need

    • to remove ~/.emacs
      and/or
    • move/add its contents to ~/.emacs.d/init.el
    • custom file: I see some of you nicely set up with black color backgrounds etc. The black is nice (and even saves power!).

    If you did that you will find that emacs has dumped settings into your ~/.emacs file (now renamed to ~/.emacs.d/init.el)

    This is very unpleasant because it mixes machine-generated setup (ie customize output) with handwritten setup.

    Fortunately emacs allows you to configure this more cleanly.

    If you put

    (setq custom-file "~/.emacs.d/custom-file.el")
    (load custom-file)
    

    in your init.el file, then emacs will put customize output into ~/.emacs.d/custom-file.el and leave your handwritten options in ~/.emacs.d/init.el

    Note if you have no customize settings just make an empty custom-file.el else emacs barfs on the load above.

Summary

In short these are the files -- all under ~:

  1. .emacs file should NOT exist
  2. .emacs.d directory should exist
  3. .emacs.d/init.el file written by you
  4. .emacs.d/custom-file.el file generated by emacs

Now onwards add setup to init.el and let emacs itself add to custom-file.el

Backup Files

One nuisance of emacs is that it puts backup files all over the place. ie if you edit a file lintel.c it will make a file lintel.c~. Its wonderful to be able to go to a backup but not all over the place. These settings (put into init.el ) will put them all in one place:

(setq  backup-directory-alist '(("." . "~/emacs-saves")))

[Choose any directory you like in place of ~/emacs-saves ]

ie

  1. Add the above like to init.el
  2. Create the directory
  3. Check it works... how???
    1. Make a new file, add a few lines to it (better not under git)
    2. Save and exit emacs
    3. Reopen emacs with that file and add/modify some lines
    4. Save exit

Org Mode

Emacs comes with org mode builtin but it may be a bit old This will give you the version

M-x org-version

< than 8 may not work on everything

How to setup new org

Tags

Tags are a way for emacs to find your way round sources. ie if you tag your src directory then emacs can help you find your way around. Here I am explaininig simplest tagging mechanism - etags.

Creating tags

From emacswiki

You create tags by running etags on your source files.

Simple Case

[Change /path/to/my/project to whatever path is appropriate for you on your machine]

Run

$ cd /path/to/my/project
$ etags *.[ch]

in your src directory. It will create a TAGS file

General case

If (eg cpython) you have many directories containing source files Run

$ cd /path/to/my/project
$ find . -type f -iname "*.[ch]" | etags -

Using tags

  • M-. finds a tag
  • M-, finds next tag
  • M-* jumps back

More here

gud (gdb)

Gdb under emacs is called gud (here)
More detailed Key bindings

Note that you dont need to go to the web for that. The builtin help of emacs called info has the same information
[Remember M-: means Alt-Shift-Colon (or Escape-Colon)]

M-: (info "(emacs)commands of gud")
M-: (info "(emacs)debuggers")

Remember to TYPE the M-: and copy-paste the stuff after that ie (info...) !

For this you need to have installed these apt-packages

emacs24-el emacs24-common-non-dfsg, emacs24-common

99% these will be there but just in case :-)

Another thing (this is new for us!) to try is inside emacs, after starting gdb, do

M-x gdb-many-windows

Some settings are needed to make it run straight-off -- not sure which

Set emacs as default editor

This expanded but experimental section on how to Make emacs your default editor

C Editing

About indentation customization, completion of (), [] etc

Note

Suggestions below will not work unless you put them into init.el and you RESTART EMACS.
ie Trying out the lisp in *scratch* buffer does not work

Indentation

See Indenting C
Also General indenting styles

As far as I can see adding the following add to ~/.emacs.d/init.el

(setq c-default-style "bsd"
      c-basic-offset 3)

(c-set-offset 'substatement-open 0)  ;; not necessary usually

should be good. Then use C-c C-q on a function to check.

Indentation of 3 seems to be what Don uses.
2 is what I usually prefer (otherwise).
But best to stay consistent with existing code.

Completion

of brackets quotes etc Insert closing brackets

Short version: put into init (electric-pair-mode 1)

More general snippets

There is yasnippet Though I recommend you dont waste too much time on that now :-)

Updated