Emacs Less-known Tips

By Xah Lee. Date: . Last updated: .

First, here are 4 commands every emacs user should know.

For example, press 【Alt+x】, then type describe-key, Enter ↵, then press 【Ctrl+f】. Emacs will then tell you the commad name corresponding to the key you just pressed.

Now try the other commands above.

Every emacs user should memorize the above 4 commands.

Editing Related

Interactive Search

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.

〔►see Emacs: Search / Highlight Words

Grep Lines

How to list lines containing a specific string?

〔►see Emacs: List/Delete/Highlight Matching Lines, Sort/Reverse Lines

How to highlight all occurrences of word in a file?

〔►see Emacs: Search / Highlight Words

How to sort lines?

Select a region first, then call sort-lines. Call 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-numeric-fields】. This will sort the line by the 2nd column, and the fields are compared as numbers.


How to delete trailing white spaces in source code?

Call delete-trailing-whitespace does it to the whole buffer.

〔►see Emacs: Delete Trailing Whitespace

How to make spaces and tabs visible?

Call whitespace-mode. 〔►see How to Use And Setup Emacs's whitespace-mode

〔►see Emacs: Tabs, Space, Indentation Setup

I have a set of different texts i want to paste frequently. Is there a way to use like multiple clipboards?

〔►see Emacs: Using Registers (Multiple Clipboards)

If your text is frequently needed (For example, XML template or signature), you should set up a abbreviation. See: Emacs: Abbrev Mode Tutorial.


How to avoid restarting emacs when adding code to emacs init file?

Many ways.

〔►see How to Evaluate Emacs Lisp Code

How to start a second shell?

To start a second shell, call universal-argumentCtrl+u】 first, like this: 【Ctrl+u Alt+x shell】.

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.

;; set file to auto refresh when change detected (For example, changed by other)
(global-auto-revert-mode 1)

I have a “.info” file. How to open it as info?

Call universal-argumentCtrl+u】, then call infoCtrl+h 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 hexl-mode.

〔►see Emacs: Set File to Open in a Major Mode


How to set the background color for new windows?

Put this in your emacs init file:

(setq default-frame-alist
  '((background-color . "cornsilk")))

To see a list of color names emacs support, call list-colors-display.

〔►see Emacs: Set Default Window (frame) Size

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs