LevSelector.com |
Emacs and XEmacs
On This Page | Other Pages |
- intro |
- emacs_short_ref |
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) ^h - help |
^x^f - open file (press Enter to get file listing) Alt-! - run shell command |
You can split the main window in several. ^x0 - kill current window Current window resize: ^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.
Alt-# n - goto line n. |
^d, Del - del char (forw/back) ^SPACE , ^@ - set beginning mark |
^u 0 ^k - kill-line ^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) |
Alt-% oldstr RET newstr RET.
|
^x^u, ^x^l - convert region to upper / lower case. |
Emacs Commands - longer version
EMACS commands
Exiting and getting help:
C-x C-c - exit |
File operations:
C-x C-c y – exit and save |
Using windows inside emacs:
C-x b - to change current buffer in a window |
Moving in the window:
usually arrows work, but page-up/down don't. Faster moving: |
Deleting and Yanking:
C-d - delete-char. |
Search:
C-s - Incremental search forward (`isearch-forward'). |
Select, copy, paste = define region, kill, yank
C-SPC or C-@ - set the mark |
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. Convert from Unix to DOS: M-% C-q C-j <enter> C-q C-m C-q C-j<enter> |
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) |