4
16
2011

### Effective Emacs中文版

Emacs是世界上最好的编辑器(真的有很多人这么认为)。不要以为emacs只是在编写程序时很牛X，其实只要你真正精通了emacs，会发现她几乎在所有用到打字的应用(比如写email啦，起草文档啦，写blog啦，写html/xml文件等等等)时都是最牛的。

IDE还具备重构功能，一般什么多人都爱拿 这个来吹，因为重构工具可以自动地帮你整理结构不良的代码。没错，在某些程度上来说。但是偶告诉你个秘密喔：重构工具不懂英文耶（所以更不懂中文啦，55）。重构工具一般只能处理一些特定的文本（前面所说的“某种程度”），当她碰上其它编辑工作时，就无能为力啦。重构工具只能解决一小部分问题，而emacs却几乎能在你进行任何一类编辑时提供一系列条理清晰的操控能力。

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout

XXXXX不用译了。

0000: 00 00 00 00 00 00 00 00
0008: 03 00 00 00 3A 00 1D 00
0010: 1D 00 3A 00 00 00 00 00

!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L

xmodmap ~/.xmodmap

http://www.macosxhints.com/article.php?story=20031102032521826

http://www.eecs.wsu.edu/%7Eschneidj/mac-os-x-10.3.html#swap

http://www.manicai.net/comp/swap-caps-ctrl.html

Alt-x是最常用的emacs组合键，每次使用时你都得把左手蜷起来。任何一个你要做几千次的动作最好应该是流线型的，所以你最好希望可以用Ctrl键来调用M-x。（要想用Ctrl舒服，你得选把条款1完成了）

(global-set-key "\C-x\C-m" 'execute-extended-command)
(global-set-key "\C-c\C-m" 'execute-extended-command)

emacs高手一般都尽量避免使用backspace键，因为它离手的基本方位太远了。如果你经常打错字，但是你的速度又很快，一分钟打50个词以上的话，把整个词删掉重打可比勤勤恳恳地用backspace倒删到你打错的地方再从一半打起要经济实惠得多。

(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)

1.如果我打错一两个字符，而此时我的光标刚好就在错误的旁边，我就用backspace键。

2.如果错误字符在光标前面15到20个字符左右的地方，那我一般就使用Ctrl-W直接杀回去，然后重新打出这些词来。

emacs高手总是设法让他们的会话窗口越高越好，最好是能垂直地填满整个屏幕--因为垂直的屏幕空间在你查看一个文档时可以说是最宝贵的空间了。当你使用屏幕空间一次性查看多行文本时，使用递增式搜索一般比使用鼠标直接定位要快得多。

emacs最强大的功能之一就是她可以迅捷地生成一个不与任何一个文件关联的buffer。一旦你习惯了使用这一技术，你就对明显地感觉到其它编辑器这一功能性的不足。

* ctrl-x 2: split-window-vertically -- 把你的当前window切成上下两个等高并且显示同一buffer的window（在你改变其中一个让它显示其它buffer之前）

* ctrl-x 3: split-window-horizontally -- 很多人并不常用这个，但有时它非常有用喔。--把当前window切成左右两个等宽window。

* ctrl-x +: balance-windows  -- 让所有可见的window近似等高。如果你刚用ctrl-x 2两次，那你就有两个1/4高的window和一个1/2高的，使用这招可以让这三个window变得等高。

* ctrl-x o: other-window --把光标移动到window列表的下一个window当中去，一般会把光标移到window下面一个window，或者回滚到最顶的window。

* ctrl-x 1: delete-other-window -- 让当前光标所在的window填满整个frame；其它的window都会消失。请注意buffer是由buffer-list的维护的，所以无论什么时候运行这个命令都是安全的啦。

(2006/01 /02加入的注解)最近看到一个人回复，此君因为条款7而对本文全篇不爽。显然，他就是那种非常习惯于使用鼠标啊菜单啊之类东东的人，因此，觉得被称为 “找不着北的新丁”很不爽。此君进一步阐述，说有无数的研究表明使用鼠标更加快捷。于是乎我也觉得自己应该把事情说清楚一点好：条款接下来的部分是全新 的，这多亏了这位不忿的读者的Blog。

bury-buffer         M-x bury-buffer RET Command: Put BUFFER at the end of the list of all buffers.（译注：帮助真的是英文，要养成看E文的习惯啊）

emacs的正则表达式有些大伙儿都不太喜欢的特质，但这并不是不可克服的，一旦你学到手，你的编辑功力会精进的喔~

(defalias 'qrr 'query-replace-regexp)

emacs 正则表达式最常被问到的是：“怎么在正则表达式或者替换字符串读取时输入回车呢？” 如果仅仅简单地直接打回车，那emacs会认为你把regexp输完了。（这也是推荐qrr而不是 replace-regexp的原因啊――――在你非常有信心一次试写就可以把正则表达式写对之前，至少，我还没到那个境界）。

* 在elisp代码中，你写两次转义（"\\"），而在minibuffer输入时，你只写一次转义就OK了。
* 由于emacs代码有很多要匹配括号，所以emacs反转了括号的语义，在emacs的regexp中，"(",")"匹配实际的括号,而"\)","\("则用以建立匹配组（字串）。
* 在替换串中，使用\1 \2 ...来插入匹配组中的字符串
* 如果你输入的regexp没有正确工作，把结果undo掉，重新输一次命令，当提示输入表达式时，使用上下箭头按键可以找出你之前输入过的记录。这样可以习省时间开销和防止混乱。

emacs提供很多小巧实用的命令来对文本进行外科手术作业，极大地提升了编辑效率。

(global-set-key [f5] 'call-last-kbd-macro)

transpose-words对mdoe特定的语法和词法边界是有感知的，比如。把光标放到下面两个词的中间：
([this])-is

([is])-this

1。 fill-paragraph (alt-p) -- 智能地帮你把文本分行，这是必备良药啊，在注释里面都能用。
2。 gnuserv: 自动用emacs来开启特定文档（包括你浏览器的"view source"）。
3。 M-x dired: 一个很强的文件/目录管理工具，它提供了一些其它工具完全没有的强大功能（至少据我所知），比如用regexp把某个用户组的文件重命名。
4。 whitespace处理命令：C-x C-o（delete-blank-lines）, delete-trailing-whitespace, tabify 和 untabify 等等。。。。
5。 nxml-mode: 唯一让你在处理xml时编辑如飞的方法，作者是xml高手James Clark；完全把其它基于IDE的XML编辑器比下去了。。
6。 picture-mode: 搞ascii艺术时最好用的东东。
7。 minibuffer管理: 掌握递归编辑，在各种情况下怎样中断退出，命令补全及其它各种输入技巧。
8。 不费吹费之力的导航: 绑定一些各个方向移动光标的命令:以字符为单位，以词为单词的。使用alt加某个字符的快捷键组合。
9。 区域管理: 选一个不太难看的区域高亮颜色。
10。 矩形区域命令: 这是另一组很神奇的命令（译注：不过很多编辑器现在也都有这功能了）。
11。 emacs shell: 高效使用emacs shell的技巧。
12。 align-regexp: 我新收藏的命令，最近才学到的，几乎天天都用到。
13。 frame初始化: 如何在每次打开emacs时，自动根据显示器尺寸把emacs窗口设定好。
14。 使用goal column: 每个emacs强人都该知道的。
15。 设定fill column: 最大发挥 fill-paragraph 和 fill-region的功能。
16。 优化操作系统设定: 设定键盘重复率，设定好的emacs字体，等等。
17。 浏览编辑归档文件: tar,gzip,zip,jar等等。这没什么奇怪的。
18。 高级绑定: 学习绑定的语法，home/end等其它不常绑定的按键，学习如何特定于buffer来绑定按键。
19。 掌握 kill ring， 包括用 Derek upham的新mode来查看它的内容。
20。 掌握 Info: 定制你的Info目录，查找和添加新的Info文档，高级导航及书签功能。
21。 使用好 M-x customize: 学习它的运作，以及怎么避免使用它。
22。 工具程序: M-x calendar, M-x calc, 及其它。

++

To yank at the i-search prompt, use M-y instead of C-y. The emacs info node on Incremental Search talks about the rebinding of C-y, C-w, etc at the i-search prompt.

To repeat execution of the last kbd macro, after hitting C-xe to run it once, keep hitting just the 'e' key for repeated execution.

Zap-to-char (M-z) is incredibly useful if you need to change myDatafileHandler to myStreamHandler and point is at D (the first char to change). Simple M-z e to zap the "Datafile" part and type in the replacement. This is not orthogonal to the backward-kill-word (bound to C-backspace for me since that works in windows browser windows as well as everything else) so there's a feel needed for which is optimal when, which for me mostly depends on where point is already.

Posted by: Ami F. at January 23, 2005 07:12 PM

"Swap Caps-Lock and Control": Or you could just get yourself a keyboard that's already swapped, or that lets you swap them in hardware. The Happy Hacking Keyboard is an example of the former. I like the Avant Stellar keyboard for the latter.

"Binding Alt-r and Alt-s": Try setting up a minor mode with the definitions you want. Then stick that keymap on the *front* of MINOR-MODE-MAP-ALIST.

Other tips:

Use iswitchb' mode. It's faster for switching between buffers, and it provides more feedback.

Emacs' integration with X11 selections is written to work well with xterm's sucky default policies. That means it works badly with modern apps and badly over slow network connections (say, a VPN from home). I have written a new set of commands to make Emacs talk to the clipboard, and they make life much easier.

Posted by: Derek U. at January 24, 2005 07:37 PM

Doing the swapping of control/cap-lock key can be done in \\HKEY_CURRENT_USER\Keyboard Layout instead of under \\HKEY_LOCAL_MACHINE.

Posted by: Chris W. at January 27, 2005 07:52 AM

I disagree with your agressive rebinding of keys. I used to rebind almost all my emacs keys so they would be more familiar to a windows user (Ctrl+C does copy, Ctrl+V is paste, etc). However, I found myself at a loss when I tried using my neighbor's computer with the default emacs installed.

Always learn default emacs keybindings first, then over-write them as you find appropriate. For example, I rebound Ctrl+J to be the goto-line macro. By default, Ctrl+J enters a newline. F7 is not bound to anything by default, so I made that bound to the compile macro (that keybinding actually comes from Visual Studio). That is about the extent of the keybindings I need/use. And, if I have to use a non-customized emacs, I can still get work done.

Cheers,
-Brian

Posted by: Brian M. at January 27, 2005 10:29 PM

To not pursue aggressive editor and keyboard customizations because other people stick to the standard is a bogus argument, in my opinion. Firstly, how often do you really use a terminal other than your own? And even then, how often do you write just scads of code there? When I do this, it's usually for just a couple of quick edits. So, why optimize for that 1% of time when you're not at your own setup? Secondly, if it does annoy me enough to care, I can always just load my rc file remotely. I use vim and have my .vimrc hardlinked into my /workplace directory so I can just say vim -u /net/ericw/workplace/.vimrc if I really need my magic. I'm certain the same can be done in emacs.

Customization is one of the main selling points of powerful editors, and our wrists are two of our most valuable assets as developers, so I don't understand why people eschew customization just because they fear that small percentage of the time when they won't have it.

Posted by: Eric W. at January 28, 2005 07:34 PM

Brian, I'm afraid I'm with Eric on this one.

I have friends who have nonstandard keyboards, in some cases to avoid repetitive-stress injury. I can't type at their workstations. I have this little secret, though, that works like a charm. I say: "uh, you type."

OK, not much of a secret, but it's gotten me by.

Everyone customizes their environment. Some SDEs use fonts so small I actually can't read them. Some use custom window managers with non-CUA hotkeys and behavior. People use Windows, Linux, MacOS. There are different Unix shells with different default keybindings and aliases. Should we tell everyone they have to use plain-vanilla Windows installations with no customizations?

You're effectively arguing that we should all reduce ourselves to the least common denominator of productivity. This argument has been debunked in other domains (e.g. should we make people with good vision wear blur-inducing glasses, so nobody feels like they're at a natural disadvantage?), and it doesn't hold water in ours either.

You're welcome to use the default bindings yourself, of course. But I wouldn't get on a bandwagon that tries to discourage people from getting better at their jobs. It's a slippery slope that I think you want to avoid.

Anyway, we now issue laptops with wireless iVPNs, so you can even bring your environment with you. It's just not an issue anymore.

Posted by: Steve Yegge at January 28, 2005 09:30 PM

I just want to know the tips you allude to in number (8) of your "Tune in next time..." section. How do you do the up/down/left/right browsing? I've been trying to train myself to use C-n, C-p, C-f, C-b and friends, but its awkward, and it isn't getting easier. Also, can I plllleeeeasssseee see your .emacs file :)

Posted by: Charles G. at February 18, 2005 01:47 AM

On March 4 2006, Luke Gorrie wrote:

Howdy,

I know it's bad form to comment on a blog entry without having read it thoroughly but I will take a chance because your eternal salavation is at stake.

I use C-h for backspace in Emacs and move help-command' elsewhere:

(global-set-key "\C-h" 'backward-delete-char-untabify)
(define-key isearch-mode-map "\C-h" 'isearch-delete-char)
(global-set-key [(hyper h)] 'help-command)

and this also works in the shell along with C-m, C-j, C-i, etc.

On March 5 2006, Anupam Kapoor wrote:

hi,
regarding tip #7, imho, its better to just disable it via Xresources,
rather than loading it all up and making it invisible (as you have
shown) via:

,----
| ! better to turn this off here than in .emacs
| emacs.toolBar: off
|
| ! no scrollbars fur me
| emacs.verticalScrollBars: off
`----

kind regards
anupam

On March 9 2006, Scott Anderson wrote:

Steve,

Great article.

Reading section 7, I became amused by the person who claimed that
"countless studies" (or whatever) had proven that the mouse was
faster.

I'm guessing he never studied GOMS, which is a method of decomposing
UIs into their basic operations and then performing objective
complexity and time analysis.

A quick typist can hit about 10 keys per second, or .1s per key. To
use a mouse there are four movements involved: move from the keyboard
to the mouse, move the cursor to a location, perform some operation at
the location, then move the hand back to the keyboard.

Moving the hand takes about .4s. Moving the mouse cursor on the screen
takes .5s. So without even doing anything, you've used 1.3 seconds. A
mouse click takes .1 seconds, and if you're dragging something, you
measure the click down, the drag, and the release, adding up to
another .7 seconds.

So let's say I want to highlight a line for copy:

mouse:
.4 move to mouse
.5 move cursor to first character
.1 depress button
.5 drag to highlight (and this is optimistic, depending on how good
you are with finicky targets like highlighting)
.1 release button
.4 move to keyboard
.2 copy (alt-w, ctrl-c, whatever. count control keys as a separate keypress)

2.2 seconds to perform.

kb scenario 1: best case: already at beginning of line
.2 mark (ctrl-space)
.2 end-of-line (ctrl-e)
.2 copy

.6 seconds to perform, or nearly 4 times as fast.

kb scenario 2: worst case: requires navigation: 30 lines down, line
.9s move to start of line (ctrl-r readi ctrl-r)
.2 mark (ctrl-space)
.2 end-of-line (ctrl-e)
.2 copy

1.5s, still faster

As it turns out, the only thing a mouse is really good for is
something involving a gross motor movement, like moving a window or
the like. Unless you're a crappy typist.

Anyway, good article, and I'm looking forward to reading the rest of
them.

Regards,
-scott

Back to Stevey's Drunken Blog Rants     image of my email address, steve dot yegge at ye olde gmail

Category: | Tags: | Read Count: 9522
OSMSG 说:
Apr 28, 2011 01:05:41 PM

(输入验证码)
or Ctrl+Enter

Powered by Chito | Hosted at is-Programmer | Theme: Aeros 2.0 by TheBuckmaker.com