I have enough random emacs customizations to put up another post, so here they are. Emacs is a great text editor, but a lot of the functionality that I have discovered is not enabled by default or not existing, but it is fortunately incredibly customizable so I can add it myself, or find someone else and use their customizations. A lot of these were taken from the Emacs Starter Kit - I browsed through their codebase and used the pieces that looked interesting.
(global-set-key (kbd "C-c r") 'revert-buffer) (global-set-key (kbd "C-c a") 'apropos) (global-set-key (kbd "C-c v") 'visual-line-mode) (define-key isearch-mode-map (kbd "C-o") (lambda () (interactive) (let ((case-fold-search isearch-case-fold-search)) (occur (if isearch-regexp isearch-string (regexp-quote isearch-string))))))
These are some keybindings for commands I use pretty frequently, as well as an additional command for isearch-mode. Revert-buffer does just as advertised; it re-loads the current buffer from what is saved on disk. Apropos, a very powerful help command, will search all defined symbols for the pattern you specify. Visual-line-mode is the Emacs23 version of longlines-mode, except much faster; it will make lines wrap and not extend past the window. I often change whether I want to be in this mode or not, so I made C-c v toggle this mode. The last keybinding makes C-o call occur with the string you are searching for when you are performing an interactive search.
(setq-default tab-width 4) (setq comment-auto-fill-only-comments t) (put 'downcase-region 'disabled nil) (put 'upcase-region 'disabled nil)
These are just some standard Emacs customizations. I set the tab-width to be 4 in all buffers and have auto-fill only work with comments in programming modes. The next two lines just enable two disabled commands that I sometimes use; It gets annoying to specify that I want to do these each time.
(defun uwb () "Untabifies the whole buffer" (interactive) (untabify (point-min) (point-max))) (defun twb () "Tabifies the whole buffer" (interactive) (tabify (point-min) (point-max))) (defun eval-and-replace () "Replace the preceding sexp with its value." (interactive) (backward-kill-sexp) (condition-case nil (prin1 (eval (read (current-kill 0))) (current-buffer)) (error (message "Invalid expression") (insert (current-kill 0))))) (global-set-key (kbd "C-c e") 'eval-and-replace) (defun shell-current-directory ( ) "Opens a shell in the current directory" (interactive) (shell (concat "shell-" default-directory "-shell" ))) (global-set-key (kbd "C-c s") 'shell-current-directory)
uwb stands for Untabify whole buffer, and is used when I have a programming file that I quickly need to standardize to keep from hooking messed up. Since it isn’t something I have to do often, I don’t need a keybinding for it. Similarly, twb tabifies the entire buffer. Eval-and-replace is similar to eval-last-sexp, a function I use quite frequently, except it replaces the evaluated expression with the result. This is something that will be useful to me; often I ended up doing this task manually, so it gets it’s own keybinding. The last command is one I now use pretty much constantly. shell-current-directory just creates a shell in the directory that the current buffer is in.This allows you to quickly pop into a shell without having to navigate to that directory, or do an eval-expression yourself. The one thing that I may end up modifying this to do is have the buffer name change based on the directory of the shell, since sometimes I wander off in the shell I opened and then the name is not that of the current directory.
(add-hook 'find-file-hook (lambda () (if (string-match "log" (buffer-name (current-buffer))) (visual-line-mode -1))))
At work, I often deal with large logfiles with long lines, and I usually want to quickly scan through them. Not having visual-line-mode on in this case is very helpful, since I can just scan through the log type with one per column and see if there is an error. There’s a couple of other modifications I’ve been thinking of writing and packaging up as a log-file-mode, since I haven’t been able to find one of my own, but I have not gotten around to this yet.
The next set of modifications isn’t actually useful; they remove functionality from emacs instead of increasing it. I noticed I use the arrow keys more than I should, and want to train myself to use C-n and the like and not leave the home row as often. To this end, I disabled the arrow keys.
(global-unset-key (kbd "<left>") ) (global-unset-key (kbd "<right>") ) (global-unset-key (kbd "<up>") ) (global-unset-key (kbd "<down>") ) (global-unset-key (kbd "C-<left>") ) (global-unset-key (kbd "C-<right>") ) (global-unset-key (kbd "C-<up>") ) (global-unset-key (kbd "C-<down>") ) (global-unset-key (kbd "M-<left>") ) (global-unset-key (kbd "M-<right>") ) (global-unset-key (kbd "M-<up>") ) (global-unset-key (kbd "M-<down>") )
That’s it for now - let me know if there are any good packages for viewing log files, and if you have any suggestions for additional customizations please leave them in the comments.