Chinaunix首页 | 论坛 | 博客
  • 博客访问: 714879
  • 博文数量: 165
  • 博客积分: 8218
  • 博客等级: 中将
  • 技术积分: 1749
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-07 19:48
文章分类

全部博文(165)

文章存档

2014年(4)

2011年(3)

2010年(6)

2009年(43)

2008年(109)

分类: LINUX

2008-09-10 15:25:58








Vi and Emacs

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.








Vi and Emacs are alike:

  • Full-function editors
    • Indenting, reformatting.
    • Syntax highlighting.
    • Unlimited undo and redo.
    • Multiple cut and paste clipboards.
    • Spell checking.
    • Multiple language support (including Unicode, R-to-L for Hebrew, Arabic, etc.)
    • Extendable
      • Macros and functions.
      • Fully customizable keys vs. actions.
    • Handle text strings, full lines, rectangles.
    • Adapt to type of file (.txt, .c, .html, etc.) being edited.
    • No line length or file size limits, within reason.
    • Automatically handle UNIX, MAC, DOS line endings (LF, CR, CRLF respectively).
    • Binary file editing.
    • Well documented, both internally and via available books and ebooks.
  • TTY and GUI versions.
    • The command 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.
    • The command vi[m] always starts in TTY mode, even in an Xterm session; use gvim or vi[m] -g to start the graphical version.
  • Implemented on many platforms:
    • Unix/Linux
    • Win32
    • Mac
    • Etc.
  • Widespread Availability
    • Both will be on any Unix/Linux machine
    • Freely available for download for Windows, Mac, etc.
  • Several versions available:
    • The original GNU Emacs from RMS, and Xemacs, forked from an earlier version of GNU Emacs source, are the most common. Other editors like Epsilon and JOVE are Emacs derivatives.
    • The original UNIX Vi (which almost nobody uses), Nvi (New Vi), Vim (Vi Improved), Vile (Vi like Emacs), Elvis.
    • For Linux distributions at least, "emacs" gives you the GNU version, and "vi" gives you vim.







Vi and Emacs are different:

  • Most simply, Vi was designed to write PROGRAMS, with all other types of file possible. Emacs was designed to write TEXT, with all other types of file possible.
  • Vi is much smaller and loads much faster; it will fit on a diskette while Emacs quite definitely will not.
  • The Biggie: Emacs is modeless, Vi has modes.
    • This relates to how an editor performs its two main functions: entering text and executing commands relating to the text.
    • Most modern editors and word processors are modeless, so that a user may enter text (e.g., typing "A") or a command (^S to save the file) at any time.
    • Vi has three modes:
      • Text insert mode
      • Keystroke command mode (vi mode)
      • Command line mode (ex mode)
  • Plus and Minus for modeless (Emacs):
    • (+) Familiar.
    • (+) Easily adapts to standard GUI pull-down menu style.
    • (-) Commands have complicated syntax and hard-to-type keystrokes, since a limited number of keys are available for commands.
    • (-) Some needed keys might not be available on all keyboards.
  • Plus and Minus for modes (Vi):
    • (-) Confusion about which mode program is currently in.
    • (-) Nuisance of switching modes frequently.
    • (+) Many more keystrokes (~90) available for commands.
    • (+) Touch typists tend to become very proficient because the fingers can stay in their normal positions almost all the time.
  • The bash shell can be configured for either style through the commands set -o emacs (the default) or set -o vi







Vi and Emacs Keystroke Usage

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:

  • Esc,a That is, hit the Escape key, let go, then hit the "a" key. This will work on all keyboards.
  • Alt-a That is, while holding down the Alt key, press "a", then release both. This will usually work on a local PC keyboard (one actually attached to the machine where the editing is taking place), but almost certainly will not work on a remote terminal.







Vi Mode Navigation

  • [ESC] always enters vi (keystroke command) mode. If already there, it just beeps. (Hint: Bottom line is blank)
  • From vi mode, colon (:) enters ex (command line) mode. (Hint: colon (:) and cursor show on bottom line)
  • From vi mode, various commands enter insert mode: i (insert), a(append), o (open line), cw (change word), etc. (Hint: Cursor doesn't move, --INSERT-- shows on bottom line)







GUI vs TTY

  • The Vi TTY program loads very quickly, and is particularly useful for small changes to files.
  • SSH and Telnet are text-only, so you need to use the TTY version of either editor if remotely connected.
  • If you plan to spend a long time in the editor, the GUI versions are the better choice:
    • They start in different windows, so the command line is still there.
    • Font size, color scheme, window dimensions easily adjustable.
    • Editing several files at once is easier.







Emacs LISP

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 Structure

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.)

Bottom Line

I use both editors regularly. Sometimes the choice is random, but for some tasks I will always use either Vi or Emacs.

  • Emacs GUI is much easier to use for large text jobs - writing the Great American Novel or whatever - and producing a "clean" file.
  • Vi TTY is always used for editing .rc files, producing quick-and-dirty scripts, etc.
  • I have extended Emacs to perform a couple of tasks that neither editor will do automatically in their virgin state.
  • Gvim is better integrated with Xwindows - easier "look and feel", better fonts, better syntax highlighting, a button bar, etc. It looks very different from the TTY version, whereas the TTY and GUI versions of Emacs look very much the same, but some of that is because Emacs managed to implement a "pull-down" menu system in TTY mode!
  • Emacs is better at editing many files at once. It is simple, for example, to switch to a directory containing a programming project and say: 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.






    Useful Links

    My .emacs customization
    My .vimrc customization



    You need files gvim70.exe and vim70d32.zip.
    Click on "Install Now"
    , publisher of full books and pocket references for both GNU Emacs and Vi (including Vim).
    , publisher of Vi Improved, by Steve Oualline.


    Last modified: Wed Apr 11 16:28:18 2007
  • 阅读(2059) | 评论(0) | 转发(2) |
    0

    上一篇:ubuntu8.04优化

    下一篇:我是一条内存

    给主人留下些什么吧!~~