分类:
2011-12-23 15:26:42
原文地址:vim vs emacs 作者:sparkzh
NOT "Vi vs. Emacs" -- Despite twenty or thirty years of abuse thrown at each other by adherents of the Church of Vi vs the Church of Emacs, I feel the two editors are complementary, rather than antagonistic. They have a very different "look and feel", but that's not a real reason for choosing one over the other. They were designed for different jobs, they are better at different things, and I use both of them, depending on the job.
Specifically, I use GNU Emacs and Vim, and every time I say "Emacs" or "Vi", assume these programs unless proven otherwise.
emacs
will start in either TTY or GUI
mode, depending on where it is invoked. Use emacs
-nw
to force the program to use an Xterm window in text
mode instead of starting its own.
vi[m]
always starts in TTY mode, even
in an Xterm session; use gvim
or vi[m]
-g
to start the graphical version.
bash
shell can be configured for either style
through the commands set -o emacs
(the default) or
set -o vi
Here are a few typical commands, showing the differences between
"mode" style and "modeless" style:
VI EMACS
-- -----
Right one column k ^f
Right one word w Esc,f
Left one word b Esc,b
Next sentence ) Esc,e
Previous sentence ( Esc,a
Save file :w ^x,^s
Delete paragraph d} Esc,x,kill-p[TAB],[RET]
Edit a new file :ename ^x,^f,name
RegEx search for "foo" /foo Esc,^sfoo
Repeat search n ^s,[RET]
Exit :q or QQ ^x,^c
Save and Exit :x ^x,^s,^x,k,[RET]
Repeat last search n ^s,[RET]
Paste from clipboard p ^y
Delete 7 lines 7dd ^a,Esc,7,^k
Undo u ^x,u or ^/
Change a letter to "x" rx ^d,x
Go to line 6 :6[RET] Esc,<,Esc,5,^n
..or 6G Esc,x,goto-l[TAB][RET],6[RET]
You can easily see Vi tends to have simpler commands (in command mode) because it has all the "ordinary" letters and numbers available for navigational use.
Note that the Emacs documentation makes frequent mention of the "Meta" key, including key sequences like M-a, etc. Since most keyboards do not have such a key, M-a, for example, can be done two different ways:
Emacs actually consists of a LISP interpreter executing a few pre-compiled primitive routines written in C plus about 200,000 lines of LISP code to implement all the functions of the editor. This makes Emacs extremely flexible, since an experienced LISP programmer can change anything and everything, as well as create new actions the program's author never thought of.
Emacs can be made into an e-mail client, a web browser, a chess
opponent, etc. by simply adding the proper code. A single keystroke
can be mapped to execute an entire LISP program, which uses the file
being edited as its subject matter. For example, here is a segment of
my .emacs file, which defines a function called
lookat-file and then "binds" it to the CTRL-F key.
(defun lookat-file ()
"Edit file with name delimited by colon at beginning of current line."
(interactive)
(save-excursion
(save-match-data
(beginning-of-line)
(search-forward-regexp "^\\(.*\\):")
(find-file (match-string 1)))))
(define-key global-map "^F" 'lookat-file)
LISP functions are all defined the same way:
(funcname arg1 arg2 ...)
The last line in the example calls the function define-key with
three arguments: the variable global-map, the
literal ^F (the CTRL-F key), and the function
name lookat-file, previously defined.
All built-in functions (like define-key and variables (like global-map) are fully documented in the extensive Emacs help system. In the definition of lookat-file shown above, every single keyword is a built-in LISP function.
Note that Emacs is fairly easy to port to a new architecture, because the "look and feel" is entirely defined by the LISP code, and that is unchanged whether Emacs is running on Solaris, Linux, Win98, or whatever. The only code that has to be modified is the LISP interpreter itself plus the primitives that handle platform-dependent stuff like file i/o and the other interfaces to the OS.
LISP is actually very simple, because everything has the
same form, namely a list of items enclosed in parentheses, and
all program operations are function calls. Variables
are untyped. For example:
(+ 2 3) ; add 2 and 3, return 5 as the value of the function.
(setq foo "John") ; store the string "John" into the variable foo.
(setq foo (+ 2 3)) ; store 5 into foo
(setq foo (and huey dewey louie))
; set foo true if all three are true, else false
(setq bar (* (+ 2 3) (- 6 2) (* 2 2) (sqrt 9)))
; store 240 into bar.
(setq ans (if (< foo bar) 1 2))
; ans is 1 if foo is less than bar, 2 otherwise
(defun myfun (arglist) statements)
; define function myfun
From these examples, it is easy to see why it's a standard joke that LISP (which really stands for LISt Processor) is an acronym for "Lots of Irritating Silly Parentheses". BTW, Vi enthusiasts tend to claim that Emacs is an acronym for "Eight Meg and Continuously Swapping". Note that this joke has been around since the days when eight MB was a lot of memory. On the other hand, Emacs bigots will refer to Vi as "six". (In which case Vim would be what, 994?)
I have to correct one statement I made earlier. Actually, Emacs also
has a command-line. Typing Alt-x (or Esc,x) puts the cursor into what
Emacs calls the "mini-buffer" at the bottom of the screen, where the
user can execute thousands of built-in or user-defined LISP routines.
For example, I could execute my lookat-file function by typing
Alt-x lookat-file[ENTER]
. (Emacs has tab completion just
like bash or zsh, so in practice I would have typed Alt,x
loo[TAB][ENTER]
, there being no other LISP function on my
machine that starts with those three letters.)
I use both editors regularly. Sometimes the choice is random, but for some tasks I will always use either Vi or Emacs.
emacs Makefile *.[ch]
to edit the whole
project at the same time. Both editors can issue the actual "make"
from within the editor, capture and parse the error messages, and
position the cursor on the correct line in the correct file.