敏捷之道——提高命令行编辑速度
1. Motion and Objects
Characters
C-fforward-char
. Moves forward (to the right) over a character.C-bbackward-char
. Moves backward (to the left) over a character.The f for forward and b for backward mnemonic will reoccur.Words
M-fforward-word
. Moves forward over a word.M-bbackward-word
. Moves backward over a word.Note the f/b mnemonic. Also, as another mnemonic, note that M-f is like a "bigger" version of C-f.Lines (vertically)
C-nnext-line
. Moves down to the next line.C-pprevious-line
. Moves up to the previous line.When moving by lines, the cursor tries to stay in the same column, but if the new line is too short, it will be at the end of the line instead. This is very important: Emacs doesn't insert spaces at the ends of lines (end of line is unambiguous).Lines (horizontally)
C-abeginning-of-line
. Moves to the beginning of the current line.C-eend-of-line
. Moves to the end of the current line.e for end, a for the beginning of the alphabet.2. Deleting, Killing and Yanking
Emacs' deletion commands are also based on the textual objects above. But first, a terminological distinction: Deletion means to remove text from the buffer without saving it; most deletion commands operate on small amounts of text. Killing means to save the removed text, so that it can be yanked back later someplace else.Killed text is saved on the kill ring. The kill ring holds the last N kills, where N is 30 by default, but you can change it to anything you like by changing the value of the variable kill-ring-max. The kill ring acts like a fifo when you're killing things (after the 30th kill, kill number one is gone), but like a ring when you're yanking things back (you can yank around the ring circularly). kill-ring-maxdoesn't apply to the amount of text (in bytes) that can be saved in the kill ring (there's no limit), only to the number of distinct kills.
Characters
C-ddelete-char
. Deletes the character to the right of (under, if the cursor is a block that covers a character) the cursor.DELdelete-backward-char
. Deletes the character to the left of the cursor.Remember, Emacs does not use C-h (aka Backspace) for deletion, but for help! Depending on your cultural background, this may well be the single hardest thing to learn about Emacs. Why does Emacs make this choice? Well, since there's no need for two commands to delete one character backward, and since DEL (aka Delete) can only be mnemonic for deletion while C-h is a nice mnemonic for help, the choice was made. Me, I've never used C-h to delete, so it suits me fine.说明:DEL是指backspace键
Words
M-dkill-word
. Kills to the end of the word to the right of the cursor (forward).M-DELbackward-kill-word
. Kills to the beginning of the word to the left of the cursor (backward).Lines (horizontally)
C-kkill-line
. Kills to the end of the current line, not including the newline. Thus, if you're at the beginning of a line it takes two C-k's to kill the whole line and close up the whitespace.C-ukill-line
. Kills to the beginning of the current line, not including the newline.这两个命令基本是我最常用的了。3. Infinite Undo with Redo
One of the most important Emacs commands isundo
, invoked with C-_ (control underbar). C-_ is a valid ASCII character, but some keyboards don't generate it, so you can also use C-x u -- but it's more awkward to type, since it's a two-character command.The undo command allows you to undo your editing, back in time. It's handy when you accidentally convert all of a huge file to uppercase, say, or delete a huge amount of text. One keystroke changes everything back to normal.
We say Emacs has infinite undo because, unlike some editors, you can undo a long chain of commands, not just one previous one, even undoing through saves. We say Emacs has redo because you can reverse direction while undoing, thereby undoing the undo.
Once you get used to this feature you'll laugh at any editor that doesn't have it (unless you're forced to use it...). It's very important to get comfortable with undo as soon as possible; I recommend reading the undo section of the manual carefully and practising.
4. Searching and Replacing
Emacs has a variety of unusual and extremely powerful search and replace commands. The most important one is called incremental search. This is what the commandisearch-forward
, bound to C-s, does: it searches incrementally, one character at a time, as you type the search string. This means that Emacs can often find what you're looking for before you have to type the whole thing. To stop searching, you can either hit RET or type any other Emacs command (which will both stop the search and execute the command). You can search for the next match at any point by typing another C-s at any point; you can reverse the search by typing C-r; and you can use DEL to delete and change what you're searching for.isearch-backward
, bound to C-r, works the same way, but searches backward. (Use C-r to search for the next match and C-s to reverse the search.)
Occasionally you may want to search non-incrementally (though I rarely do). You can do this by typing C-s RET text RET, where text is the text to search for.
Much more useful is word search, which lets you search for a sequence of one or more words, regardless of how they're separated (e.g, by any number and combination of newlines and whitespace). To invoke word search, type C-s RET C-w word word word RET.
Emacs can also search incrementally (or not) by regular expressions; this is an extremely powerful feature, but too complex to describe here.