Wednesday, December 10, 2008

emacs: the dark side of the force

Recently I've decided to try the dark side of the force - emacs.
I'm Vim user for a long time. Several times I wanted to try emacs but didn't have a good chance.
Now I'm working on project with huge amount of sources and I decided to try emacs for it.
It works! ;)

Playing with emacs I've found out that it's not so complex as some people say.

The thing to which I couldn't get used to for some time is that I don't have to press ESC to switch to the command mode, press i(INS) to switch to editor mode and so on.

I haven't found some Vim features(as visual mode) but I believe that just don't know how to make them work.

The main difference is that there is no distinct differences between editor mode and command mode. You are allowed to run commands while you are editing the text.

All commands(or better to say most of them) begin with control- or meta- character. control is usually Ctrl on your keyboard and meta is Alt.

For guys who want to try emacs here is the migration guide on vim-to-emacs commands.
The table of equivalence of vim and emacs commands.

split horizontalsplit vertical
:qa/:wqa/:xa/:qa!C-x C-cexit(emacs prompts whether to save buffers or not)
b/BM-bword backward
w/WM-fword forward
0C-abeginning of the line
$C-eend of the line
gg/1GC-<beginning of the buffer
GC->end of the buffer
xC-ddelete under cursor
DC-kdelete from cursor to EOL
ddC-k C-kdelete line
dw/dWM-ddelete word
db/dBM-{BACKSPACE}delete word backwards
:set ignorecase {ENTER} /C-s {needle in lower case}icase search forward
:set ignorecase {ENTER} ?C-r {needle in lower case}icase search backward
/C-ssearch forward
?C-rsearch backward
:set ignorecase {ENTER} /M-C-s {regexp in lower case}icase regexp search forward
:set ignorecase {ENTER} ?M-C-r {regexp in lower case}icase regexp search backward
:%s/{needle}/{replacement}/gcM-% {needle} {ENTER} {replacement} {ENTER}query replace
/M-C-sregexp search forward
?M-C-rregexp search backward
uC-_/C-x uundo
C-RC-_/C-x uredo(it's tricky for emacs*)
ESCC-gquit the running/entered command(switch to command mode in Vim)
:e fileC-x C-fopen file
:set roC-x C-qset file as read-only
:wC-x C-ssave buffer
:w fileC-x C-w filesave buffer as ...
:waC-x ssave all buffers
:buffersC-x C-bshow buffers
:b [name]C-x b [name]switch to another buffer
:q/:q!/:wq/:xC-x kclose buffer
C-w n/:splitC-x 2
C-w v/:vsplitC-x 3
C-w C-wC-x oswitch to another window
:qC-x 0close window
C-w oC-x 1close other windows
:! {cmd}M-!run shell command
m{a-zA-Z}C-@/C-spaceset mark
C-x C-xexchange mark and position
{visual}yM-wcopy region**
{visual}dC-wdelete region**
C-V {key}C-q {key}insert special char, e.g. ^M:
{visual}SHIFT->C-x TABindent region
C-]M-.find tag
C-tM-*return to previous location
:e!M-x revert-bufferreload buffer from disk

*To redo changes you have undone, type `C-f' or any other command that will harmlessly break the
sequence of undoing, then type more undo commands
**region is from current position to the mark

Other useful emacs commands:
M-ggo to line
C-x iinsert file
C-x hmark whole buffer
C-x C-tswitch two lines
M-C-abeginning of the function
M-C-eend of the function
M-abeginning of the statement
M-eend of the statement
M-C-hmark the function
M-C-\indent region
C-c C-qindent the whole function according to indention style
C-c C-ccomment out marked area
M-x uncomment-regionuncomment marked area
M-,jumps to the next occurence for tags-search
M-;insert comment in code
C-x w hhighlight the text by regexp
C-x w rdisable highlighting the text by regexp

To run emacs without X add -nw command line argument.

To run multiply commands 'C-u {number} {command}' or 'M-{digit} {command}'.

emacs has bookmarks that are close to Vim marks:
C-x r mset a bookmark at current cursor position
C-x r bjump to bookmark
C-x r llist bookmarks
M-x bookmark-write write all bookmarks in given file
M-x bookmark-loadload bookmarks from given file

My ~/.emacs looks like:
(setq load-path (cons "~/.emacs.d" load-path))

(auto-compression-mode t) ; uncompress files before displaying them

(global-font-lock-mode t) ; use colors to highlight commands, etc.
(setq font-lock-maximum-decoration t)
(transient-mark-mode t) ; highlight the region whenever it is active
(show-paren-mode t) ; highlight parent brace
(global-hi-lock-mode t) ; highlight region by regexp

(column-number-mode t) ; column-number in the mode line

(setq make-backup-files nil)

(setq scroll-conservatively most-positive-fixnum) ; scroll only one line when I move past the bottom of the screen

(add-hook 'text-mode-hook 'turn-on-auto-fill) ; break lines at space when they are too long

(fset 'yes-or-no-p 'y-or-n-p) ; make the y or n suffice for a yes or no question

(setq comment-style 'indent)

(global-set-key (kbd "C-x C-b") 'buffer-menu) ; buffers menu in the same window

(global-set-key (kbd "C-x 3") 'split-window-horizontally-other) ; open new window horisontally and switch to it
(defun split-window-horizontally-other ()
        (other-window 1)

(global-set-key (kbd "C-x 2") 'split-window-vertically-other) ; open new window vertically and switch to it
(defun split-window-vertically-other ()
 (other-window 1)

(global-set-key (kbd "C-c c") 'comment-region) ; comment code block
(global-set-key (kbd "C-c u") 'uncomment-region) ; uncomment code block

(global-set-key (kbd "C-x TAB") 'tab-indent-region) ; indent region
(global-set-key (kbd "C-x <backtab>") 'unindent-region) ; unindent region
(defun tab-indent-region ()
 (setq fill-prefix "\t")
    (indent-region (region-beginning) (region-end) 4)
(defun unindent-region ()
    (indent-region (region-beginning) (region-end) -1)

(global-set-key (kbd "TAB") 'self-insert-command)
(global-set-key (kbd "RET") 'newline-and-indent)

(setq key-translation-map (make-sparse-keymap))
(define-key key-translation-map "\177" (kbd "C-="))
(define-key key-translation-map (kbd "C-=") "\177")
(global-set-key "\177" 'delete-backward-char)
(global-set-key (kbd "C-=") 'delete-backward-char)

(setq indent-tabs-mode t)
(setq tab-always-indent t)
(setq default-tab-width 4)

(setq inhibit-startup-message t) ; do not show startup message

(iswitchb-mode t)
(desktop-save-mode t)


Happy emacsing!


geocar said...

Why not use Vimpulse which adds visual mode (and other VIM stupidity) to Emacs?

Cheba said...

In you a traitor I see.

Ni@m said...

I don't want to use emacs as I used Vim. It's another universe.
I'm enabling transient-mark-mode to view the marked area.

To be honest I'm happy that tried emacs. It's more comfortable than Vim.
I still have to explore its power and bind(rebind) functions to the keys with which I'm comfortable.

Ni@m said...

>In you a traitor I see.
The force is strong with this one.

Anonymous said...

I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.


Ni@m said...

Thank you, packey!
I'm pleased that you found this blog interesting!