`
arganzheng
  • 浏览: 103531 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

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

阅读更多

敏捷之道——提高命令行编辑速度
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.

分享到:
评论

相关推荐

    DOS实模式下的小巧文本编辑器

    描述中提到的“汇编语言制作”意味着这个文本编辑器是用低级编程语言——汇编语言编写的。汇编语言是一种符号化的机器语言,每一个指令都对应着处理器的一个具体操作,因此用汇编编写的程序通常更接近硬件,执行效率...

    Laravel开发-laravel-make-rest-resource

    要使用`laravel-make-resource`,你需要确保已经安装了Laravel的命令行工具——Artisan。在命令行中,你可以输入以下命令来创建一个新的资源控制器: ```bash php artisan make:resource <ControllerName> ``` ...

    svn和vss培训资料(打包在一起了有ppt和pdf)

    本文将深入探讨两种广泛使用的版本控制工具——Visual SourceSafe (VSS) 和 Subversion (SVN),适合初学者入门学习。 **Visual SourceSafe (VSS)** VSS 是微软开发的一款早期的源代码版本控制系统。它提供了基本的...

    Git官方安装包,给小水管下载

    安装过程中,用户可以选择配置项,如Git Bash模拟Linux命令行环境,以及是否设置Git为系统默认的文本编辑器。此外,安装包可能还包括Git GUI,这是一个图形化的界面工具,帮助不熟悉命令行的用户进行操作。 在日常...

    SVN.rar_pbscc 2.01.80

    在"SVN.rar_pbscc 2.01.80"这个压缩包中,包含的是SVN的一个特定版本——2.01.80,它可能是一个定制或者增强版,带有"PBSCC"的标识,这可能代表一个特定的项目、团队或公司对其进行了定制或集成。 源代码维护是软件...

    microservice-11:该存储库由CAE生成,它具有从microservice-11生成的源代码

    这暗示该项目遵循敏捷开发原则,利用自动化工具来提高效率和质量。 描述中的"纽米克"可能是项目的名字或者是服务的特定版本,这里可能是一个拼写错误或者专有名词的缩写,但具体含义需要更多信息来确认。"请通过...

Global site tag (gtag.js) - Google Analytics