Emacs

(I intend to add to this page from time to time.)

Emacs Tips & Tricks

How to edit a file as root in running Emacs

Use the normal find-file (C-x C-f) and open /sudo::/PATH/TO/FILE . Emacs will ask for the root password.

List the colors available in Emacs

M-x list-colors-display

ESS (Emacs speaks statistics)

In 2007 I wrote some lisp code which made ESS mode show the arguments of the R function and their default values interactively. The ESS maintainers kindly improved my code a lot and put it into ESS 5.3.5, so that it now is part of ESS. I am glad, I was able to contribute to this fantastic software.

My ~/.emacs file

;; This is Sven Hartenstein's ~/.emacs file
;; last change 2009-12-12 (or later)

;; ==================================================
;; Basics
;; ==================================================

;; Toolbar and menu bar distract you from learning the key
;; bindings (i.e. becoming efficient).
(tool-bar-mode 0)
(menu-bar-mode 0)
(scroll-bar-mode 0)

;; Inhibit startup message
(setq inhibit-startup-screen t)

;; Cursor, please do not blink
(blink-cursor-mode nil)

;; Do not make backup files
(setq make-backup-files nil)

;; When emacs asks for "yes" or "no", let "y" or "n" sufficide
(fset 'yes-or-no-p 'y-or-n-p)

;; Mode line non-3D
(set-face-attribute 'mode-line nil :box nil)

;; Show column number in mode line
(setq column-number-mode t)

;; When point is on paranthesis, highlight the matching one
(show-paren-mode t)

;; Indicate empty lines at the end of buffer
(setq default-indicate-empty-lines t)

;; Show trailing whitespace
(setq-default show-trailing-whitespace t)

;; Switch buffers the easy way
(iswitchb-mode 1)

;; Scroll one line (not half a page) when moving past the bottom of
;; the window
(setq scroll-step 1)

;; Leave point at same position in window when scrolling page
(setq scroll-preserve-screen-position t)

;; Yank from clipboard, kill into clipboard
(setq x-select-enable-clipboard t)

;; Yank at point, not where the mouse is
(setq mouse-yank-at-point t)

;; Scroll with mouse only one line at a time
(setq mouse-wheel-scroll-amount '(1 ((shift))))

;; Put mouse away when using keyboard
(mouse-avoidance-mode 'banish)
;; Default is upper right corner, but this interferes with
;; notifications, thus i change it:
(defun mouse-avoidance-banish-destination ()
  "The position to which mouse-avoidance-mode `banish' moves the mouse.
Redefined by Sven."

 (cons (1- (frame-width)) 10))

;; No tabs but spaces for indentation
(setq indent-tabs-mode nil)

;; Show keystrokes in minibuffer immediately
(setq echo-keystrokes 0.01)

;; End sentences with one space, not two (affects fill commands)
(setq-default sentence-end-double-space nil)

;; Let week start with Monday (not Sunday)
(setq calendar-week-start-day '1)

;; Abbrevs are fun (especially when writing mails).
(setq-default abbrev-mode t)
(setq abbrev-file-name "~/.emacs-stuff/abbrev_defs")
;; Be sure that the file exists, otherwise Emacs complains.
(quietly-read-abbrev-file abbrev-file-name)
;; Save abbrevs when exiting emacs without asking.
(setq save-abbrevs 'silently)


;; ==================================================
;; Some handy functions and their key bindings
;; ==================================================

;; This function is called when I start my "main" emacs by running
;; "emacs -title Emacs -f my-start-emacs". Otherwise (i.e. in my vm
;; instance) it is not.
(defun my-start-emacs ()
  "This function is to be called from the command line: emacs -f
my-start-emacs."

  (interactive "*")
  (server-start) ; Start the emacs server so that emacsclient can use it
  (load "~/.emacs-stuff/nxhtml/autostart.el")
  (find-file "~/dokumente/org/todo.org") ; Show my todo list
  )

;; Insert date (stolen from somewhere in the www)
(defun my-insert-date ()
  "Insert the current date."
  (interactive "*") (insert (format-time-string "%Y-%m-%d")))
(global-set-key [f2] 'my-insert-date)

;; Scrolling buffer without moving the point within the window. I use
;; this a lot. Info mode uses M-n for something else which I do not
;; need, therefore the extra key binding.
(defun my-scroll-up (&optional arg)
  "Scroll up (forward in text) one (or N) line(s)."
  (interactive "p") (progn (scroll-up (or arg 1)) (next-line)))
(global-set-key (kbd "M-n") '(lambda nil "" (interactive) (my-scroll-up)))
(add-hook 'Info-mode-hook
          '(lambda () (interactive)
             (define-key Info-mode-map (kbd "M-n") '(lambda nil "" (interactive) (my-scroll-up)))))
(defun my-scroll-down (&optional arg)
  "Scroll down (backward in text) one (or N) line(s)."
  (interactive "p") (progn (scroll-down (or arg 1)) (previous-line)))
(global-set-key (kbd "M-p") '(lambda nil "" (interactive) (my-scroll-down)))

;; This comes in useful after popups or so (especially in ESS where
;; the R process is in the small window at the bottom).
(defun my-fix-window-size (height)
  "Set the window's height to frame-height minus 8 (or prefix arg HEIGHT) rows."
  (interactive "P")
  (enlarge-window (- (frame-height) (or height 8) (window-height))))
(global-set-key (kbd "C-<tab>") 'my-fix-window-size)

;; Kill from point to end of buffer. I bind it to M-k which by default
;; is kill-sentence which I do not use.
(defun my-kill-to-eob ()
  "Kill everything from point to end of buffer."
  (interactive "*")
  (save-excursion
    (setq start (point))
    (end-of-buffer)
    (kill-region start (point))
    (goto-char start)))
(global-set-key (kbd "M-k") 'my-kill-to-eob)

;; Copy char from previous/next line
(defun line-copy-char (&optional b)
  "Copy a character exactly below/above the point to the current point
of the cursor (default is above)."

  (interactive "p")
  (let (p col s)
    (setq p (point))
    (setq col (current-column))
    (forward-line (if b -1 1))
    (move-to-column col)
    (setq s (buffer-substring (point) (+ (point) 1)))
    (goto-char p)
    (insert s)))
(global-set-key [(control <)] 'line-copy-char)
(global-set-key [(control >)] '(lambda ()(interactive)(line-copy-char nil)))


;; ==================================================
;;  org mode
;; ==================================================

(add-hook 'org-mode-hook
          '(lambda ()
          (auto-fill-mode 1) ; Wrap long lines
          ))

;; Show empty line between subtrees even if there is only one
(setq org-cycle-separator-lines 1)

;; Export without table of contents or section numbers
(setq org-export-with-toc nil)
(setq org-export-with-section-numbers nil)


;; ==================================================
;;  AUCTeX
;; ==================================================

(add-hook 'TeX-mode-hook
          '(lambda ()
;;; (setq-default TeX-master nil) ; multi-file documents
             (setq TeX-PDF-mode t) ; Use pdflatex by default
             (setq TeX-auto-save t) ; recommended in quickstart
             (setq TeX-parse-self t) ; recommended in quickstart
             (outline-minor-mode)
             (turn-on-auto-fill)
             (TeX-fold-mode 1)
             (setq font-latex-verbatim-environments '("verbatim" "verbatim*" "lstlisting"))
             (setenv "TEXINPUTS" ".:/home/sven/.sven_latex/:")
             ))


;; ==================================================
;;  ESS: EMACS SPEAKS STATISTICS
;; ==================================================

(add-hook 'ess-mode-hook
          '(lambda ()
             (setq inferior-R-args "--no-restore-history --no-save")
             (setq ess-ask-for-ess-directory nil)
             (setq comint-scroll-to-bottom-on-input t)
             (setq comint-scroll-to-bottom-on-output t)
             ))

;; Do everything with S-RET
;; from http://www.emacswiki.org/cgi-bin/wiki/EmacsSpeaksStatistics
(defun my-ess-eval ()
  (interactive)
  (if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
      (progn
        (delete-other-windows)
        (setq w1 (selected-window))
        (setq w1name (buffer-name))
        (setq w2 (split-window w1 25)) ; 40 lines in upper window
        (R)
        (set-window-buffer w2 "*R*")
        (set-window-buffer w1 w1name)))
  (if (and transient-mark-mode mark-active)
      (call-interactively 'ess-eval-region)
    (call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook '(lambda() (local-set-key [(control return)] 'my-ess-eval)))


;; ==================================================
;; vm (viewmail) development version
;; ==================================================

(add-to-list 'load-path (expand-file-name "~/.sven_scripts/vm/lisp"))
(add-to-list 'Info-default-directory-list (expand-file-name "~/.sven_scripts/vm/info"))
(require 'vm-autoloads)


;; ==================================================
;; other modes
;; ==================================================

;; CSS mode: Indent 2 spaces only
(setq css-indent-offset 2)


;; ==================================================
;; Global key bindings
;; ==================================================

;; Go to line N
(global-set-key (kbd "M-g") 'goto-line)

;; Similar to "C-l" but places me more to the top. I use this a lot.
(global-set-key (kbd "C-ö") '(lambda nil "" (interactive) (recenter 8)))

;; Use electric buffer list
(global-set-key [(control x)(control b)] 'electric-buffer-list)


;; ==================================================
;; Customization that Emacs does better than I can
;; ==================================================

(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default ((t (:inherit nil :stipple nil :background "white" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 181 :width normal :foundry "unknown" :family "DejaVu Sans Mono"))))
 '(fringe ((((class color) (background light)) (:background "grey95" :foreground "goldenrod")))))
Zuletzt geändert am 31.12.2011 17:18 Uhr