Sie sind auf Seite 1von 4

Emacs Less-known Tips

http://xahlee.org/emacs/emacs_esoteric.html

Home Math Computing Arts Words Literature Music

Emacs Tutorial

Emacs Less-known Tips


Xah Lee, 2006-04, 2008, 2009-09-10 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)

YOUR AD HERE
k .

To nd the command name of a keybinding, call command describe-key Ctrl + h To nd the keybinding of a command, call describe-function Ctrl + h inline documentation of the command.
f .

This also gives the which lists all


m .

If you forgot the name of a command, call apropos-command Ctrl + h commands of a given string.

a ,

In any mode, if you want to see its inline documentation, call describe-mode Ctrl + h

Editing Related
Interactive Search

Q:

How to search the last searched string?


A: Type Ctrl + s
Ctrl + s

to search the same word searched last time.

Q:

How to search the string under cursor without typing it?


A: 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

Q:

How to list lines containing a specic string?


A: list-matching-lines. There's also delete-matching-lines and deletenon-matching-lines. (for detail, see: List Matching Lines and Delete Matching Lines in Emacs.)

Q:

How to highlight all occurrences of word in a le?


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

Q:

How to sort lines?


A: Use sort-lines. Use reverse-region to reverse order in region by lines. To sort lines by a specic column, use sort-elds or sort-numeric-elds. (elds are separated by space or tabs. First eld is 1, not 0.)

1 of 4

Sunday 30 January 2011 06:10 AM

Emacs Less-known Tips

http://xahlee.org/emacs/emacs_esoteric.html

For example: Type Ctrl + u , then 2 , then Alt + x sort-eld. This will sort the line by the 2nd column, and the elds are compared as strings. Emacs Tutorial

Other

Q:

How to delete trailing white spaces in source code?


A: delete-trailing-whitespace does it to the whole buer.

Q:

How to make spaces and tabs visible?


A: Call whitespace-mode, available in emacs 23. See: How To Use And Setup Emacs's whitespace-mode.

Q:

I have a set of dierent texts i want to paste frequently. Is there a way to use like multiple clipboards?
A: 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 dene 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 "-Jane http://example.org/")) (global-set-key (kbd "<f8>") 'insert-my-sig)

Put the above in your emacs init le.

Misc

2 of 4

Sunday 30 January 2011 06:10 AM

Emacs Less-known Tips

http://xahlee.org/emacs/emacs_esoteric.html

Q:

How to start a second shell?

Emacs Tutorial

A: 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 le to its current saved state? For example, other people have edited the le thru a network.
A: Call revert-buer. If someone modied the le, typically emacs will auto detect it, and when you type something, emacs will automatically ask you what to do.

Q:

Q:

I have a .info le. How to open it as info?


A: Type Ctrl + u
Alt + x

info then the info le name.

Q:

Is there a way for emacs to interpret the le as hex-decimal? (i.e. byte-code editor)
A: To open the le as hex, call hexl-nd-le. If the le is already opened, call hexl-mode.

Display

Q: How to set the background color so that all new frames will have that color? (not set-background-color)
A: Alt + x customize-variable
Enter

default-frame-alist
v

Enter

If you want to code elisp manually, see Ctrl + h example you can put in your emacs init le:
(setq default-frame-alist '((tool-bar-lines . 0) (menu-bar-lines . 1) (background-color . "cornsilk")))

default-frame-alist. Here's a

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

Alias Long Commands to Shorter Names


Many emacs's commands have long names. You can alias frequently used commands to 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

3 of 4

Sunday 30 January 2011 06:10 AM

Emacs Less-known Tips


(defalias 'gf 'grep-find) (defalias 'fd 'find-dired) (defalias 'sh 'shell) (defalias (defalias (defalias (defalias 'qrr 'lml 'dml 'rof 'query-replace-regexp) 'list-matching-lines) 'delete-matching-lines) 'recentf-open-files)

http://xahlee.org/emacs/emacs_esoteric.html

Emacs Tutorial

(defalias 'eb 'eval-buffer) (defalias 'er 'eval-region)

The default keyboard shortcut for execute-extended-command is Alt + x . You can dene Alt + a , which makes it on the home row. (See ErgoEmacs Keybinding)

Was this page useful to you?

(global-set-key (kbd "M-a") 'execute-extended-command) ; was backward-sentence

You like: Tips on Long Term Emacs Productivity Emacs Lisp Basics How to Dene Keyboard Shortcuts in Emacs

Add New Comment


T ype your comment here.

Post as

Showing 0 comments
Sort by

Popular now

Subscribe by email

Subscribe by RSS

blog comments powered by DISQUS

2010 Xah Lee.

Home Terms of Use About Advertise

Subscribe

Search XahLee.org here

Search

4 of 4

Sunday 30 January 2011 06:10 AM

Das könnte Ihnen auch gefallen