Saturday, November 24, 2007

Emacs survival kit

  • ESC x  set font-lock-mode
  • Enable PHP highlighting on ubuntu (I used emacs21)
  • aptitude install emacs-snapshot emacs-goodies-el php-elisp ruby-elisp python-elisp

  • Useful commands:
    C-something = CTRL+something
    M-something = ESC then something

    C-x b : swith buffers
    C-x o : other splitted window
    C-SPACE : yank current line
    C-w : delete selected block
    M-x : grep
    M-% : replace
    C-g : quit all macro
    C-x C-c : exit emacs
    C-x C-f ~ : to know where is your .emacs
  • C-q C-j: Inserting a CR in an emacs file
    M-x whitespace-mode:  show whitespaces and tabs:

  • Macros:

  • C-x ( start recording key strokes to build a keyboard macro.
  • C-x ) stop recording key strokes to for a keyboard macro.
  • C-x e execute the keyboard macro. Only one keyboard macro can be defined at a time. This is very powerful when used in conjunction with C-u 
  • do for example: C-u 200 C-x e 

  • .emacs file

  • ;; show column numbers
    (setq column-number-mode t)
    (set-frame-font "DejaVu Sans Mono-09")

    (set-frame-font "DejaVu Sans Mono-8")
    ;; 1ASC-Liberation Mono-bold-normal-normal-*-*-*-*-*-m-0-iso10646-1

    ;; 2 spaces for tabs
    (setq-default indent-tabs-mode nil);
    (setq default-tab-width 2);

    ;; don't back up tilde files
    (setq make-backup-files nil)

    ;; show column numbers
    (setq column-number-mode t)

    ;; show matching parentesis when pressing % on a parenthesis
    (global-set-key "%" 'match-paren)
    (defun match-paren (arg)
      "Go to the matching paren if on a paren; otherwise insert %."
      (interactive "p")
      (cond ((looking-at "\\s(") (forward-list 1) (backward-char 1))
    ((looking-at "\\s)") (forward-char 1) (backward-list 1))
    (t (self-insert-command (or arg 1)))))
  • json
  • (Just run M-x beautify-json on a buffer and it will reformat it)
  • (defun beautify-json ()
      (interactive)
      (let ((b (if mark-active (min (point) (mark)) (point-min)))
            (e (if mark-active (max (point) (mark)) (point-max))))
        (shell-command-on-region b e
         "python -mjson.tool" (current-buffer) t)))
  • to compile C++ using the F7 key
  • (global-set-key [f7] 'compile)
  • Vista
  • You cannot create a .emacs file simply on Vista (problem creating a file with a name starting with a dot). What I did is download one from the net.

    • Replace in multiple files
    1. Call dired to list files in dir, or call find-name-dired if you need all subdirectories. (just select all files at this step "*")
    2. Mark the files you want. You can mark all by doing CTRL G m m .You can mark by regex by typing 【% m】. (pom.xml for example)
    3. Type Q to call dired-do-query-replace-regexp.
    4. Type your find regex and replace string. 〔➤ common elisp regex pattern
    5. For each occurrence, type y to replace, n to skip. Type 【Ctrl+g】 to abort the whole operation.
    6. Type ! to replace all occurrences in current file without asking, N to skip all possible replacement for rest of the current file. (N is emacs 23 only)
    7. To do the replacement on all files without further asking, type Y. (Emacs 23 only)
    8. Call ibuffer to list all opened files.
    9. Type 【* u】 to mark all unsaved files, type S to save all marked files, type D to close them all.
  • Debugging C++ under emacs22 
  • - M-x gdb
  • Left click on the light gray column on the C++ source to set breakpoints (gdb needs to be running)
  • Use icons at the top as debugger commands.
  • - I tried "ddd". It is on top of gdb. It is OK, but it is an external editor, independent of emacs. The ddd window is an XWindow but it is very close to what you get with gdb+emacs.
  •  .

    No comments: