Emacs Cheat Sheet

jplindstrom on 2004-08-03T15:11:57

I started using Emacs the other day, and it's a fairly nice experience so far.

What I miss the most is Shift-movement to select text. That's a very convenient thing in Windows, and I now realize I use it all the time.

Below is my collection of Emacs survival-knowledge so far.

Did I miss anything?

 

1.1 Moving around Emacs

C-x C-c Exit Emacs 
C-g     Stop current command sequence 
C-x C-s Save buffer 
C-x b   Jump to different Buffer 
C-x C-b Buffer menu 
 
C-x ESC ESC      Display most recent Lisp code 
 
C-x 1   Only display one window 
C-x 2   Display two windows 

1.1.1 Repeat command

C-u n COMMAND 

1.2 Moving around your document

C-SPC   Put Mark at Point/Cursor 
C-x C-x   Exchange Point and Mark 
 
C-u C-SPC Go back to previous locations (like undo but non-destructive) 

1.2.1 Search

C-s Incremental Search 
C-r Reverse Incremental Search 
 
C-s C-w     Search with the current word highlighted 
 
C-M-s       Regexp Incremental Search 
C-M-r       Regexp Incremental Search 

1.2.2 Bookmarks

C-x r m <somekey>             Mark bookmark 
C-x r b <somekey>             Visit bookmark 
C-x r l                       List bookmarks 

1.3 Display

C-x 2 Split window into two 
C-x 1 Only one window 

C-x 5 2    Create new Emacs window 

1.3.1 Don't wrap lines

toggle-truncate-lines 

1.3.2 Display only functions

Almost. Display only lines with a maximum indentation

C-u 2 C-x $     Hide lines with text in column 1 or 2 
C-x $           Show all lines again 

1.4 Editing

M-x delete-trailing-whitespace 

C-x C-q Make read-only file writable 

M-c     Capitalize the following word, moving over. 
M-l     Convert following word to lower case, moving over. 
M-u     Convert following word to upper case, moving over. 

M-h     Put point at beginning of this paragraph, mark at end. 

1.4.1 Copy & Paste

C-k     Kill to right of line, or blank line 
 
C-SPC   Set Mark 
C-w     Kill Region (between Mark and Point) (Cut) 
M-w     Copy Region (Copy) 
C-y     Yank copied text (Paste) 
M-y     Yank older text (repeatedly) 

1.4.2 Undo

C-x u    Undo one step. C-f to "turn around" and redo instead 
C-_      Undo, same as above 
 
C-u C-_  Undo only in region 

1.4.3 Typing

M-/       Command Completion with words from open buffers 

1.4.4 Indentation

http://www.fnal.gov/docs/products/emacs/emacs/emacs_23.html#SEC182

C-M \           Indent several lines to same column (indent-region). (ESC C-M \) 

1.4.5 Comparing

M-x compare-windows 

1.4.6 Narrow to Region

This will dive into the current region and hide the rest of the document

C-x n n   Narrow region 
C-x n w   Widen region 

1.5 Programming

1.5.1 Find Other Source file

Jump to corresponding header/cpp file, or the one on the point

ff-find-other-file 

1.5.2 C++ etags

http://www.yolinux.com/TUTORIALS/LinuxTutorialXemacs.html

First, create a TAGS file, from the shell

find . -iregex '.*\.\(c\|h\|cpp\|cc\)' -print | etags -d -t --members - 

Then tell Emacs you want to use it

M-x visit-tags-table 


M-.     Find a definition for a tag. The default tag is identifier under the cursor. Name completion type partial name and then TAB. 
M-,     Find the next definition for the tag. 
M-*     Pop tag stack (go back one level) 

1.6 Useful tricks

1.6.1 Speedbar

The speedbar is a file/tags list

M-x speedbar 

1.6.2 Shell Functions

M-x shell 
M-x grep 
M-!     Single shell command 

1.6.3 Macro recording

C-x (   Start recording 
C-x )   Stop recording 
X-x e   Execute macro 

Apply to the entire Region

M-x apply-macro-to-region-lines 

Name the last recorded macro

M-x name-last-kbd-macro 

2 Links


Selecting with shift...

ambs on 2004-08-03T15:21:12

M-x pc-selection-mode

Welcome to emacs.

Re:Selecting with shift...

jplindstrom on 2004-08-03T15:28:09

EXCELLENT!

Thanks!

ahh...

spur on 2004-08-03T15:29:57

ain't nothing as good as some elisp hacking :-)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Selecting text with Shift+arrows, like in Windoze
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun previous-line-and-select (&optional arg)
  (interactive "_p")
  (if (null (region-active-p))
      (set-mark-command nil))
  (previous-line arg))

(defun next-line-and-select (&optional arg)
  (interactive "_p")
  (if (null (region-active-p))
      (set-mark-command nil))
  (next-line arg))

(defun forward-char-and-select (&optional arg)
  (interactive "_p")
  (if (null (region-active-p))
      (set-mark-command nil))
  (forward-char arg))

(defun backward-char-and-select (&optional arg)
  (interactive "_p")
  (if (null (region-active-p))
      (set-mark-command nil))
  (backward-char arg))

(global-set-key '(shift up) 'previous-line-and-select)
(global-set-key '(shift down) 'next-line-and-select)
(global-set-key '(shift right) 'forward-char-and-select)
(global-set-key '(shift left) 'backward-char-and-select)

Make file writable?

jplindstrom on 2004-08-03T15:33:58

BTW; is there any clever way to make the currently edited file +w?

Any shortcut for "this file" when using (M-!) ?

emacs quick reference

slanning on 2004-08-03T15:41:56

There is an old "emacs quick reference" (aka cheat sheet) floating around that you can print out.

Regarding changing file mode, do that from `dired` (when you edit a directory, just like a file) by typing 'M' with the cursor on the line the file is listed on

How do I keep truncate lines?

jplindstrom on 2004-08-03T15:52:05

Actually, I have a problem with newly opened files.

I have (setq truncte-lines t) in my .emacs file, but it doesn't seem to work for newly opened files, and I have to "M-x toggle-truncate-lines" for each of them.

How do I make it do that automagically?

Re:How do I keep truncate lines?

merlyn on 2004-08-03T16:21:49

Because it's a buffer-local variable. You have to:
(setq-default truncate-lines t)
which affects the value for each new buffer. You can determine this by asking for the help on that variable and noticing that it says "automatically becomes buffer-local when set in any fashion".

Generally, for most user settings, the modern "customize" mechanism is much easier. For example, M-x customize-apropos RET truncate RET brings up the settings for this variable.

Good information and comments

jordan on 2004-08-05T14:33:08

I'm trying to use emacs, for a number of Good Reasons, but I find a one thing rather painful.

Startup times are slow, compared to other editors. In my work, I hit a lot of different systems and investigate problems and questions. It's so much easier to fire up my personal vi-like favorite editor, vile, than it is to start emacs.

Do people seriously use emacs with Ange-FTP and the various shell/telnet modes to manage a lot of different environments and stay in one emacs all day? I think this might be a Good Thing, seeing as it would be convenient to have all the logs from your telnet sessions handy for reference, but I've not got the knack of it yet.

Re:Good information and comments

merlyn on 2004-08-05T16:22:17

"screen emacs" is your friend. I fire up an Emacs only about once a month or so on each machine I visit. Then, I just reuse it and abuse it.

I also read mail, news, and connect to IRC from within Emacs. I use dired mode to avoid ever typing "rm mumble*mumble". I use shell mode to fire up a few shells inside Emacs, to allow me a consistent cut-paste environment.

At the risk of repeating myself, "screen emacs" is your friend.

Re:Good information and comments

jordan on 2004-08-05T16:42:12

Thanks for the pointer. I've been using it at home on my Linux machine like that a lot. I read email and news, connect to IRC and I've even been playing with the various WWW browsers from within emacs as an immersive technique.

I doubt that I could really keep an emacs running for days on the work machines. Some sys admin might object and they're often rebooting, but I could setup to fire off a set of rsh commands from my desktop each morning as I'm starting up to startup my various emacs sessions that I'd use all day. I typically check my Outlook email first anyway...

Re:Good information and comments

perrin on 2004-08-09T14:42:54

Ange-FTP (or rather it's successor) is pretty good, but these days I just let KDE handle that for me. It can deal with SCP in addition to FTP, and lets you edit in whichever editor you like.