dot-emacs trickery

So, I will be offline for a few days, and Emacs-Fu has been a bit quiet anyways, so I'm inviting everyone to share their best little .emacs tricks. Things like:

(blink-cursor-mode nil)             ;; stop cursor from blinking
(setq search-highlight t            ;; highlight when searching... 
  query-replace-highlight t)        ;; ...and replacing
What useful trick should every emacs user know? Please use the comments to share your wisdom!


Unknown said...

Well, here's my little contribution, nothing special:

(savehist-mode) ; to save minibuffer history

(defalias 'yes-or-no-p 'y-or-n-p) ; to answer y or n instead of yes or no :-P ...I'm to lazy

(if (eq window-system 'x)
(set-face-font 'default '"8x13")

an emacs-fu fan! :)

p4bl0 said...

In fact my .emacs is a one-liner :
(load "~/.emacs.d/dotemacs.el")

And I have a git repos in ~/.emacs.d/ to keep my emacs config safe.

My Emacs config is available at http://github.com/p4bl0/emacs-config/

Note that the php-mode.el is a personal fork, I made some modifications to suit my taste.

Busty said...

;;To turn the beeping off
(setq visible-bell t)

p4bl0 said...

@Busty i did something more radical to achieve that: i wrote "xset b off" in my ~/.xinitrc so the beeping if off for my whole system (Bash, Emacs...).

I really don't like this kind of annoyance!

djcb said...

@valvo: ah, did not know savehist-mode -- great tip!

Black Sheep's Corner said...

;;; Don't quit unless you mean it!
(defun maybe-save-buffers-kill-emacs (really)
"If REALLY is 'yes', call save-buffers-kill-emacs."
(interactive "sAre you sure about this? ")
(if (equal really "yes")
(global-set-key [(control x)(control c)] 'maybe-save-buffers-kill-emacs)

a said...

I often have to look at particularly large files, and dislike

1) don't want undo taking memory/time
2) don't usually want to edit it

I use this hook to disable said features upon opening "large" files:

(defun tj-find-file-check-make-large-file-read-only-hook ()
"If a file is over a given size, make the buffer read only."
(when (> (buffer-size) (* 1024 1024))
(setq buffer-read-only t)
(message "Buffer is set to read-only because it is large. Undo also disabled.")))

(add-hook 'find-file-hooks 'tj-find-file-check-make-large-file-read-only-hook)

Saint Aardvark said...

Not much, but it's handy for me:

;; So I can kill (and thus paste) text from read-only buffer
(setq kill-read-only-ok 1)

; can't remember who I stole this from...
(defun x-hugh-reload-dot-emacs ()
(load-file "~/.emacs"))
(defun x-hugh-edit-dot-emacs ()
(find-file "~/.emacs"))
(global-set-key "\C-c\C-r" 'x-hugh-reload-dot-emacs)
(global-set-key "\C-c\C-e" 'x-hugh-edit-dot-emacs)

Anonymous said...

A few of my favs:

;; clean out buffers except shell, agenda and org
(defun restart ()
(let ((list (buffer-list)))
(while list
(let* ((buffer (car list))
(name (buffer-name buffer)))
(and (not (string-equal name "*shell*"))
(not (string-equal name "*Org Agenda*"))
(not (string-equal name "greg.org"))
(kill-buffer buffer)))
(setq list (cdr list)))))

;; allow shift + arrow for selections

;; bury the buffer
(global-set-key [f8] 'bury-buffer)

;; titlebar = buffer unless filename
(setq frame-title-format '(buffer-file-name "%f" ("%b")))

;; no splash screen
(setq inhibit-startup-message t)

my configs are at http://github.com/gregnewman/20seven-emacs and constantly evolving

xwl said...


It may be safer to use

(savehist-mode 1)

Unknown said...

Yeah, in my dotemacs I have (savehist-mode t), I forgot to type t ;) (I didn't use drag&drop :P)

Dmitry said...

My 2 cents:

;; mark current line:
(global-hl-line-mode 1)
;; color for current line:
(set-face-background 'hl-line "#e0f8ff")

rejeep said...

Here are some of my favorites:

;; Easy buffer switching by holding down shift and press any arrow key.
(windmove-default-keybindings 'shift)

;; Show unfinished keystrokes early.
(setq echo-keystrokes 0.1)

(defun nuke-all-buffers ()
"Kill all buffers, leaving *scratch* only."
(mapcar (lambda (x) (kill-buffer x)) (buffer-list)) (delete-other-windows))

Anonymous said...

;; Don't start the server unless we can verify that it isn't running.
(require 'server)
(when (and (functionp 'server-running-p) (not (server-running-p)))

;; Minibuffer bindings
(define-key minibuffer-local-map "\C-c\C-u" 'backward-kill-sentence)

;; Make M-z stop minimizing frames
(defun iconify-or-deiconify-frame nil)

;; Always add a final newline
(setq require-trailing-newline t)

;; Menu bars are free on OS X – the space is used whether they're
;; enabled or not – but a waste anywhere else.
(menu-bar-mode (if (eq 'ns window-system) 1 -1))

p4bl0 said...

I just read today about uniquify, and it's really great : see http://curiousprogrammer.wordpress.com/2009/04/28/emacs-hacks/

It allows to have for instance "Makefile|proj" and "Makefile|otherProj" as buffer name instead of "Makefile" and "Makefile<2>", and it is activated only if there are multiple files with the same name!

;; uniquify!
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "|")
(setq uniquify-after-kill-buffer-p t)
(setq uniquify-ignore-buffers-re "^\\*")

Steve Purcell said...

@p4bl0 - just rename your ~/.emacs.d/dotemacs.el to ~/.emacs.d/init.el, then you won't need your ~/.emacs any more.

p4bl0 said...

@Steve Purcell: you rocks!

This post is gonna be an Emacs Tricks Bible :-p

Raja said...
This comment has been removed by the author.
Raja said...

My small contribution

;; no yes-or-no
(defalias 'yes-or-no-p 'y-or-n-p)

;; Use control-arrow keys for window resizing
(global-set-key (kbd "<C-right>") 'enlarge-window-horizontally)
(global-set-key (kbd "<C-left>") 'shrink-window-horizontally)

;;; Drive out the mouse when it's too near to the cursor.
(mouse-avoidance-mode 'animate):

Steve Purcell said...

Here are a few obscure but handy settings (lots more here):

;; Show a marker in the left fringe for lines not in the buffer
(setq default-indicate-empty-lines t)

;; Undo/redo window layouts with "C-c <left>" and "C-c <right>"
(winner-mode 1)

;; Show approx buffer size in modeline

;; Make URLs in comments/strings clickable
;(add-hook 'find-file-hooks 'goto-address-prog-mode)

erbo5212 said...

Remap capslock to execute-extended-command and then defalias some infrequently used commands. Got the xmodmap trick from Sacha Chua :)

(if (eq window-system 'x)
(shell-command "xmodmap -e 'clear Lock' -e 'keycode 66 = F13'")
(global-set-key [f13] 'execute-extended-command))
(when win32
(setq w32-enable-caps-lock nil)
(global-set-key [f13] 'execute-extended-command)))

(defalias 'eb 'ediff-buffers)
(defalias 'ef 'ediff-files)

Ashutosh said...

;; Striping ^M from files, Stolen from BradfordHolcombe.emacs (its tiny, yet handy):

(fset 'dem [?\M-< ?\M-% ?\C-q ?\C-m return return ?! ?\M-<])

;; eldoc for quick reference
(require 'eldoc)
(autoload 'turn-on-eldoc-mode "eldoc" nil t)
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)

;; Delete the selected region when something is typed or with DEL
(delete-selection-mode 1)

;; blank-mode visualization
(autoload 'blank-mode "blank-mode" "Toggle blank visualization." t)

;; IBuffer management
(global-set-key "\C-x\C-b" 'ibuffer)

;; Use system trash (for emacs 23)
(setq delete-by-moving-to-trash t)

;; fm package that highlights corresponding line in source buffer as one moves through the output (occur, compilation, ...) buffer.
(require 'fm)
(add-hook 'occur-mode-hook 'fm-start)
(add-hook 'compilation-mode-hook 'fm-start)
;; Once following is activated in a buffer, it can be toggled with the "f" key in that buffer.

Eric said...

To go with the original post's blink cursor mode, I like

(bar-cursor-mode t)

as I think a vertical line between two characters is more in line (no pun intended) with what point means in Emacs.

Anonymous said...

Follow learnemacs on TwitterHere are a few things from my .emacs

;; display preferences
(global-font-lock-mode 1)
(show-paren-mode t)
(setq show-paren-style 'expression)
(tool-bar-mode -1)
(menu-bar-mode nil)
(scroll-bar-mode nil)
(setq inhibit-splash-screen t)
(transient-mark-mode t)
(setq-default truncate-lines t)
(and (fboundp 'blink-cursor-mode) (blink-cursor-mode (- (*) (*) (*))))
(column-number-mode 1)

;; shortcuts
(defalias 'eb 'eval-buffer)
(defalias 'er 'eval-region)
(defalias 'ee 'eval-expression)
(defalias 'elm 'emacs-lisp-mode)
(defalias 'eis 'elisp-index-search)

;; Inserts the date in the format
(defun insert-date ()
"Insert date at point."
(insert (format-time-string "%d.%m.%Y %H:%M")))

;; open table of contents
(defun toc ()
(find-file "~/NOTES/TOC.org")

;; dotemacs
(defun dotemacs ()
(find-file "~/.emacs")

(require 'twit)
(require 'nav)

Anonymous said...

;; show-paren-mode
(setq show-paren-delay 0
show-paren-style 'expression)
(show-paren-mode 1)
(set-face-background 'show-paren-match-face "#efedd2")

;; Initial *scratch* message
(setq initial-scratch-message (purecopy "\
;; Lisp Interaction Buffer:


;; Show calendar after start
(add-hook 'desktop-after-read-hook 'calendar)

Anonymous said...

Some bits I haven't seen posted yet:

;; Make scripts executable on save
(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)

;; disable C-z on X11 sessions
(when window-system
(global-unset-key "\C-z"))

;; Remember position in files between sessions
(when (require 'saveplace)
(setq-default save-place t))

;; copy/paste with accentuation intact
(setq selection-coding-system 'compound-text-with-extensions)

(autoload 'longlines-mode
"Minor mode for automatically wrapping long lines." t)

;; display current time in the modeline

I guess everyone knows this, but just in case: type M-x eval-buffer after adding stuff to your init file to have emacs use your new settings (and yell at you if you botched something).

Bill Night said...

Prevent clumsy fingers from stopping a long sequence of "undo" commands:

(global-set-key "\C-x\C-u" 'advertised-undo)

Disable other annoying keybindings:

(global-unset-key "\C-x3")
(if window-system
(global-unset-key "\C-x\C-z")
(global-unset-key "\C-z")))

Travis said...

wow, what a neat web site.

One trick I use a lot is when I'm looking at a lengthy debug log over and over. Typically, there will be certain changes that will help me decipher what's going on: maybe I'm stripping off the timestamp because I don't care, or I'm highlighting a key variable. It all depends on the project.

The emacs trick is that I wrap my edits into one routine, changing it as needed. Automagically, the debug logs are closer to what I need.

Here's two examples:

(defun fix-frames ()
"Make assorted edits to output of FieldCell debug statements"

(goto-char (point-min))
(save-excursion (replace-regexp "x=20, y=1" "NORMAL_LHS_ORIGIN" nil nil nil))
(save-excursion (replace-regexp "x=100, y=1" "NORMAL_RHS_ORIGIN" nil nil nil))
(save-excursion (replace-regexp "w=100, h=18" "SINGLE_LINE" nil nil nil))
(save-excursion (replace-regexp "icon.frame = {x=[0-9]+, y=[0-9]+; w=0, h=0}" "NO_ICON" nil nil nil))
(save-excursion (replace-regexp "x=0, y=0; w=0, h=0}" "{ZERO_FRAME}" nil nil nil))

(defun fix-order ()
"Make assorted sorts to narrowed output of ItineraryViewController debug statements.
if you aren't set up with the right narrowed, timestamped buffer, this command will arbitrarily scramble everything.

NB: this command is idempotent.

(save-excursion (replace-regexp "^.*?[ ]" "sorting " nil nil nil))
(shell-command-on-region (region-beginning) (region-end) "sort|uniq" (quote (4)) (quote (4)) nil t)
(sort-fields 7 (region-beginning) (region-end))
(sort-fields 6 (region-beginning) (region-end))

Anonymous said...

TIP: How to paste these stuff to your dotemacs file,

1. select the part of code (with mouse) in your browser.
2. middle click somewhere (probabily at the end) in your dotemacs buffer or C-y
3. C-M-\ to properly indent the code. (you don't want to rely on parenthesis much, you rely on indentation to read the code)
4. M-x eval-region (to check the code actually works. some blog automatically changes quote and double quotes symbols.)

And try C-h f, C-h F and C-h v at various points to see what all those functions, variables, modes and hooks mean.

Here's my share.

;; [usage-memo] [umemo] (integration of memo and help) ;; put it before all minor mode. ;; you write memo at the end of Help buffer and press C-x C-s to save it.
(require 'usage-memo)
(setq umemo-base-directory "~/em/umemo/") ; need not create the dir by hand.

;; [parenface] for hiding (or toning down) parenthesis in lisp modes
(require 'parenface)

;; [truncate] soft word wrapping mode on and off in emacs. ;; This does not change the content of the buffer.
;; (setq-default truncate-lines t) ; truncate lines by default, i.e. put soft word wrapping mode off by default. You need truncate-lines mode to easily read a big ascii table and a deeply indented code with many very long lines.
(global-set-key (kbd "\C-c w") 'toggle-truncate-lines)

Unknown said...

Pertinent to the last suggestion by yoosblog about turning word wrap on, consider also adding:
(setq truncate-partial-width-windows nil), Setting this the toggle-truncate trick will be possible in buffers into a split of windows. It overrides truncate functionality, which otherwise wouldn't work in a buffer contained in a frame split with two or more windows.

Shardul said...

Not particularly intelligent ones, but I find these helpful

1. in case of vertically split window.

;; enlarge frame height by 10 lines.
(global-set-key [f8] "\C-u\ 10\ \C-x\ ^")

;; get current line to the top of window.
(global-set-key "\C-t" "\C-u\ 1\ \C-l")

Eric said...

@shardul: That last one is not necessary in Emacs 23. C-l is now bound to recenter-top-bottom. One press gets to the old behaviour; two take the current line to the top and three to the bottom.

Anonymous said...

'(recentf-mode 1)

(global-set-key "\C-x\C-r" 'recentf-open-most-recent-file)