Xah Lee, 2006-04, 2008, 2009-09-10
This page shows some less known emacs tips.
Before continuing, here are some things every serious emacs user should know. I used them about daily. (And they are all listed under the graphical menu Help)
• To find the command name of a keybinding, use the command describe-key (Ctrl+h k).
• To find the keybinding of a command, use describe-function (Ctrl+h f). This also gives the inline documentation of the command.
• If you forgot the name of a command, you can use apropos-command (Ctrl+h a), which lists all commands of a given string.
• In any mode, if you want to see its documentation, use describe-mode (Ctrl+h m).
How to search the last searched string?
Type “Ctrl+s Ctrl+s” to search the same word searched last time.
How to search the string under cursor without typing it?
Type “Ctrl+s Ctrl+w” to search the word under the cursor. You can type “Ctrl+w” multiple times to expand the word selection.
How to list lines containing a specific string?
list-matching-lines. There's also delete-matching-lines and delete-non-matching-lines.
How to highlight all occurrences of word in a file?
Call highlight-phrase, highlight-regexp, highlight-lines-matching-regexp.
How to sort lines?
Use sort-lines. Use reverse-region to reverse order in region by lines.
To sort lines by a specific column, use sort-fields or sort-numeric-fields. (“fields” are separated by space or tabs. First field is 1, not 0.)
For example: Type “Ctrl+u”, then 2, then “Alt+x sort-field”. This will sort the line by the 2nd column, and the fields are compared as strings.
How to delete trailing white spaces in source code?
delete-trailing-whitespace does it to the whole buffer.
How to make spaces and tabs visible?
Call whitespace-mode, available in emacs 23. See: How To Use And Setup Emacs's whitespace-mode.
I have a set of different texts i want to paste frequently. Is there a way to use like multiple clipboards?
Yes. Select the region, then “Alt+x copy-to-register”, give it a name such as 1. To paste it, type “Alt+x insert-register”.
You can create elisp code so that typing F7 will automatically copy to register 1, and F8 will automatically paste from register 1. So, F7 and F8 becomes another copy and paste without changing emacs's kill-ring. Here's the code:
(defun copy-to-register-1 (p1 p2) "Copy text selection to register named “1”." (interactive "r") (copy-to-register ?1 p1 p2 ) ) (defun insert-register-content-1 () "Insert register named “1”'s content." (interactive) (insert-register ?1) ) (global-set-key (kbd "<f7>") 'copy-to-register-1) (global-set-key (kbd "<f8>") 'insert-register-content-1)
If your text is frequently needed long term, such as a XML template or signature, you can define a function and bind it to a key, so that it inserts the string when you press a button. Here's a example:
(defun insert-my-sig () "Insert my signature." (interactive) (insert "-- Mary http://example.org/")) (global-set-key (kbd "<f8>") 'insert-my-sig)
Put the above in your emacs init file.
How to start a second shell?
To start a second shell, give it a empty argument, like this: “Ctrl+u Alt+x shell”. In emacs, many commands take a empty argument for a slightly changed behavior.
I have a “.info” file. How to open it as info?
Type “Ctrl+u Alt+x info” then the info file name.
How to “refresh” a opened file to its current saved state? For example, other people have edited the file thru a network.
Call revert-buffer. If someone modified the file, typically emacs will auto detect it, and when you type something, emacs will automatically ask you what to do.
Is there a way for emacs to interpret the file as hex-decimal? (i.e. byte-code editor)
To open the file as hex, call hexl-find-file. If the file is already opened, call hexl-mode.
How to set the background color so that all new “frames” will have that color? (not set-background-color)
“Alt+x customize-variable Enter default-frame-alist Enter”
If you want to code elisp manually, see Ctrl+h v default-frame-alist. Here's a example you can put in your emacs init file:
(setq default-frame-alist
'((tool-bar-lines . 0)
(menu-bar-lines . 1)
(background-color . "cornsilk")))
To see a list of color names emacs support, call list-colors-display.
Many emacs's commands have long names. You can alias frequently used commandsto shorter names.
For example, here is some of my aliases:
; get rid of prompt that forces you to type full “yes” or “no”. ; y or n is enough (defalias 'yes-or-no-p 'y-or-n-p) ; shortening of often used commands (defalias 'gf 'grep-find) (defalias 'fd 'find-dired) (defalias 'sh 'shell) (defalias 'qrr 'query-replace-regexp) (defalias 'lml 'list-matching-lines) (defalias 'dml 'delete-matching-lines) (defalias 'rof 'recentf-open-files) (defalias 'eb 'eval-buffer) (defalias 'er 'eval-region) (defalias 'ee 'eval-expression)
The default keyboard shortcut for execute-extended-command is “Alt+x”. You can define “Alt+a”, which makes it on the home row. (See A Ergonomic Keyboard Shortcut Layout For Emacs)
(global-set-key (kbd "M-a") 'execute-extended-command) ; was backward-sentence