How to highlight a particular line in emacs? - emacs

I need to highlight facility for emacs in order to mark some lines in file while working with it.
It should be smth like M-s h l but should work based on line number, not on a regexp. I want to highlight a current line, but the hl-line-mode is not suitable, as I need to highlight many lines, every time I press a specific key on each of them.

I just quickly wrote the following:
(defun find-overlays-specifying (prop pos)
(let ((overlays (overlays-at pos))
found)
(while overlays
(let ((overlay (car overlays)))
(if (overlay-get overlay prop)
(setq found (cons overlay found))))
(setq overlays (cdr overlays)))
found))
(defun highlight-or-dehighlight-line ()
(interactive)
(if (find-overlays-specifying
'line-highlight-overlay-marker
(line-beginning-position))
(remove-overlays (line-beginning-position) (+ 1 (line-end-position)))
(let ((overlay-highlight (make-overlay
(line-beginning-position)
(+ 1 (line-end-position)))))
(overlay-put overlay-highlight 'face '(:background "lightgreen"))
(overlay-put overlay-highlight 'line-highlight-overlay-marker t))))
(global-set-key [f8] 'highlight-or-dehighlight-line)
(Here find-overlays-specifying came from the manual page)
It will highlight current line, and when used again it will remove it.
Maybe the following could be useful as well: removing all your highlight from the buffer (could be dangerous, you might not want it if you highlight important things)
(defun remove-all-highlight ()
(interactive)
(remove-overlays (point-min) (point-max))
)
(global-set-key [f9] 'remove-all-highlight)

You can use bm.el. You can install bm.el from MELPA.
bm.el provides bm-toggle to highlight and unhighlight current line.
bm.el also provides bm-bookmark-regexp which highlights only matched lines.
And you can jump between highlighted lines by bm-previous and bm-next
Following is sample configuration of bm.el
(require 'bm)
(global-set-key (kbd "<f5>") 'bm-toggle)
(global-set-key (kbd "<f6>") 'bm-previous)
(global-set-key (kbd "<f7>") 'bm-next)
(global-set-key (kbd "<f8>") 'bm-bookmark-regexp)

Bookmark+ does what you are asking for. Use C-x p RET (by default) to set a bookmark at point. And you can configure the kind of highlighting you want for such bookmarks. This is similar to what bm.el offers (syohex's answer), but more flexible.

Related

emacs ow can I helm-find with default directory pre-specified?

I use emacs for notes mainly. All my notes are in:
~/Dropbox/Uni/Notes
I want to tie a keyboard shortcut (e.g C-f12) to do a helm-find that always starts in the above dir irrelevant of the source buffer.
I have tried:
(global-set-key (kbd "C-<f2>") (lambda () (interactive) (helm-find "~/Dropbox/Uni/Notes/")))
But when I run it, it still prompts me for 'DefaultDirectory' which is usually the same as the current buffer.
?
[edit]
I made a hack-around:
(global-set-key (kbd "<C-f2>")
(lambda ()
(interactive)
(find-file "~/Dropbox/Uni/Notes/leo.org")
(helm-find nil)))
That opens a file and then when I do a helm-find, it's relative to leo.org's location. But a better solution would be preferred.
[edit]
Below solution works perfectly.
Here you go:
(defmacro helm-find-note (dir)
`(defun ,(intern (format "helm-find-note-%s" dir)) ()
(interactive)
(let ((default-directory ,dir))
(helm-find nil))))
(global-set-key (kbd "C-M-3") (helm-find-note "~/Downloads"))

Emacs: scroll buffer not point

Is it possible to scroll the entire visible portion of the buffer in Emacs, but leave point where it is. Example: point is towards the bottom of the window and I want to see some text which has scrolled off the top of the window without moving point.
Edit: I suppose C-l C-l sort of does what I wanted.
try these. Change M-n and M-p key bindings according to your taste
;;; scrollers
(global-set-key "\M-n" "\C-u1\C-v")
(global-set-key "\M-p" "\C-u1\M-v")
;;;_*======================================================================
;;;_* define a function to scroll with the cursor in place, moving the
;;;_* page instead
;; Navigation Functions
(defun scroll-down-in-place (n)
(interactive "p")
(previous-line n)
(unless (eq (window-start) (point-min))
(scroll-down n)))
(defun scroll-up-in-place (n)
(interactive "p")
(next-line n)
(unless (eq (window-end) (point-max))
(scroll-up n)))
(global-set-key "\M-n" 'scroll-up-in-place)
(global-set-key "\M-p" 'scroll-down-in-place)
This might be of use. According to the EmacsWiki page on Scrolling;
The variable scroll-preserve-screen-position may be useful to some.
When you scroll down, and up again, point should end up at the same
position you started out with. The value can be toggled by the built
in mode M-x scroll-lock-mode.
I think this is better:
(defun gcm-scroll-down ()
(interactive)
(scroll-up 1))
(defun gcm-scroll-up ()
(interactive)
(scroll-down 1))
(global-set-key [(control down)] 'gcm-scroll-down)
(global-set-key [(control up)] 'gcm-scroll-up)
reference : emacs wiki
;; Preserve the cursor position relative to the screen when scrolling
(setq scroll-preserve-screen-position 'always)
;; Scroll buffer under the point
;; 'scroll-preserve-screen-position' must be set to a non-nil, non-t value for
;; these to work as intended.
(global-set-key (kbd "M-p") #'scroll-down-line)
(global-set-key (kbd "M-n") #'scroll-up-line)
Based on Bilal's answer:
(global-set-key [(meta down)] (lambda () (interactive) (scroll-down 1)))
(global-set-key [(meta up)] (lambda () (interactive) (scroll-up 1)))

Emacs tab between buffers

Is there a way to switch between buffers without having to go through the
buffer-list, or writing the name of the buffer that I want to switch to? More specific I wonder if emacs can tab between buffers much like how it is working in notepad++
Emacs 22.1 and higher supports the previous-buffer (C-x <left arrow>) and next-buffer (C-x <right arrow>) commands.
These two commands can be added to older Emacsen using this script.
I've never ended up using C-x <right> or C-x <C-right> much, because I find them cumbersome to repeat if I want to cycle past more than one buffer, so I've just written a couple of functions to let you continue to switch to next/previous-buffer with <C-right> and <C-left> if the last command was also a next/previous-buffer command.
e.g. C-x <C-left> <C-left> <C-left> <C-right> <C-left> would take you back three buffers, forward one, and backward again.
I've made the assumption that <C-left> & <C-right> are usually bound to forward/backward-word, and am calling those explicitly as the fallback.
(defun my-forward-word-or-buffer-or-windows (&optional arg)
"Enable <C-left> to call next-buffer if the last command was
next-buffer or previous-buffer, and winner-redo if the last
command was winner-undo or winner-redo."
(interactive "p")
(cond ((memq last-command (list 'next-buffer 'previous-buffer))
(progn (next-buffer)
(setq this-command 'next-buffer)))
((memq last-command (list 'winner-redo 'winner-undo))
(progn (winner-redo)
(setq this-command 'winner-redo)))
(t ;else
(progn (forward-word arg)
(setq this-command 'forward-word)))))
(defun my-backward-word-or-buffer-or-windows (&optional arg)
"Enable <C-left> to call previous-buffer if the last command
was next-buffer or previous-buffer, and winner-undo if the last
command was winner-undo or winner-redo."
(interactive "p")
(cond ((memq last-command (list 'next-buffer 'previous-buffer))
(progn (previous-buffer)
(setq this-command 'previous-buffer)))
((memq last-command (list 'winner-redo 'winner-undo))
(progn (winner-undo)
(setq this-command 'winner-undo)))
(t ;else
(progn (backward-word arg)
(setq this-command 'backward-word)))))
(global-set-key (kbd "<C-left>") 'my-backward-word-or-buffer-or-windows)
(global-set-key (kbd "<C-right>") 'my-forward-word-or-buffer-or-windows)
(I use Icicles for buffer switching, myself, but...)
If you want to repeat the previous command any number of times, just use C-x z z z z z z... In this case, e.g., C-x left C-x z z z...
If that's too cumbersome, bind (next|previous)-buffer to other, repeatable keys, as others have suggested.
But repeatable keys are in great demand. If you don't want to waste any, you can even put such commands on a prefix key, so that, e.g., you can do, e.g., C-x left left left... Here's a trick to do that (taken from the Bookmark+ code):
(defun my-repeat-command (command)
"Repeat COMMAND."
(let ((repeat-message-function 'ignore))
(setq last-repeatable-command command)
(repeat nil)))
(defun my-next-whatever-repeat (arg) ; `C-x right'
"Jump to the Nth-next whatever.
N defaults to 1, meaning the next whatever.
Plain `C-u' means start over at the first whatever (and no repeat)."
(interactive "P")
(require 'repeat)
(my-repeat-command 'next-whatever))
(define-key ctl-x-map [right] 'my-next-whatever-repeat
Although the following suggestion does use the buffer-list, Ivy (or Helm) and evil, I think it is a nice, nearly equivalent alternative to the common Ctrl-TAB way (incl. updating the buffer list) when using e.g. Spacemacs.
Of course the commands can be adapted to your personal config (vanilla Emacs)
(evil-global-set-key 'motion (kbd "<C-tab>") 'ivy-switch-buffer)
(evil-global-set-key 'insert (kbd "<C-iso-lefttab>") 'ivy-switch-buffer)
(define-key ivy-mode-map (kbd "<C-tab>") 'ivy-next-line-and-call)
(define-key ivy-mode-map (kbd "<C-iso-lefttab>") 'ivy-previous-line-and-call)
or for the equivalent for Helm
(evil-global-set-key 'motion (kbd "<C-tab>") 'helm-buffers-list)
(evil-global-set-key 'motion (kbd "<C-iso-lefttab>") 'helm-buffers-list)
(define-key helm-map (kbd "<C-tab>") 'helm-follow-action-forward)
(define-key helm-map (kbd "<C-iso-lefttab>") 'helm-follow-action-backward)
The keystring for the last command means Ctrl-Shift-tab in my keyboard. You can find the one to use with C-h k C-S-tab.
Indeed you still need to press RET or C-l after releasing <C-tab>.
For evil users using Helm, a possibly even nicer alternative is to bind helm-buffers-list to C-j and C-k, then set helm-follow-mode-persistent to t, and in the helm-buffers-list buffer activate helm-follow-mode with C-c C-f. Now you can switch (and preview) buffers with C-j and C-k.

.emacs global-set-key and calling interactive function with argument

In my .emacs i have the following function that transposes a line
(defun move-line (n)
"Move the current line up or down by N lines."
(interactive "p")
(let ((col (current-column))
start
end)
(beginning-of-line)
(setq start (point))
(end-of-line)
(forward-char)
(setq end (point))
(let ((line-text (delete-and-extract-region start end)))
(forward-line n)
(insert line-text)
;; restore point to original column in moved line
(forward-line -1)
(forward-char col))))
And I bind a key to it like this
(global-set-key (kbd "M-<down>") 'move-line)
;; this is the same as M-x global-set-key <return>
However, I want to bind M-up to move-line (-1) But I cant seem to be able to do it correctly:
;; M-- M-1 M-x global-set-key <return>
How do I define the above using global-set-key to call move-line -1?
Not minutes after asking the question I figured it out by copy+pasting code. However I have no clue how it works.
(global-set-key (kbd "M-<up>") (lambda () (interactive) (move-line -1)))
global-set-key only takes 2 arguments: the key sequence and the command you want to bind to it. So
(global-set-key (kbd "M-<down>") 'move-line)
works fine. But if you want to use move-line with an argument you need to wrap it in an anonymous (aka lamba) function so that it presents itself to global-set-key as one value.
You can simply ask for the number of lines you want and convert the input string into an integer:
(global-set-key (kbd "M-<up>")
(lambda ()
(interactive)
(move-line (string-to-int (read-string "Lines: ")))))
I found this when I had the same problem, but I ended up solving it in another way.
(global-set-key (kbd "M-<down>") 'move-line)
(global-set-key (kbd "M-<up>") (kbd "C-u -1 M-<down>"))
Definitely not a perfect solution, since M-<down> could be reassigned and C-u -1 might not make sense on it, but since it's just my local init file, it should be no problem.
Also this obvious only works well for keyboard commands that you want to have reversed.
You might want to check out the "transpose-lines" built-in function.

What's in your .emacs?

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I've switched computers a few times recently, and somewhere along the way I lost my .emacs. I'm trying to build it up again, but while I'm at it, I thought I'd pick up other good configurations that other people use.
So, if you use Emacs, what's in your .emacs?
Mine is pretty barren right now, containing only:
Global font-lock-mode! (global-font-lock-mode 1)
My personal preferences with respect to indentation, tabs, and spaces.
Use cperl-mode instead of perl-mode.
A shortcut for compilation.
What do you think is useful?
Use the ultimate dotfiles site. Add your '.emacs' here. Read the '.emacs' of others.
My favorite snippet. The ultimate in Emacs eye candy:
;; real lisp hackers use the lambda character
;; courtesy of stefan monnier on c.l.l
(defun sm-lambda-mode-hook ()
(font-lock-add-keywords
nil `(("\\<lambda\\>"
(0 (progn (compose-region (match-beginning 0) (match-end 0)
,(make-char 'greek-iso8859-7 107))
nil))))))
(add-hook 'emacs-lisp-mode-hook 'sm-lambda-mode-hook)
(add-hook 'lisp-interactive-mode-hook 'sm-lamba-mode-hook)
(add-hook 'scheme-mode-hook 'sm-lambda-mode-hook)
So you see i.e. the following when editing lisp/scheme:
(global-set-key "^Cr" '(λ () (interactive) (revert-buffer t t nil)))
I have this to change yes or no prompt to y or n prompts:
(fset 'yes-or-no-p 'y-or-n-p)
I have these to start Emacs without so much "fanfare" which I got from this question.
(setq inhibit-startup-echo-area-message t)
(setq inhibit-startup-message t)
And Steve Yegge's function to rename a file that you're editing along with its corresponding buffer:
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
One thing that can prove very useful: Before it gets too big, try to split it into multiple files for various tasks: My .emacs just sets my load-path and the loads a bunch of files - I've got all my mode-specific settings in mode-configs.el, keybindings in keys.el, et cetera
My .emacs is only 127 lines, here are the most useful little snippets:
;; keep backup files neatly out of the way in .~/
(setq backup-directory-alist '(("." . ".~")))
This makes the *~ files which I find clutter up the directory go into a special directory, in this case .~
;; uniquify changes conflicting buffer names from file<2> etc
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
This sets up uniquify which changes those ugly file<2> etc. buffer names you get when multiple files have the same name into a much neater unambiguous name using as much of the whole path of the file as it has to.
That's about it... the rest is pretty standard stuff that I'm sure everyone knows about.
This is not the whole kit and kaboodle, but it is some of the more useful snippets I've gathered:
(defadvice show-paren-function (after show-matching-paren-offscreen
activate)
"If the matching paren is offscreen, show the matching line in the
echo area. Has no effect if the character before point is not of
the syntax class ')'."
(interactive)
(let ((matching-text nil))
;; Only call `blink-matching-open' if the character before point
;; is a close parentheses type character. Otherwise, there's not
;; really any point, and `blink-matching-open' would just echo
;; "Mismatched parentheses", which gets really annoying.
(if (char-equal (char-syntax (char-before (point))) ?\))
(setq matching-text (blink-matching-open)))
(if (not (null matching-text))
(message matching-text))))
;;;;;;;;;;;;;;;
;; UTF-8
;;;;;;;;;;;;;;;;;;;;
;; set up unicode
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
;; This from a japanese individual. I hope it works.
(setq default-buffer-file-coding-system 'utf-8)
;; From Emacs wiki
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
;; Wwindows clipboard is UTF-16LE
(set-clipboard-coding-system 'utf-16le-dos)
(defun jonnay-timestamp ()
"Spit out the current time"
(interactive)
(insert (format-time-string "%Y-%m-%d")))
(defun jonnay-sign ()
"spit out my name, email and the current time"
(interactive)
(insert "-- Jonathan Arkell (jonathana#criticalmass.com)")
(jonnay-timestamp))
;; Cygwin requires some seriosu setting up to work the way i likes it
(message "Setting up Cygwin...")
(let* ((cygwin-root "c:")
(cygwin-bin (concat cygwin-root "/bin"))
(gambit-bin "/usr/local/Gambit-C/4.0b22/bin/")
(snow-bin "/usr/local/snow/current/bin")
(mysql-bin "/wamp/bin/mysql/mysql5.0.51a/bin/"))
(setenv "PATH" (concat cygwin-bin ";" ;
snow-bin ";"
gambit-bin ";"
mysql-bin ";"
".;")
(getenv "PATH"))
(setq exec-path (cons cygwin-bin exec-path)))
(setq shell-file-name "bash")
(setq explicit-shell-file-name "bash")
(require 'cygwin-mount)
(cygwin-mount-activate)
(message "Setting up Cygwin...Done")
; Completion isn't perfect, but close
(defun my-shell-setup ()
"For Cygwin bash under Emacs 20+"
(setq comint-scroll-show-maximum-output 'this)
(setq comint-completion-addsuffix t)
(setq comint-eol-on-send t)
(setq w32-quote-process-args ?\")
(make-variable-buffer-local 'comint-completion-addsuffix))
(setq shell-mode-hook 'my-shell-setup)
(add-hook 'emacs-startup-hook 'cygwin-shell)
; Change how home key works
(global-set-key [home] 'beginning-or-indentation)
(substitute-key-definition 'beginning-of-line 'beginning-or-indentation global-map)
(defun yank-and-down ()
"Yank the text and go down a line."
(interactive)
(yank)
(exchange-point-and-mark)
(next-line))
(defun kill-syntax (&optional arg)
"Kill ARG sets of syntax characters after point."
(interactive "p")
(let ((arg (or arg 1))
(inc (if (and arg (< arg 0)) 1 -1))
(opoint (point)))
(while (not (= arg 0))
(if (> arg 0)
(skip-syntax-forward (string (char-syntax (char-after))))
(skip-syntax-backward (string (char-syntax (char-before)))))
(setq arg (+ arg inc)))
(kill-region opoint (point))))
(defun kill-syntax-backward (&optional arg)
"Kill ARG sets of syntax characters preceding point."
(interactive "p")
(kill-syntax (- 0 (or arg 1))))
(global-set-key [(control shift y)] 'yank-and-down)
(global-set-key [(shift backspace)] 'kill-syntax-backward)
(global-set-key [(shift delete)] 'kill-syntax)
(defun insert-file-name (arg filename)
"Insert name of file FILENAME into buffer after point.
Set mark after the inserted text.
Prefixed with \\[universal-argument], expand the file name to
its fully canocalized path.
See `expand-file-name'."
;; Based on insert-file in Emacs -- ashawley 2008-09-26
(interactive "*P\nfInsert file name: ")
(if arg
(insert (expand-file-name filename))
(insert filename)))
(defun kill-ring-save-filename ()
"Copy the current filename to the kill ring"
(interactive)
(kill-new (buffer-file-name)))
(defun insert-file-name ()
"Insert the name of the current file."
(interactive)
(insert (buffer-file-name)))
(defun insert-directory-name ()
"Insert the name of the current directory"
(interactive)
(insert (file-name-directory (buffer-file-name))))
(defun jonnay-toggle-debug ()
"Toggle debugging by toggling icicles, and debug on error"
(interactive)
(toggle-debug-on-error)
(icicle-mode))
(defvar programming-modes
'(emacs-lisp-mode scheme-mode lisp-mode c-mode c++-mode
objc-mode latex-mode plain-tex-mode java-mode
php-mode css-mode js2-mode nxml-mode nxhtml-mode)
"List of modes related to programming")
; Text-mate style indenting
(defadvice yank (after indent-region activate)
(if (member major-mode programming-modes)
(indent-region (region-beginning) (region-end) nil)))
I have a lot of others that have already been mentioned, but these are absolutely necessary in my opinion:
(transient-mark-mode 1) ; makes the region visible
(line-number-mode 1) ; makes the line number show up
(column-number-mode 1) ; makes the column number show up
You can look here: http://www.dotemacs.de/
And my .emacs is pretty long to put it here as well, so it will make the answer not too readable. Anyway, if you wish I can sent it to you.
Also I would recomend you to read this: http://steve.yegge.googlepages.com/my-dot-emacs-file
Here are some key mappings that I've become dependent upon:
(global-set-key [(control \,)] 'goto-line)
(global-set-key [(control \.)] 'call-last-kbd-macro)
(global-set-key [(control tab)] 'indent-region)
(global-set-key [(control j)] 'join-line)
(global-set-key [f1] 'man)
(global-set-key [f2] 'igrep-find)
(global-set-key [f3] 'isearch-forward)
(global-set-key [f4] 'next-error)
(global-set-key [f5] 'gdb)
(global-set-key [f6] 'compile)
(global-set-key [f7] 'recompile)
(global-set-key [f8] 'shell)
(global-set-key [f9] 'find-next-matching-tag)
(global-set-key [f11] 'list-buffers)
(global-set-key [f12] 'shell)
Some other miscellaneous stuff, mostly for C++ development:
;; Use C++ mode for .h files (instead of plain-old C mode)
(setq auto-mode-alist (cons '("\\.h$" . c++-mode) auto-mode-alist))
;; Use python-mode for SCons files
(setq auto-mode-alist (cons '("SConstruct" . python-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("SConscript" . python-mode) auto-mode-alist))
;; Parse CppUnit failure reports in compilation-mode
(require 'compile)
(setq compilation-error-regexp-alist
(cons '("\\(!!!FAILURES!!!\nTest Results:\nRun:[^\n]*\n\n\n\\)?\\([0-9]+\\)) test: \\([^(]+\\)(F) line: \\([0-9]+\\) \\([^ \n]+\\)" 5 4)
compilation-error-regexp-alist))
;; Enable cmake-mode from http://www.cmake.org/Wiki/CMake_Emacs_mode_patch_for_comment_formatting
(require 'cmake-mode)
(setq auto-mode-alist
(append '(("CMakeLists\\.txt\\'" . cmake-mode)
("\\.cmake\\'" . cmake-mode))
auto-mode-alist))
;; "M-x reload-buffer" will revert-buffer without requiring confirmation
(defun reload-buffer ()
"revert-buffer without confirmation"
(interactive)
(revert-buffer t t))
To refresh the webpage you're editing from within Emacs
(defun moz-connect()
(interactive)
(make-comint "moz-buffer" (cons "127.0.0.1" "4242"))
(global-set-key "\C-x\C-g" '(lambda ()
(interactive)
(save-buffer)
(comint-send-string "*moz-buffer*" "this.BrowserReload()\n"))))
Used in combination with http://hyperstruct.net/projects/mozlab
You can find my configuration (both in html & in tar'ed archive) on my site. It contains lot of settings for different modes
This block is the most important for me:
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
I've never been clear on the difference between those, though. Cargo cult, I guess...
I try to keep my .emacs organized. The configuration will always be a work in progress, but I'm starting to be satisfied with the overall structure.
All stuff is under ~/.elisp, a directory that is under version control (I use git, if that's of interest). ~/.emacs simply points to ~/.elisp/dotemacs which itself just loads ~/.elisp/cfg/init. That file in turn imports various configuration files via require. This means that the configuration files need to behave like modes: they import stuff they depend on and they provide themselves at the end of the file, e.g. (provide 'my-ibuffer-cfg). I prefix all identifiers that are defined in my configuration with my-.
I organize the configuration in respect to modes/subjects/tasks, not by their technical implications, e.g. I don't have a separate config file in which all keybindings or faces are defined.
My init.el defines the following hook to make sure that Emacs recompiles configuration files whenever saved (compiled Elisp loads a lot faster but I don't want to do this step manually):
;; byte compile config file if changed
(add-hook 'after-save-hook
'(lambda ()
(when (string-match
(concat (expand-file-name "~/.elisp/cfg/") ".*\.el$")
buffer-file-name)
(byte-compile-file buffer-file-name))))
This is the directory structure for ~/.elisp:
~/.elisp/todo.org: Org-mode file in which I keep track of stuff that still needs to be done (+ wish list items).
~/.elisp/dotemacs: Symlink target for ~/.emacs, loads ~/.elisp/cfg/init.
~/.elisp/cfg: My own configuration files.
~/.elisp/modes: Modes that consist only of a single file.
~/.elisp/packages: Sophisticated modes with lisp, documentation and probably resource files.
I use GNU Emacs, that version does not have real support for packages. Therefore I organize them manually, usually like this:
~/.elisp/packages/foobar-0.1.3 is the root directory for the package. Subdirectory lisp holds all the lisp files and info is where the documentation goes. ~/.elisp/packages/foobar is a symlink that points to the currently used version of the package so that I don't need to change my configuration files when I update something. For some packages I keep an ~/.elisp/packages/foobar.installation file around in which I keep notes about the installation process. For performance reasons I compile all elisp files in newly installed packages, should this not be the case by default.
Here's a couple of my own stuff:
Inserts date in ISO 8601 format:
(defun insertdate ()
(interactive)
(insert (format-time-string "%Y-%m-%d")))
(global-set-key [(f5)] 'insertdate)
For C++ programmers, creates a class skeleton (class's name will be the same as the file name without extension):
(defun createclass ()
(interactive)
(setq classname (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
(insert
"/**
* " classname".h
*
* Author: Your Mom
* Modified: " (format-time-string "%Y-%m-%d") "
* Licence: GNU GPL
*/
#ifndef "(upcase classname)"
#define "(upcase classname)"
class " classname "
{
public:
"classname"();
~"classname"();
private:
};
#endif
"))
Automatically create closing parentheses:
(setq skeleton-pair t)
(setq skeleton-pair-on-word t)
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "{") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "<") 'skeleton-pair-insert-maybe)
i use paredit for easy (e)lisp handling and ido-mode minibuffer completions.
It's hard to answer this question, because everyone uses Emacs for very different purposes.
Further more, a better practice may be to KISS your dotemacs. Since the Easy Customization Interface is widely supported amongst Emacs' modes, you should store all your customization in your custom-file (which may be a separate place in your dotemacs), and for the dotemacs, put in it only load path settings, package requires, hooks, and key bindings. Once you start using Emacs Starter Kit, a whole useful bunch of settings may removed from your dotemacs, too.
See EmacsWiki's DotEmacs category. It provides lots of links to pages addressing this question.
(put 'erase-buffer 'disabled nil)
(put 'downcase-region 'disabled nil)
(set-variable 'visible-bell t)
(set-variable 'tool-bar-mode nil)
(set-variable 'menu-bar-mode nil)
(setq load-path (cons (expand-file-name "/usr/share/doc/git-core/contrib/emacs") load-path))
(require 'vc-git)
(when (featurep 'vc-git) (add-to-list 'vc-handled-backends 'git))
(require 'git)
(autoload 'git-blame-mode "git-blame"
"Minor mode for incremental blame for Git." t)
I set up some handy shortcuts to web pages and searches using webjump
(require 'webjump)
(global-set-key [f2] 'webjump)
(setq webjump-sites
(append '(
("Reddit Search" .
[simple-query "www.reddit.com" "http://www.reddit.com/search?q=" ""])
("Google Image Search" .
[simple-query "images.google.com" "images.google.com/images?hl=en&q=" ""])
("Flickr Search" .
[simple-query "www.flickr.com" "flickr.com/search/?q=" ""])
("Astar algorithm" .
"http://www.heyes-jones.com/astar")
)
webjump-sample-sites))
Blog post about how this works here
http://justinsboringpage.blogspot.com/2009/02/search-reddit-flickr-and-google-from.html
Also I recommend these:
(setq visible-bell t) ; no beeping
(setq transient-mark-mode t) ; visually show region
(setq line-number-mode t) ; show line numbers
(setq global-font-lock-mode 1) ; everything should use fonts
(setq font-lock-maximum-decoration t)
Also I get rid of some of the superfluous gui stuff
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1)))
One line to amend the load path
One line to load my init library
One line to load my emacs init files
Of course, the "emacs init files" are quite numerous, one per specific thing, loaded in a deterministic order.
emacs-starter-kit as a base, then I've added.. vimpulse.el, whitespace.el, yasnippet, textmate.el and newsticker.el.
In my ~/.emacs.d/$USERNAME.el (dbr.el) file:
(add-to-list 'load-path (concat dotfiles-dir "/vendor/"))
;; Snippets
(add-to-list 'load-path "~/.emacs.d/vendor/yasnippet/")
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "~/.emacs.d/vendor/yasnippet/snippets")
;; TextMate module
(require 'textmate)
(textmate-mode 'on)
;; Whitespace module
(require 'whitespace)
(add-hook 'ruby-mode-hook 'whitespace-mode)
(add-hook 'python-mode-hook 'whitespace-mode)
;; Misc
(flyspell-mode 'on)
(setq viper-mode t)
(require 'viper)
(require 'vimpulse)
;; IM
(eval-after-load 'rcirc '(require 'rcirc-color))
(setq rcirc-default-nick "_dbr")
(setq rcirc-default-user-name "_dbr")
(setq rcirc-default-user-full-name "_dbr")
(require 'jabber)
;;; Google Talk account
(custom-set-variables
'(jabber-connection-type (quote ssl))
'(jabber-network-server "talk.google.com")
'(jabber-port 5223)
'(jabber-server "mysite.tld")
'(jabber-username "myusername"))
;; Theme
(color-theme-zenburn)
;; Key bindings
(global-set-key (kbd "M-z") 'undo)
(global-set-key (kbd "M-s") 'save-buffer)
(global-set-key (kbd "M-S-z") 'redo)
Always save my config in svn http://my-trac.assembla.com/ez-conf/browser/emacs.d
After reading this, I figured it would be good to have a simple site just for the best .emacs modifications. Feel free to post and vote on them here:
http://dotemacs.slinkset.com/
https://b7j0c.org/stuff/dotemacs.html
I'm new to emacs, in my .emacs file there are
indentation configuration
color theme
php mode, coffee mode and js2 mode
ido mode
FWIW, my .emacs is here:
http://svn.red-bean.com/repos/kfogel/trunk/.emacs
lots of stuff: https://github.com/tavisrudd/emacs.d
el-get has made managing it and dependencies a lot easier: https://github.com/tavisrudd/emacs.d/blob/master/dss-init-el-get.el
For Scala coders
;; Load the ensime lisp code... http://github.com/aemoncannon/ensime
(add-to-list 'load-path "ENSIME_ROOT/elisp/")
(require 'ensime)
;; This step causes the ensime-mode to be started whenever ;; scala-mode is started for a buffer. You may have to customize this step ;; if you're not using the standard scala mode.
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
;; MINI HOWTO: ;; Open .scala file. M-x ensime (once per project)
My emacs configuration has grown up pretty big over the years and I have lot of useful stuff for me there but if I have two functions it probably would have been those ones.
Define C-x UP and C-x DOWN to move the current line or down keeping the cursor at the right place :
;Down/UP the current line
(global-set-key '[(control x) (up)] 'my-up-line)
(global-set-key '[(control x) (down)] 'my-down-line)
(defun my-down-line()
(interactive)
(let ((col (current-column)))
(forward-line 1)
(transpose-lines 1)
(forward-line -1)
(forward-char col)
)
)
(defun my-up-line()
(interactive)
(let ((col (current-column)))
(transpose-lines 1)
(forward-line -2)
(forward-char col)
)
)