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.
Tags: emacs
This is a great list of bindings, especially the unbidnings.
I don’t understand eval-and-replace. I understood what it does, but I can’t ever imagine needing to use it. I often use C-u C-x C-e, but would never imagine deleting code I wrote. What’s the situation?
I really like the key unbinding. I use the arrows to much too. But I don’t have your confidence so I wanted a safety net. I just created the following function to toggle the arrow keys on and off.
(defun my-toggle-arrow-key-functions (&optional arg)
“Enable or disable the arrow keys.
Called with no arguments disables the arrow keys.
Called with a non nil argument enables the arrow keys.
Used to force yourself to keep your hands on the home row for
basic navigation.”
(interactive “P”)
(cond ((null arg)
(global-unset-key (kbd “”))
(global-unset-key (kbd “”))
(global-unset-key (kbd “”))
(global-unset-key (kbd “”))
(global-unset-key (kbd “C-”))
(global-unset-key (kbd “C-”))
(global-unset-key (kbd “C-”))
(global-unset-key (kbd “C-”))
(global-unset-key (kbd “M-”))
(global-unset-key (kbd “M-”))
(message “Arrow keys disabled”))
(t
(global-set-key (kbd “”) ‘backward-char)
(global-set-key (kbd “”) ‘forward-char)
(global-set-key (kbd “”) ‘previous-line)
(global-set-key (kbd “”) ‘next-line)
(global-set-key (kbd “C-”) ‘backward-word)
(global-set-key (kbd “C-”) ‘forward-word)
(global-set-key (kbd “C-”) ‘backward-paragraph)
(global-set-key (kbd “C-”) ‘forward-paragraph)
(global-set-key (kbd “M-”) ‘backward-word)
(global-set-key (kbd “M-”) ‘forward-word)
(message “Arrow keys enabled” ))))
Then I place (my-toggle-arrow-key-functions) in my .emacs file.
When I want the arrow keys back I just type “C-1 M-x my-toggle-arrow-key-functions ” and the arrow keys are back untill i type “M-x my-toggle-arrow-key-functions “.
Well that didn’t work! I guess I’ve got to escape the angle brackets.
How?
like this “\”?
how ’bout “<right>”
I really like the key unbinding. I use the arrows to much too. But I don’t have your confidence so I wanted a safety net. I just created the following function to toggle the arrow keys on and off.
(defun my-toggle-arrow-key-functions (&optional arg) "Enable or disable the arrow keys. Called with no arguments disables the arrow keys. Called with a non nil argument enables the arrow keys. Used to force yourself to keep your hands on the home row for basic navigation." (interactive "P") (cond ((null arg) (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>")) (message "Arrow keys disabled")) (t (global-set-key (kbd "<left>") 'backward-char) (global-set-key (kbd "<right>") 'forward-char) (global-set-key (kbd "<up>") 'previous-line) (global-set-key (kbd "<down>") 'next-line) (global-set-key (kbd "C-<left>") 'backward-word) (global-set-key (kbd "C-<right>") 'forward-word) (global-set-key (kbd "C-<up>") 'backward-paragraph) (global-set-key (kbd "C-<down>") 'forward-paragraph) (global-set-key (kbd "M-<left>") 'backward-word) (global-set-key (kbd "M-<right>") 'forward-word) (message "Arrow keys enabled" ))))Then I place (my-toggle-arrow-key-functions) in my .emacs file.
When I want the arrow keys back I just type “C-1 M-x my-toggle-arrow-key-functions ” and the arrow keys are back untill i type “M-x my-toggle-arrow-key-functions “.