Emacs Intermediate Tips

Xah Lee, 2005

If you are a emacs beginner, see Emacs Basics.

Don't forget, all the basics such as copy, paste, open, save... are in the graphical user menus.

Make Emacs Modern

First, turn on the CUA mode, under the menu “Options‣C-x/C-c/C-v Cut and Paste (CUA)”. Also, turn on “Options‣Paren Match Highlighting”, then select “Options‣Save Options”.

emacs basic options

The CUA mode will activate the shortcuts “Ctrl+x” for cut, “Ctrl+c” for copy, “Ctrl+v” for paste. Also, it will highlight when a region of a text is selected. And, when a text is selected, typing or pressing the Delete key will delete the slection. These are standard conventions.

Basic Keyboard Shortcuts

Open, Save, Close

Ctrl+x Ctrl+fOpen a new file
Ctrl+x Ctrl+sSave file
Ctrl+x kClose the current file

Moving Cursor

Use the arrow keys ↑↓←→ and Page up/down keys.

Alt+bBackward one word.
Alt+fForward one word.
Alt+<Beginning of document.
Alt+>End of document.
Ctrl+aMove to beginning of line. Also the Home key.
Ctrl+eMove to end of line. Also the End key.

Deleting Text

Alt+dDelete the next word
Alt+BackspaceDelete the previous word
Ctrl+kDelete all characters from cursor to end of line

Copy, Paste, Undo

Ctrl+SpaceMark the starting point for copy/cut a text
Alt+wCopy
Ctrl+wCut
Ctrl+yPaste
Ctrl+x hSelect All
Ctrl+/Undo. (To redo, type any character then do undo twice.)

If you rather prefer Cut/Copy/Paste with X/C/V keys, you can turn it on under the menu “Options‣C-x/C-c/C-v Cut and Paste (CUA)”. Highly recommended.

Split Window

Ctrl+x 2Split window into 2 panes
Ctrl+x 1Make the cursor pane fill window
Ctrl+x oMove cursor to the other pane

Searching Text

To search a string, press “Ctrl+s”, then type your search word, emacs will move the cursor to the location of the matched text as you type. Press “Ctrl+s” again to jump to the next occurrence, press “Ctrl+r” to jump to the previous occurrence.

Once you found what you are looking for, press the arrow key to cancel the search and leave your cursor there. To exit search and leave the cursor at its original place, press “Ctrl+g”.

Find & Replace Text

See advanced emacs tips.

Basic Principles of Emacs

General commands

Ctrl+gThis is the general command to cancel any key sequence.
Alt+xexecute-extended-command (give a command name to execute)

In emacs, every keystroke actually execute a command. For example, when you type a key “e”, emacs actually runs the command self-insert-command. In other words, any key or key combination or key sequence you press is bound to a command. There are about 3000 commands in emacs. Most commonly used commands have a keyboard shortcut. For example, moving the cursor, opening file, copy paste, close a file, search text. To run a command in emacs, type “Alt+x” followed by the command name.

To find out what command is associated with a particular keyboard combination, type “Alt+x describe-key” (Ctrl+h k), then press the key combination you want to know. To find out the shortcut for a particular command (if it has one), type “Alt+x describe-function” (Ctrl+h f) followed by the command name.

Example of Useful Commands

In emacs, a selection of text is called “region”. Many commands acts on the region. Here are some examples of commands that acts on the region.

Not all commands works on region. Here are some commands that does not necessary need a region:

Emacs Online Help

Emacs has a embedded tutorial. Type “Alt+x help-with-tutorial” to start it.

This tutorial is written in the 1980's mindset. However, if you bear with it, it still works, and will teach you the basics and make you self-sufficient to look further.

Emacs has far too many other help facilities to deal with its thousands of commands, thousands of emacs lisp functions, over a thousand pages of emacs documentation, over a thousand pages of emacs lisp documentation, and thousands pages of other GNU software documentations. To talk about them here would be just too much. They are all accessible under the “Help” menu, you can have a look if you are curious.


Related essays:


Page created: 2005-08.
© 2005 by Xah Lee.
Xah Signet