敏捷之道——提高命令行编辑速度

来源:互联网 发布:java泛型t 基本类型 编辑:程序博客网 时间:2024/04/30 07:52

敏捷之道——提高命令行编辑速度
2011-03-31 星期四 晴朗
经常在shell命令行下操作,如果能够节省一点时间,由于操作频繁,总的节约时间还是挺可观的。
shell命令行默认的编辑模式是emacs编辑模式。所以其实可以方便的是使用emacs的快捷键来提高工作效率。当然,你也可以配置成VI编辑模式,不过我觉得Emacs的几个快捷键挺好记的,我都已经习惯了,呵呵。
废话少说,直切正题。由于时间关系,我这里就直接贴了,耐心看,其实英文没有什么的。
说明:C表示Ctrl,M在我的Ubuntu下是Alt-Shift。

1. Motion and Objects

Characters

C-f
forward-char. Moves forward (to the right) over a character.
C-b
backward-char. Moves backward (to the left) over a character.
The f for forward and b for backward mnemonic will reoccur.

Words

M-f
forward-word. Moves forward over a word.
M-b
backward-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-n
next-line. Moves down to the next line.
C-p
previous-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).

说明:c-n和c-p可以在shell的history中一条条翻转,相当于向上向下箭头。

Lines (horizontally)

C-a
beginning-of-line. Moves to the beginning of the current line.
C-e
end-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-d
delete-char. Deletes the character to the right of (under, if the cursor is a block that covers a character) the cursor.
DEL
delete-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-d
kill-word. Kills to the end of the word to the right of the cursor (forward).
M-DEL
backward-kill-word. Kills to the beginning of the word to the left of the cursor (backward).

Lines (horizontally)

C-k
kill-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-u
kill-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 is undo, 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 command isearch-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.

这个命令实际上是特别好用的命令,当然对于很常见的比较长的命令,我们还是建立一个alias比较合适,但是对于你在本session中经常敲打的命令,C-r是非常好用的。C-s貌似不行。
另外,IPython使用跟shell一样的快捷键,当然,确切的说,是跟Emacs一样的快捷键。
EOF.

原创粉丝点击