Wednesday, May 03, 2006

Making emacs work for you

Although I have used linux since i was 19, i never really use it for server/network infra seriously. Most of my work on the Linux machine is programming work. At first, I just liked how it looks like and that it could run the necessary server needed to test my development, like Apache Tomcat.

As time goes by, I tried to learn vi. Since alot of my friends was using it at that time. But thanks for Arimdem Dey who was not very supportive in teaching me vi, i decided to learn how to use emacs on my own. I'm sure its a no brainer thing and should be fairly easy to use. And indeed it was. Since then, all I know about vi is the command dd, !wq, and insert, delete and the use of the escape key.

But I see now that it makes sense for me to learn emacs as it is a terrific programming tool, if you don't like using an ide like eclipse all the time and prefer light weight ide. Of course the default installation does not have special features and you have to install php-mode(for php), c-mode(for c/c++), java-mode(for java) and html-mode(for html). With this modes, it recognizes specific pattern recognition for each language. It allows syntax highlighting to recognizes syntax based on the file extension like .php, .java or etc....

Emacs configuration can be a bit confusing, and sometimes the default configuration doesn't include the nifty things you can do on emacs during installation. Hence, running emacs can be cumbersome, as you have to run alt-x global-font-lock-mode to have syntax highlighting appear. And alt-x show-paren to do parenthesis matching. So I finally decided to load all of my favorite configuration during initial startup. To do this, i created a file .emacs on my home directory.

Here are my favorite configuration on an emacs.






$ emacs ~joey/.emacs

; turns on autofill on emacs
(setq-default auto-fill-function 'do-auto-fill)

;; Make lines wrap automatically in text mode.
(add-hook 'text-mode-hook
'(lambda () (auto-fill-mode 1)))

;; Key binding to go to line by just doing alt-x g, to go to a line somewhere in your code
(global-set-key "\M-g" 'goto-line)

;; set tab width to 4 by default
(setq-default tab-width 4)

;; spaces instead of tabs by default...
(setq-default indent-tabs-mode nil)

;;If at beginning of a line, don't make me C-k twice.
(setq kill-whole-line t)

;; Don't let me add new lines to the bottom of a file with the downarrow
(setq-default next-line-add-newlines nil)

;; Don't let next-line add space to the bottom of your file.
(setq-default next-line-add-newlines nil)

;; Set up hungry deleting for c and c++
(setq c-hungry-delete-key 1)

;Basic unit of spaces for each indentation level. You can change
; the 2 if you need larger or smaller indents.
(add-hook 'c++-mode-hook '(lambda ()
(setq c-basic-offset 2)))
(add-hook 'c-mode-hook '(lambda ()
(setq c-basic-offset 2)))

; Use only spaces for indentation (default is tabs mixed with spaces)
; so that our files will always look the same regardless of the viewing
; software.
(add-hook 'c++-mode-hook '(lambda ()
(setq indent-tabs-mode nil)))
(add-hook 'c-mode-hook '(lambda ()
(setq indent-tabs-mode nil)))

; Make Emacs automatically hit return for you after left curly braces,
; right curly braces, and semi-colons.
(setq c-auto-newline 1)

; Make Emacs use "newline-and-indent" when you hit the Enter key so
; that you don't need to keep using TAB to align yourself when coding.
; This is akin to setting autoindent in vi.
(global-set-key "\C-m" 'newline-and-indent)

; Turn on auto-fill-mode (line wrapping like in a word processor).
; This is VERY nice for typing end-of-line comments, since Emacs will
; not only position you on the next line when you go beyond the
; fill-column (set below), but it will line you up with the first
; slash on the previous line and then put two slashes and a space for
; you! (In C mode it's a "/* */" pair with the cursor positioned
; correctly in the middle.)
(add-hook 'c++-mode-hook 'turn-on-auto-fill)
(add-hook 'c-mode-hook 'turn-on-auto-fill)

; Set the default comment column for end-of-line comments to 40.
; When you want to comment the end of a line of code, put the
; cursor anywhere on that line and hold down Alt and press the
; semi-colon key (';'). Emacs automatically goes to column 40, puts
; in the appropriate comment characters, and then lets you type your
; comment. With the auto-fill feature turned on, this also allows you
; to have your comment extend beyond the end of the line and still be
; lined up and enclosed with comment characters for you on the next
; line.
(add-hook 'c++-mode-hook '(lambda ()
(setq comment-column 40)))
(add-hook 'c-mode-hook '(lambda ()
(setq comment-column 40)))

;; Turn on global font-locking
(global-font-lock-mode t)

;; Turn on paren matching
(show-paren-mode t)

;; Displays the name of the file being edited in the title bar.
(setq frame-title-format "%b"

;; Makes the screen scroll only one line when the cursor moves past the edge.
(setq scroll-step 1)
(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)
'(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.
)



It makes a whole lot of sense when coding and makes it such a more powerful tool compared to for example, vi....But that is just an opinion.
Anyway, maybe later, I'll talk about using emacs with cvs and svn.
Yes, you update, add and commit files as you code;)