Item 10: Master the fine-grained text manipulation commands
条款10:掌握一套一致的文本处理命令
Emacs provides many small but useful commands for doing surgery on text. They add up to laserlike efficiency.
emacs提供很多小巧实用的命令来对文本进行外科手术作业,极大地提升了编辑效率。
For starters, don't be tempted to re-bind Ctrl-k to a function that kills the whole line including the newline. I know that's the way kill-line works in all other editors. But it's clumsy and coarse compared to the way kill-line works by default. The default behavior, which kills the text to the end of the line but doesn't kill the newline, gives you finer-grained control, and leads to more efficient text surgery on the whole. Trust me: all Emacs users use other applications that only support the fat-fingered kill-whole-line version, so they've had plenty of opportunity to use both approaches. There's a reason the default is the way it is.
如果是初学者,不要经不住诱惑把ctrl-k重绑定到一个删除整行的命令上。我知道,这是很多其它编辑器的"删行"命令。但相比于kill-line的默认行为,这显然很粗造。默认行为,删除到行末而不把行末换行符删除,会让你有更细致的操控,当你在对全盘文本进行外科手术作业时这会提高效率的。相信我:所有的emacs用户都用过那些只支持鲁莽"kill-whole-line"的编辑器,他们早就用过了,但还把kill-line的默认行为设成现今这个样子,不是没有原因的。
Keyboard Macros.
键盘宏记录
I believe I can state without the slightest hint of exaggeration that Emacs keyboard macros are the coolest thing in the entire universe. They are, in a sense, as fine-grained and special-purpose as it gets, because you create them on the fly to solve specific editing problems.Whenever you find you need to make a specific, patterned change more than, say, 10 to 15 times,create a keyboard macro. It's really easy to do.
我可以毫不夸张地说,emacs的宏是全宇宙最酷的东东了。从感觉上讲,它们是一种特定用途的细致操作,因为你当即把它们做出来,然后就当即用来解决手头特定的问题。只要你觉得自己要进行一组特定的,有迹可寻的操作,而且不止一次(比如10次,15次)时,写一个键盘宏,这真的非常简单高效。
First it helps to do a trial run on the macro. Once you've done that, put your cursor at at the beginning of the first place to change, and use Ctrl-x ( to start recording the macro. Make your edits, and make sure to put the cursor in the corresponding place on the next line (or several lines down, as appropriate), so the macro will execute exactly the same pattern every time. To stop recording, type Ctrl-x ), and to invoke the macro, use Ctrl-x e (call-last-kbd-macro).
先试运行一下宏是有益的。首先把光标移到你第一个要改动的地方,按下Ctrl-x ( 启动宏记录。继续你的编辑操作,确保你的光标刚好在下一行(或者说下几行,视情况而定),这样就可以按模式运行了。用Ctrl-x )来结束宏录制。再来就是用Ctrl-x e(call-last-kbd-macro)来调用宏了。
It's something of an art to define a robust macro -- you learn to use anchors like beginning-of-line and end-of line to make sure the macro is in a stable point before adding another action to it.
有时,定义一个可靠的宏算得上是一种技艺――你得学会在添加操作之前,使用 beginning-of-line和 end-of-line 这样的锚点来保证宏操作在一个可确定的位置上。
And incremental-search is useful for skipping forward to the next place to invoke the macro: if you use isearch within the macro, to find the place to start, then each invocation of the macro later will automatically perform the search each time. Very convenient.
递增搜索可以很方便地跳过下一个位置。如果你在记录宏时使用了isearch,那么宏调用时,isearch也会被执行,非常方便。
And it's OK to make minor navigational mistakes while recording the macro. Just move the cursor back to where it should be and keep recording. The mistake will be replayed every time you execute the macro, but it'll happen so fast you'll be unlikely to notice.
在记录宏时有时会有些小的失误,这不要紧,直接改正这个失误,继续录宏。在宏调用时,这个失误会反复出现,然后反复被改正,而且很快,你几乎注意不到。
The trick to getting good at macros is to be persistent: make sure you get the macro working, and don't give up, even if it means spending more time fiddling with the macro than making the edits manually. The next time around it'll be easier, and eventually they'll become second nature.Keyboard macros are one of the most powerful features of Emacs, and they can make your life much easier.
用宏的技巧是很死的:确保你的宏完成它的任务,并且不要放弃,哪怕有时录一个正确的宏比你手工编辑还慢。因为下一次你就会快很多,久而久之,会成为你的本能反应。键盘宏是emacs最强大的功能之一,它让你的生活更美好~~~
One last tip for keyboard macros: typically you'll play them dozens or even hundreds of times in a file, or across multiple files. You should bind call-last-kbd-macro to a single keystroke, such as the F5 key. You'll usually be "babysitting" the job, pressing the key over and over, watching the changes as you make them to ensure you don't hork something unintentionally. So it's fine to have it bound to a "faraway" key like F5. You can set it up like so:
(global-set-key [f5] 'call-last-kbd-macro)
最后一个有关宏的招数:有时候你得调用它们几百次,而且不止在一个文件中,可能在多个文件中。这时你应该把 call-last-kbd-macro 绑定到一个单次按键中,比如f5啦。这时你可以翘起二郎腿,反复地按那个键,再看看编辑器上文本的变化有没什么意外即可。所以把综绑定到一个很远的按键也是可行的:
(global-set-key [f5] 'call-last-kbd-macro)
Transpose-* functions
调换XXX的功能
Finally you'll find the transpose-* commands surprisingly useful once you get used to them, even though they look like gimmicks. Probably the most useful is transpose-words. You'd be amazed at how often you find yourself using this command, which is bound to Alt-t. It has two uses: swapping two adjacent words, and dragging a single word forward or backward in a sentence.
虽然看起来很古怪,但一旦你习惯了它们,你就会发现调换功能出奇地有用。可能最突出要是默认绑定到alt-t的transpose-words了:它的作用有两个,一个是调换连续的两个词,一个是把单词在句子中拖来拖去。
The transpose-words function is aware of mode-specific syntax and word boundaries, so, for instance, putting the cursor between these two words:
([this])-is
and hitting Alt-t will result in:
([is])-this
transpose-words对mdoe特定的语法和词法边界是有感知的,比如。把光标放到下面两个词的中间:
([this])-is
然后按alt-t,结果变为:
([is])-this
You can transpose words across hyphens, HTML tags, and basically all punctuation. It's a pretty handy feature when you need it.
你可以在连字符,html标签,和其它标点符号之间调换单词。这是一个急你所需的灵活功能。
When you transpose 2 words, e.g. "dog food", the leftmost word swaps with the rightmost (assuming the cursor is somewhere between the beginning of the first word and the end of the second one),resulting in "food dog". But the cursor moves so that it's still to the right of the word that moved to the right. So if you apply it repeatedly, the word moves along the sentence to the right.I don't think there's a built-in way to make a "drag-word-left", but it would be easy to write a short function to do it. The corresponding Eclipse plug-in would be 5,000 lines of code in 60 source files and would take nine days to write and debug.
当你调换两个词时,比如"dog food",左过的词与右边的词互换了(这里假定你把光标放到两个词中间),变成"food dog"。但是这时的光标是还是在被移到右边来的这个词(food)的右边。所以如查你不停地按调换键,那个词就不断地在句子中向右边爬行驶。我不知道有什么内置的方法可以让一个词往左边爬的,但是如果要写一个的话,也一点也不难。相应的eclipse插件,可能就得要5000行代码,分布在60个文件中,得花个9天时间来编写和调试了...
You can also transpose chars, lines, sentences, and paragraphs. These are all useful as you perform editing operations on your raw text and even your source code. Experiment with them and try to remember that they're there, and eventually they'll also become second-nature.
你还可以调换字符,行,句子,段落。无论你在编写纯文本还是程序时,这些都是很有用的,试验并试着记住它们,目标是形成本能反应~