levselector.com New York
home > Emacs Email

Emacs and XEmacs
On This Page Other Pages

- intro
- emacs_commands_short_ref
- emacs_commands_long_reference
- dot_emacs_file

- emacs_short_ref
- emacs_short_ref.doc - 1-pager, good for printing out

Intro ------------------------------

Emacs (and its independent "brother" XEmacs) - are powerful open-source editors. Mostly used as programming editors on Unix (including Apple Macintosh). But you can also effectively use these editors on Windows.

Get emacs:

Install it:

Documentation:

In addition to tutorials and docs inside the emacs, you can google for tutorials, for example:

Speedbar - allows you to get a listing of files or tags inside files - and easily navigate by clicking on them.

http://cedet.sourceforge.net/speedbar.shtml

 

emacs commands - short reference

Basic emacs is easy to learn. In fact, you can be an effective user in ~30 min. Here are the main commands you need to know. Here Ctrl key is represented with "^", and Meta key (Alt, Option) is represented by "Alt".

Exit / get help Open/Save file emacs windows and buffers

^x^c - close window (exit)
^g - get out (of a command entry) - may be several times
^x u - undo
^x _ - undo
^x / - undo

^h - help
^h^h - Help on help
^h t - tutorial
^h i - info (documentation)
^h k <key> - help on this key
^h k <function> - describe function
Alt-` or ESC` or F10 - menubarAlt-x - switch to minibuffer (^g - out)

^x^f - open file (press Enter to get file listing)
^x d - open a directory
^x i - insert text from another file.
^x^s - save file
C-x C-c y – exit and save
^x^w - write to another file
^x^v - find alternate file (with ~)

Alt-! - run shell command
Alt-x shell - open shell window

You can split the main window in several.
Each window can be attached to a buffer (file, scratch, etc.)
^x b - change/create buffer
^x^b - list of buffers
^x k - close buffer
^x o - select "other" window

^x0 - kill current window
^x1 - kill all windows except current
^x2 - split horizontal
^x3 - split vertical 
^x40 - kill current window and buffer

Current window resize:
^x^ - taller / ^x- - shorter
^x} - wider / ^x{ - narrower
^x + - make all windows same height

^l ( small L ) - redraw the screen

Move in a window delete, mark, cut/copy, yank(paste) more on selecting, ...

usually arrows work, but PgUp/PgDn don't.
^f, ^b, ^n, ^p (forward, back, next, previous)

Move faster:

  forward, end back, a(bc)
word Alt-f Alt-b
line ^e ^a
sentence Alt-e Alt-a
page ^x ] ^x [
buffer(file) Alt-> Alt-<
screen (cursor in mid-screen) ^Alt-e ^Alt-a
screen ^v Alt-v

Alt-# n - goto line n.
Alt-x goto-line RET 5 RET - goto line 5

^d, Del - del char (forw/back)
(note: ^h is for help)
Alt-d, ^Del - word
^k - rest of the line
^k^k^k^k - cut several lines
Alt-k - rest of sentence.
^a^k - delete the line

^SPACE , ^@ - set beginning mark
^x ^x - jump to/from mark point
Alt-@ - select word
Alt-h - select paragraph
^x h
- select all (mark-whole-buffer)

^w - cut from current pos. to mark (kill-region - put it on the kill ring)

Alt-w - copy from current pos to mark (kill-ring-save - equivalent to ^w^y)

^y - paste (yank) last killed thing
Alt-y -Yank earlier kills.

^u 0 ^k - kill-line
^u -1 ^k - kill-line including preceding newline

^Alt-k - kill-sexp. Kills the sexp after the cursor.
^u -1 ^Alt-k - kill-sexp. Kills the sexp before the cursor.
The command backward-kill-sexp exists, but is not bound to any key by default.

^Alt-@ - mark-sexp. Sets the region around matching parenths. (the same sexp that C-M-f would move to).

^Alt-h - mark-defun. Mark function definnition.

^x^i - indent-rigidly the region by 1 or more chars (default is 1).

Search Search and Replace Capital/low case, debugger

^s str RET - isearch-forward (as you type)
^r str RET - isearch-backward
^s RET str RET - regular search

^s - to repeat search

^s^w - search for word
^s^y - search for the rest of the line
^s Alt-y - search for copied text

=====================

I
f C-s doesn’t work because C-s is “stop output”, do the following:
M-x enable-flow-control
Then use C-\ instead of C-s, and C-^ instead of C-q
auto-fill-mode
C-q C-l – to enter control-l

Alt-% oldstr RET newstr RET.
Space, y replace oldstr with newstr.
Delete, n skip to the next occurrence (without replacing this one).
Enter, Esc, q exit without any more replacements.
. (period) replace this occurrence and exit.
^ go back to the location of the 
previous occurrence if you changed it.
! replace all remaining occurrences 
without asking again.

^x^u, ^x^l - convert region to upper / lower case.
Alt-u, Alt-l - conv. word to upper / lower case.
Alt-c - capitalize the word.

Using debugger:
^x3 - create second window
Alt-x - perldb RET - enter command perldb
n - next
q - quit

 

Emacs Commands - longer version

EMACS commands

Exiting and getting help:

C-x C-c - exit
C-g (may be several times) - get out from the middle of some command entry
C-x u - undo changes
C-x _ - undo
C-h - help
C-h t - tutorial
C-h i - info (documentation)
C-h k <somekey> - help on this <somekey> .
C-h f somefunction – describe somefunction
M-` or <ESC><`> or F10 - Activate menubar.
M-x – switch to minibuffer (C-g – out)

File operations:

C-x C-c y – exit and save
C-x C-s - save
C-x C-w - save file as
C-x C-f - open file (if you then press enter - it will show you the directory listing from which you can choose a file).
C-x C-d - open directory
C-x i - insert file
===============================
M-! - run shell command
M-x shell - open shell window

Using windows inside emacs:

C-x b - to change current buffer in a window
C-x C-b - to list buffers (to select one)
C-x 2 - split window into 2 (one above another = split-window-vertically)
C-x 3 - Split the selected window into two windows side by side (split-window-horizontally)
C-x o - Select another window (other-window).
C-x 0 - Delete the selected window (delete-window).
C-x 1 - Delete all windows in the selected frame except the selected window (delete-other-windows).
C-x 4 0 - Delete the selected window and kill the buffer that was showing in it (kill-buffer-and-window).
C-x ^ - Make selected window taller (enlarge-window).
C-x } - Make selected window wider (enlarge-window-horizontally).
C-x { - ... narrower (shrink-window-horizontally).
C-x - - Shrink this window if its buffer doesn't need so many lines (shrink-window-if-larger-than-buffer).
C-x + - Make all windows the same height (balance-windows).

Moving in the window:

usually arrows work, but page-up/down don't.
C-f - forward-char.
C-b - backward-char.
M-f - forward-word.
M-b - backward-word.
C-n - next-line.
C-p - previous-line.
C-a - beginning-of-line. (a - beginning of the alphabet).
C-e - end-of-line (e - End).

Faster moving:
M-a , M-e - sentences
M-{ , M-} - paragraphs
C-x [ , C-x ] - pages
C-v, M-v – screen down/up
CM-e, CM-a – screen down/up, cursor in mid-screen
M-< , M-> - beginning / end of buffer (file).

Deleting and Yanking:

C-d - delete-char.
DEL - delete-backward-char. (Remember, Emacs does not use C-h (aka Backspace) for deletion, but for help!)
M-d - kill-word. (forward).
M-DEL - backward-kill-word.
C-k - kill-line.
C-u 0 C-k - kill-line. Kills to the beginning of the current line, not including the newline.
C-u -1 C-k - kills the line including the newline before the line.
C-M-k - kill-sexp. Kills the sexp after the cursor.
C-u -1 C-M-k - kill-sexp. Kills the sexp before the cursor.
The command backward-kill-sexp exists, but is not bound to any key by default.
C-y (yank) - restore the yanked text.

Search:

C-s - Incremental search forward (`isearch-forward').
C-r - Incremental search backward (`isearch-backward').
M-% - interactive query-replace. At each match you can:
SPC - Perform this replacement.
DEL - Don't perform this replacement.
RET - Terminate query-replace without performing this replacement.
ESC - Same as RET.
. - Perform this replacement but then terminate the query-replace.
! - Perform this replacement for all the rest in the buffer without asking.


Select, copy, paste = define region, kill, yank

C-SPC or C-@ - set the mark
C-x C-x - jump between the cursor point and the mark point
C-w - kill-region - put it on the kill ring.
C-y – yank (paste)
M-w - kill-ring-save. (equivalent to C-w C-y).
C-x h - mark-whole-buffer (select ALL).
M-@ - mark-word.
M-h - mark-paragraph.
C-M-@ - mark-sexp. Sets the region around matching parenths. (the same sexp that C-M-f would move to).
C-M-h - mark-defun. Mark function definnition.
C-x C-i - indent-rigidly. Rigidly indents the region by as many characters (columns) as you provide as a numeric argument (default is 1 column).
===============================
If C-s doesn’t work because C-s is “stop output”, do the following:
M-x enable-flow-control
Then use C-\ instead of C-s, and C-^ instead of C-q
auto-fill-mode
C-q C-l – to enter control-l
M-x goto-line N

Search/replace newlines and tabs:

To search/replace special characters, you need to prefix them with quoting command C-q.

For example, to get the new-line character (\n) you need to type : C-q C-j.
And to find a tab you can type: C-q <tab> (or C-q C-i).

Convert from Unix to DOS: M-% C-q C-j <enter> C-q C-m C-q C-j<enter>

 

dot_emacs file

Example of a .emacs file

(setq list-command-history-max 500)
(setq comint-input-ring-size 500)
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt)
(setq load-path (append (list "~/lib/cperl") load-path ) )
;
           ; Stolen from mtk
           ;
           ;(define-key global-map [up] 'up-one-line)
           ;(define-key global-map [down] 'down-one-line)
           ;(define-key global-map [right] 'scroll-right)
           ;(define-key global-map [left] 'scroll-left)
           ;
           ;(defun up-one-line (n) "scroll window up n lines (default 1)"
           ;  (interactive "p")
           ;  (scroll-up (cond ((null n) 1) (t n))))
           ;(defun down-one-line (n) "scroll window down n lines (default 1)"
           ;  (interactive "p")
           ;  (scroll-down (cond ((null n) 1) (t n))))
(require 'echistory)
           (global-set-key "\^x\^[\^[" 'electric-command-history)
           (global-set-key "\C-xb" 'electric-buffer-list)
;
           ; Confirm suspension and exit
           ;
           ;(defun my-exit-from-emacs ()
           ;   (interactive)
           ;   (if (yes-or-no-p "Do you want to exit ")
           ;       (save-buffers-kill-emacs)))
;(defun my-suspend-emacs ()
           ;  (interactive)
           ;  (if (yes-or-no-p "Do you want to suspend ")
           ;      (suspend-emacs)))
;(global-set-key "\C-z" 'my-suspend-emacs)
           ;(global-set-key "\C-x\C-z" 'my-suspend-emacs)
           ;(global-set-key "\C-x\C-c" 'my-exit-from-emacs)
 
;; =================== from Patrick
           ;; Activate font lock mode (for all buffers)
           ;;(global-font-lock-mode t)
           ;;(transient-mark-mode t)
;;(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
           ;;(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
           ;;(autoload 'cperl-mode "cperl-mode" "alternate mode for editing Perl programs" t)
           ;;(require 'cperl-mode)
           ;; ==================== end Patrick
         
; Setting from Damian Conway's book, "Perl Best Practices"
           ;
           (defalias 'perl-mode 'cperl-mode)
;(global-set-key "\r" 'newline-and-indent)
           (setq-default indent-tabs-mode nil)
(setq fill-column 78)
           (setq auto-fill-mode t)
;; (global-set-key "%" 'match-paren)
           ;; (defun match-paren (arg)
           ;;   "Go to the matching paren if on a paren; otherwise insert %."
           ;;   (interactive "p")
           ;;   (let ((prev-char (char-to-string (preceding-char)))
           ;;      (next-char (char-to-string (following-char))))
           ;;     (cond ((string-match "[[{(<]" next-char) (forward-sexp 1))
           ;;        ((string-match "[\]})>]" prev-char) (backward-sexp 1))
           ;;        (t self-insert-command (or arg 1)))))
(defun application-template-pl ()
   "Inserts the standard Perl application template"
           (interactive "*")
           (switch-to-buffer "application-template-pl")
           (insert-file "~/.code_templates/perl_application.pl"))
           (global-set-key "\C-ca" 'application-template-pl)
(defun module-template-pm ()
   "Insert the standard Perl module template"
           (interactive "*")
           (switch-to-buffer "module-template-pm")
           (insert-file "~/.code_templates/perl_module.pm"))
           (global-set-key "\C-cm" 'module-template-pm)
(abbrev-mode 1)
(define-abbrev-table 'global-abbrev-table 
           '(
           ("pdbg"   "use Data::Dumper qw( Dumper );\nwarn Dumper[];"    nil 1)
           ("phbp"   "#! /usr/bin/perl -w"                               nil 1)
           ("pbmk"   "use Benchmark qw( cmpthese );\ncmpthese -10, {};"  nil 1)
           ("pusc"   "use Smart::Comments;\n\n### "                      nil 1)
           ("putm"   "use Test::More 'no_plan';"                         nil 1)
           ))
(add-hook 'text-mode-lambda (lambda () (abbrev-mode 1)))
(setq auto-mode-alist (append auto-mode-alist '(("\\.C$" . c-mode)
           ("\\.perl$" . perl-mode)
           ("\\.p$" . perl-mode)
           ("\\.PL$" . perl-mode)
           ("\\.pod$" . perl-mode)
           ("\\.doc$" . text-mode)
           ("\\.txt$" . text-mode)
           ("\\.cpp$" . c-mode)
           ("\\.cc$" . c-mode)
           ("\\.H$" . c-mode)
           ("\\.x$" . c-mode)
           ("\\.ec$" . c-mode)
           ("\\.h$" . c-mode)
           ("\\.hh$" . c-mode)
           ("\\.st$" . mst-mode)
           ("\\.tar$" . tar-mode)
           ("\\.k?sh$" . ksh-mode)
           ("\\.t$" . perl-mode)
           ("\\.ph$" . perl-mode)
           ("\\.pm$" . perl-mode)
           ("\\.xs$" . c-mode)
           ("\\.sql$" . c-mode)
           ("\\.jar$" . archive-mode)
           ("\\.Z$" . uncompress-while-visiting))))
(custom-set-variables
           ;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
           ;; Your init file should contain only one such instance.
           '(case-fold-search t)
           '(cperl-close-paren-offset -4)
           '(cperl-continued-statement-offset 4)
           '(cperl-indent-level 4)
           '(cperl-indent-parens-as-block t)
           '(cperl-tab-always-indent t)
           '(current-language-environment "English")
           '(global-font-lock-mode t nil (font-lock)))
(custom-set-faces
           ;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
           ;; Your init file should contain only one such instance.
           )
(global-set-key (kbd "<deletechar>") 'backward-delete-char-untabify)
;; disable underlining of trailing spaces in cperl mode
           (setq cperl-invalid-face nil)
;; always highlight scalars
           (setq cperl-highlight-variables-indiscriminately t)
       

yet another example

(setq inhibit-default-init t) 
         (setq line-number-mode t) 
         (setq column-number-mode t) 
         (setq truncate-partial-width-windows nil) 
         (setq frame-title-format "%*%[%f%]  {emacs}  %s") 
         ;;(setq icon-title-format  "%f - Emacs") 
(add-hook 'c++-mode-hook 
           '(lambda () 
           (c-set-style "bsd") 
           (c-toggle-auto-state 1) 
           (c-toggle-hungry-state 1) 
           (define-key c++-mode-map "\C-t" 'tags-search) 
           (define-key c++-mode-map "\M-t" 'tags-query-replace) 
           (define-key c++-mode-map "\C-\M-t" 'tags-apropos) 
           ) 
           t) 
(add-hook 'c-mode-hook 
           '(lambda () 
           (c-set-style "bsd") 
           (c-toggle-auto-state 1) 
           (c-toggle-hungry-state 1) 
           (define-key c-mode-map "\C-t" 'tags-search) 
           (define-key c-mode-map "\M-t" 'tags-query-replace) 
           (define-key c-mode-map "\C-\M-t" 'tags-apropos) 
           ) 
           t) 
;;(autoload 'perl-mode "~/emacs/cperl-mode" "alternative mode for Perl editing" t) 
(add-hook 'cperl-mode-hook 
           '(lambda () 
           (cperl-set-style "PerlStyle") 
           (define-key perl-mode-map "\C-t" 'tags-search) 
           (define-key perl-mode-map "\M-t" 'tags-query-replace) 
           (define-key perl-mode-map "\C-\M-t" 'tags-apropos) 
           ) 
           t) 
   
         
;;(setq perl-hairy t) 
           (setq cperl-hairy t) 
(setq auto-mode-alist 
           (append '(("\\.\\([pP][lmkL]\\|al\\)$" . cperl-mode))  auto-mode-alist )) 
(setq interpreter-mode-alist (append interpreter-mode-alist 
           '(("miniperl" . cperl-mode)))) 
         
;;(setq message-log-max nil) 
           ;;(kill-buffer "*Messages*") 
(setq display-time-24hr-format t) 
             
         
(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist)) 
(setq default-frame-alist 
           (append default-frame-alist 
           '((top . 20) 
           (left . 35) 
           (width . 80) 
           (height . 50) 
           ;;                (background-color . "White") 
           ;;                (background-color . "Black") 
           ;;                (foreground-color . "White") 
           ;;              (font .  "-misc-fixed-*-*-*-*-13-*-*-*-*-*-*-*") 
           ) 
           ;;              '((font .  "-misc-fixed-*-*-*-*-13-*-*-*-*-*-*-*") 
           ;;                (background-color . "White") 
           ) 
           ) 
(setq show-paren-mode 1) 
           (add-hook 'shell-mode-hook 
           '(lambda () (setq comint-completion-addsuffix '("\\" . ""))) 
           t) 
(define-key global-map [M-S-down-mouse-1] 'imenu) 
           (global-set-key [home] 'beginning-of-line) 
           (global-set-key [end] 'end-of-line) 
           (global-set-key [f8] 'delete-other-windows) 
           (global-set-key [f9] 'split-window-horizontally) 
           (global-set-key [f12] 'bury-buffer) 
           (global-set-key [f11] 'iswitchb-buffer) 
           (global-set-key [f5] 'replace-string) 
           (global-set-key [f6] 'goto-line) 
           ;;(autoload 'follow-mode "follow" 
           ;;    "Synchronize windows showing the same buffer, minor mode." t) 
;; Turn on font-lock in all modes that support it 
           (if (fboundp 'global-font-lock-mode) 
           (global-font-lock-mode t)) 
;; Maximum colors 
           (setq font-lock-maximum-decoration t) 
           (custom-set-variables 
           '(tab-width 4)) 
           (custom-set-faces)