This page shows some less known emacs tips.
Before continuing, here are 4 commands every serious emacs user should know. I used them about daily. (And they are all listed under the graphical menu 〖Help〗)
describe-function【F1 f】. This also gives the inline documentation of the command.
apropos-command【F1 a】. This lists all commands of a given string.
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?
(for detail, see: List Matching Lines and Delete Matching Lines in Emacs.)
How to highlight all occurrences of word in a file?
How to sort lines?
reverse-region to reverse order in region by lines.
To sort lines by a specific column, use
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?
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?
copy-to-register【Ctrl+x r s】, give it a single character name as “1” or “a”.
insert-register【Ctrl+x r i】.
If your text is frequently needed (⁖ XML template or signature), you should set up a abbreviation. See: Using Emacs Abbrev Mode for Abbreviation.
How to avoid restarting emacs when adding code to emacs init file?
eval-bufferto evaluate all code.
load-file. Or, in dired, press 【L】 on the file name.
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.
How to “refresh” a opened file to its current saved state? For example, other people have edited the file thru a network.
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.
I have a “.info” file. How to open it as info?
universal-argument 【Ctrl+u】, then call
info 【F1 i】, then the info file name.
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
How to set the background color so that all new “frames” will have that color? (not set-background-color)
customize-variable then give “default-frame-alist”.
If you want to code elisp manually, call
describe-variable on “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