Emacs Less-known Tips

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

Alt + x describe-key → Find the command name of a keyboard shortcut.

+ → Find the command name of a keyboard shortcut. Alt + x describe-function → Find the keyboard shortcut of a command.

+ → Find the keyboard shortcut of a command. Alt + x apropos-command → Search command by name.

+ → Search command by name. Alt + x describe-mode → Show current mode's documentation.

For example, Alt + x 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.

list lines containing a string

Alt + x list-matching-lines

+ Alt + x delete-matching-lines

+ Alt + x delete-non-matching-lines

+ Alt + x delete-duplicate-lines (Emacs 24.4)

[see Emacs: List/Highlight/Delete Matching Lines, Sort Reverse]

highlight all occurrences of word

Alt + x highlight-phrase

+ Alt + x highlight-regexp

+ Alt + x highlight-lines-matching-regexp

[see Emacs: Search Text in Buffer]

sort lines

Select a region first, then Alt + x sort-lines .

Alt + x reverse-region → reverse order in region by lines.

sort-fields → sort lines by a specific column, as string. (“fields” are separated by space or tabs. First field is 1, not 0.)

sort-numeric-fields → sort lines by a specific column, as number.

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.

delete trailing white spaces

Alt + x delete-trailing-whitespace does it to the whole buffer.

[see Emacs: Delete Trailing Whitespace]

make spaces and tabs visible

Alt + x whitespace-mode .

[see Emacs: Make Whitespaces Visible]

[see Emacs: Tab/Space/Indent Setup]

multiple clipboards

if you have a set of different texts i want to paste frequently, you can use multiple clipboards.

Select the region, then Alt + x copy-to-register 【 Ctrl + x r s 】, give it a single character name as “1” or “a”.

+ 【 + 】, give it a single character name as “1” or “a”. To paste it, Alt + x insert-register 【 Ctrl + x r i 】.

[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 by Commands.

no restarting emacs when add to init file

Many ways.

Just select the new emacs lisp code, then Alt + x eval-region .

+ . Alt + x eval-buffer to evaluate all code in current file.

+ to evaluate all code in current file. Alt + x load-file . Or, in dired, press L ( dired-do-load ) on the file name. [see dired tutorial]

[see Evaluate Emacs Lisp Code]

start a second shell

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

“refresh” a opened file to its saved state

Alt + x 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.

(global-auto-revert-mode 1)

How to open .info file as info?

Alt + x universal-argument 【 Ctrl + u 】, then Alt + x info 【 Ctrl + h i 】, then the info file name.

open file as hex-decimal (byte-code editor)

To open the file as hex, Alt + x hexl-find-file . If the file is already opened, Alt + x hexl-mode .

[see Emacs: Set Default Major Mode]

set the background color

Put this in your emacs init file:

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

To see a list of color names emacs support, Alt + x list-colors-display .

[see Emacs: Set Default Window Size]