Monday, August 20, 2007

Emacs: the GNU's editor

I have been using Vim editor for past ( almost ) four years, but now I have switched over to Emacs. I find it more intuitive ( just a personal opinion ). While I was learning Emacs I had few queries. So, I have prepared a small HOWTO which might help a new emacs user.

HowTo Colorize the syntax?

M-x font-lock-mode

Here M is the Meta key, which may be Alt or Esc (top-left corner of the keyboard). So the above is equivalent to "Alt+x font-lock-mode" or "Esc x font-lock-mode".

How do I add color themes to my Emacs editor?

Install the following package :

emacs-color-theme

you will get a lot of color themes (for Emacs) installed onto your system. To select / apply a theme use M-x color-theme-select Theme-Name
I liked Hober and Euphoria.


I usually log onto a slow server (say, using SSH) and editing of files on the remote server has become a hated task, usually when I have to navigate a big text/program file up and down. Can Emacs help me?

Sure, Emacs can help you edit files on the remote system. Install the following package:
TRAMPS
for emacs and go ahead.

While logging in onto a server for editing files, TRAMPS asks me a password each time I try to open a separate file. So, how do I create ( SSH ) login with no password required?

On remote system (server):
$ ssh-keygen -t rsa

On local system (localhost):
$ ssh-keygen -t rsa
$ cd .ssh
$ scp id_rsa.pub user@server :~/.ssh/id_rsa.user_at_localhost

On remote system ( server ):
$ cd ~/.ssh
$ cat id_rsa.user_at_localhost >> authorized_keys

Now you login from localhost to server.


How do I recover a file from the last session, which unfortunately crashed?

M-x recover-file

How do I indent a region?

C-M-\

How do I comment a region?

M-;

How do I AutoComplete a word which I have typed partially, but it is already present somewhere in the currrent file?

M-/

How do I delete all trailing whitespace from lines in the text ( or the selected region) ?

M-x delete-trailing-whitespace RET. (This command does not remove the form-feed characters.)

How do I use the Code Folding feature of Emacs?

To fold all the function bodies: M-1 C-x $ and magic happens!
As usual, it's white magic: C-x $ will bring your code back.



How do I Customize my EMACS using the .emacs configuration file?

This is a file I found somewhere on the Internet. Just copy paste the contents if you want to experiment.


(setq additional-paths '("/home/tuxdna" "/home/joe/misc/lisp"))
(setq load-path (append additional-paths load-path))

;;;;;;;;;;;;;;;;;
;; APPEARANCE
;;;;;;;;;;;;;;;;;
(setq font-lock-maximum-decoration t)
(setq visible-bell t)
(setq require-final-newline t)
(setq resize-minibuffer-frame t)
(setq column-number-mode t)
(setq-default transient-mark-mode t)
(setq next-line-add-newlines nil)
(setq blink-matching-paren nil)
(global-font-lock-mode 1 t)
(blink-cursor-mode -1)
(tool-bar-mode -1)
(tooltip-mode -1)

;;Customized syntax highlighting colors
(set-default-font "-misc-fixed-medium-r-normal-*-*-200-*-*-c-*-koi8-r")
(set-foreground-color "#dbdbdb")
(set-background-color "#000000")
(set-cursor-color "#ff0000")
(custom-set-variables '(load-home-init-file t t))

(if (> (display-color-cells) 20)
(custom-set-faces
'(font-lock-builtin-face ((t (:foreground "deepskyblue"))))
'(font-lock-comment-face ((t (:foreground "gray60"))))
'(font-lock-doc-face ((t (:foreground "darkkhaki"))))
'(font-lock-keyword-face ((t (:foreground "magenta"))))
'(font-lock-function-name-face ((t (:foreground "green" :background "seagreen"))))
'(font-lock-string-face ((t (:foreground "gold"))))
'(font-lock-type-face ((t (:foreground "cyan" :background "slateblue"))))
'(font-lock-variable-name-face ((t (:foreground "yellow"))))
'(modeline ((t (:foreground "plum1" :background "navy"))))
'(region ((t (:background "sienna"))))
'(highlight ((t (:foreground "black" :background "darkseagreen2"))))
'(diff-added-face ((t (:foreground "green"))))
'(diff-changed-face ((t (:foreground "yellow"))))
'(diff-header-face ((t (:foreground "cyan"))))
'(diff-hunk-header-face ((t (:foreground "magenta"))))
'(diff-removed-face ((t (:foreground "red")))))
)

;;;;;;;;;;;;;;;;;
;; KEYS
;;;;;;;;;;;;;;;;;

(setq suggest-key-bindings t)
(global-set-key [delete] 'delete-char)
(global-set-key [kp-delete] 'delete-char)
(global-set-key "\C-h" 'backward-delete-char)
(global-set-key "\C-x\ ?" 'help)
(global-set-key "\C-c\ l" 'goto-line)
(global-set-key "\C-x\ f" 'find-file-other-window)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)
(global-set-key "\C-z" nil)

;;;;;;;;;;;;;;;;;
;; MISC
;;;;;;;;;;;;;;;;;

;; go right to an empty buffer
(setq inhibit-startup-message t)

;; translates ANSI colors into text-properties, for eshell
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

;; cvs stuff
(setq vc-default-back-end 'CVS)
(autoload 'cvs-add "cvs-add" "CVS add" t)
(autoload 'cvs-commit "cvs-commit" "CVS commit" t)
(autoload 'cvs-diff "cvs-diff" "CVS diff" t)

;; recentf stuff
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 25)

;; mode stuff
(setq auto-mode-alist (cons '("\\.php$" . php-mode) auto-mode-alist))
(autoload 'ruby-mode "ruby-mode" nil t)
(setq auto-mode-alist (cons '("\\.rb$" . ruby-mode) auto-mode-alist))

;; autorevert stuff
(autoload 'auto-revert-mode "autorevert" nil t)
(autoload 'turn-on-auto-revert-mode "autorevert" nil nil)
(autoload 'global-auto-revert-mode "autorevert" nil t)
(global-auto-revert-mode 1)

(defalias 'yes-or-no-p 'y-or-n-p)
(setq auto-save-interval 50)
(setq list-matching-lines-default-context-lines 1) ;; for M-x occur

;; counting functions
(defalias 'lc 'count-lines-page)

(defun wc ()
"Count the words in the current buffer, show the result in the minibuffer"
(interactive) ; *** This is the line that you need to add
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let ((count 0))
(while (forward-word 1)
(setq count(1+ count)))
(message "There are %d words in the buffer" count)))))

;; normally disabled by default
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

;; Highlights the selected region
(transient-mark-mode t)

4 comments:

Kazim Zaidi said...

So.. you're one of those *dhokebaaz* vi users who quit vi camp and joined emacs camp?

The tutorial you wrote is good, but it just seems to me that switching to emacs (giving my proficiency in vi, an experience of almost 2 years) would be rather hard. It would take a lot of time to reach that level, lots of patience, and lots of extra typing (vi commands look short than emacs?!)

- Kazim Zaidi

saleem.a.ansari said...

Are you trying to say that Emacs commands seem longer than Vi ( perhaps Vim ) 's ?
That may seem so but actually you will notice that Emacs commands are shorter( just a few key strokes), although they may seem to be longer. Vim's commands are rather longer.

There are a lot top hackers who already use Emacs. If you want to see then read here. http://usmanahmad.wordpress.com/2006/08/02/great-programmers-answersinterview-with-steve-yegge-linus-torvalds-dave-thomas-david-heinemeier-peter-norvig-james-gosling-guido-van-tim-bray/ Of the 10 top hackers, six use Emacs and rest use other editors.

Rakesh said...

Hi Saleem,

The 'Emacs:the GNU's editor' is very useful post for a newbie.

Best,
Rakesh

Ashish SHUKLA said...

The color scheme in your .emacs is cool :).

Thanks