Emace 文本操作

Emacs 编辑器使用缓冲区保存文件,不直接编辑文件。文章介绍了缓冲区的基本操作,如打开、保存、退出,以及编辑模式和常用快捷键。自动换行模式、文本改写模式、光标控制、文本删除和恢复等编辑技巧。还讲解了在平衡表达式中导航的方法,如括号匹配。提供了丰富的命令和快捷键,帮助用户提高编辑效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编辑器并不会对某个文件本身编辑,而是先放到临时缓冲区中。在通知保存缓冲区之前原始文件不会变。通常缓冲区名字跟文件名字一样,但是也有一些特殊缓冲区*scratch*临时性辅助缓冲区,有点像草稿;*help*显示帮助信息。

编辑模式:

一个缓冲区只能有一种主模式,在主模式之外还有一些副模式(minor mode),是在定义emacs某些特定行为。

menu bar

Tool bar

mini buffer(辅助输入区)

C-x something通常绑定文件操作类命令

C-c something 通常与某些特殊的编辑模式有关,比如图形模式和邮件模式

M-x long-command-name Return可以用于所有的命令


菜单栏(menu bar)

F10

C-x C-f 打开文件

C-x C-v 可以重新输入路径来替换刚才的缓冲区文件(find-alternative-file读取新的替换刚才的)

C-x i   在光标位置追加文件

C-x C-s 保存文件,如果是在*scratch*,他会让你输入一个文件名

C-x C-wwrite-file命令,如果你没有输入文件名的话,他等价与C-xC-s。否则相当与saveas

C-x C-c 退出Emacs


帮助

C-h   在线帮助help-command

C-h t 启动Emacs教程

C-h k 按键释义describekey

C-h f 函数释义describefunction


二:文件编辑

当你打字的时候后,到最右段的话,它不会自动换行,而是加一个箭头形式的东西,提示下面属于上一行,你或者通过键入enter手动换行,但是太麻烦。通过命令”M-x auto-fill-mode Return”可以实现自动换行。对于英文的话,因为空格是字段分割符,你可以想像一下,因为一个字母代表一个字符,当一个单词超过了80的时候,他会自动的把离它最近的空格后面的字母移到第二行。而中文的话,它是所以当到达最后的时候,你会发现一个中文占了两个字符位,加入在80列,你输入中文的话,这个中文会自动在下一行。总而言之,自动换行就是以字段分割符为界限,如果当一个单词或中文字在超出界限的时候还没有输完,这个时候就把正在输入的换到下一行。

一般默认的话,emacs窗口一行只能输入80个字符,你可义通过设置column进行更改,比如到了120。这个时候你要注意了因为你本身窗口并没有变大,所以在形式上输入第二行的前40个字符时,在实质上还是属于第一行的东西。

M-x auto-fill-mode Return自动换行,自动切换开关状态

文本改写模式:用新的字符取代就的内容

M-x overwrite-mode Return自动切换开关状态


基本的光标控制(BASIC CURSOR CONTROL

             上一行C-p

               :

向左移C-b.. 目前光标位置..向右移C-f

                                         :

                                  下一行C-n

C-f/C-b都是对字符或一个中文汉字而言。

M-f/M-b 英文的话,单词而言,中文的话,以非中文字符为界,可以时字符,空格,标点等。

C-a  行的开始

C-e  行的结束

M-a  一句的开头

M-b  一句的结束

M-<  所有文字的最开头

M->  所有文字的最末尾

C-v  或键入PgDn,命令是scroll-up,有两行重复的

M-v  或键入PgUp,向后移动一屏,有两行重复的


命令重复执行

C-u n n是前缀参数,如C-u5 C-p 向上移动5

C-u 8 = 插入8个'='字符


C-l  重绘屏幕,例如有邮件,talk聊天太杂之类,另一方面可以将要编辑的东西放到中心位置

C-x u 撤销上一次操作


文本删除

<Del>  删除光标前的一个字符

C-d    删除光标后的一个字符

M-<Del> 移除光标前的一个词

M-d    移除光标后的一个词

C-k    移除从光标到行尾间的字符

M-k    移除从光标到句尾间的字符

C-S-backspace C-y 复制一整行

C-S-DEL 删除一整行

通用删除方式

C-@C-SPC→键盘移动光标划出范围→C-w删除

C-w   删除所选区域

M-w   复制所选区域

恢复删除操作

C-y yank

M-y yank-pop


块的标记

M-h   选中一个段,并且鼠标在段的起始位置mark-paragraph

C-x h 相当与CTRL+a(mark-whole-buffer)

鼠标标记文本块

M-x transient-mark-mode Return临时标记模式,让你即使松掉鼠标,选中区仍旧是高亮

方法二:在起始位置点击鼠标左键,然后在最后位置点击鼠标右键


rect-mark 详见:定制Emacs环境 --16

C-x r C-@    mark rectangle


编辑技巧

交换位置

C-t     transpose-chars     放到第二个字符上键入命令交换两个字符的位置

M-t     transpose-words     放到单词之间,交换两个单词

C-x C-t transpose-lines     放在第二个文本行任意位置,键入命令交换两个文本行

       transpose-sentences  交换两个句子位置

       transpose-paragraphs 交换两个段落位置

改变大小写

M-c把光标所在位置字母变为大写

M-u单词变为大写,范围是从光标所在单词位置到该单词末尾

M-l单词变为小写,范围时从光标所在单词位置到该单词末尾


EMACS失去响应的时候(WHEN EMACS IS HUNG

C-g来安全地终止这条命令。C-g也可以终止一条执行过久的命令。如果你不小心按了一下<ESC>做法是再连按两次<ESC>


Navigating over balanced expressions

A balanced expression can be a parenthetical group, a number, a word or a symbol(type C-h s or F1 s to check). The precise definition of a balanced expression may depend on the current mode. Here is how to move from a balanced expression to another:

Here are a number of shortcuts for dealing with sexp:

  C-M-n     Move forward over a parenthetical group 
  C-M-p     Move backward over a parenthetical group 
  C-M-f     Move forward over a balanced expression
  C-M-b     Move backward over a balanced expression
  C-M-k     Kill balanced expression forward
  C-M-SPC   put the mark at the end of the sexp.

Here is a short example; let us consider the following sexp in text mode:

    (with-current-buffer "foo.tex" (insert " \\emph{first chapter} "))

If the cursor is before the first parenthesis, C-M-f puts it right after the last parenthesis. With cursor on the word current,C-M-f puts it at the end of the wordbuffer. If cursor is on the first “ character,C-M-f puts it just in front of the . character. Indeed, “ is not an open delimiter, so the cursor only moves forward one word. The { character howewer is recognized as a delimiter and the cursor will jump just after the corresponding }.

Now if we want to remove the second parenthesized group, when the cursor is on the parenthesis at the beginning of this group, we just typeC-M-k. If we want to put it in the kill ring without removing it, we first typeC-M-SPC, followed by M-w. We can then yank it somewhere else with the usual commandC-y.

These commands see nothing but parentheses (according to the syntax table; {} are considered as parentheses in text-mode for example).

Let us return to the example of the previous section. With cursor on the word current, C-M-f puts it at the end of the word buffer, becausewith-current-buffer is considered as an sexp in text-mode. On the other hand,C-M-n puts the cursor in front of the last parenthesis. That is, the cursor jumped over the next parenthesis group, given by(insert " \\emph{first chapter} ").



http://www.emacswiki.org/emacs/NavigatingParentheses

http://ergoemacs.org/emacs/gnu_emacs_keybinding.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值