Emacs Less-known Tips

Advertise Here

, , …,

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〗)

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.

Grep Lines

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?

Call highlight-phrase, highlight-regexp, highlight-lines-matching-regexp.

How to sort lines?

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-field】. This will sort the line by the 2nd column, and the fields are compared as strings.

Other

How to delete trailing white spaces in source code?

Call 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?

If your text is frequently needed (e.g. XML template or signature), you should set up a abbreviation. See: Using Emacs's Abbrev Mode for Abbreviation.

Misc

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

For simple code, you can just select the code and call eval-region.

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.

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.

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

Call universal-argumentCtrl+u】, then call infoF1 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.

Display

How to set the background color so that all new “frames” will have that color? (not set-background-color)

Call 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 list-colors-display.

blog comments powered by Disqus