简体   繁体   English

如何替换Emacs中的一个字?

[英]How to replace a word in Emacs?

Given the following sentence:给定以下句子:

This is one two three.这是一二三。

When I move the cursor to the space between one and two , I would like to replace two with XXX.当我将 cursor 移动到12之间的空间时,我想用 XXX 替换2 I know there is a way in vi so that I can mark two as the modified string so that when I finish entering the new string and the new string replaces the two in-place.我知道在 vi 中有一种方法可以将两个标记为修改后的字符串,这样当我完成输入新字符串并且新字符串就地替换两个字符串时。

// Update-1 // // 更新 1 //

The corresponding command in vi is 'cw' vi中对应的命令是'cw'

I would probably just use Md SPC XXX (if the SPC is necessary depends on wether the cursor is placed directly after "one" or before "two") or Mf M-DEL XXX , but maybe that's not what you're looking for.我可能只会使用Md SPC XXX (如果需要 SPC,取决于 cursor 是直接放在“一”之后还是“二”之前)或Mf M-DEL XXX ,但也许这不是你要找的。

Ok, you didn't answer my comment, and didn't vote up an answer, so I guessed what you might like and here is a little hack:好的,你没有回答我的评论,也没有投票给答案,所以我猜到你可能喜欢什么,这里有一个小技巧:

(defun change-word (n)
  (interactive "p")
  (lexical-let ((old-window-configuration (current-window-configuration)))
    (clone-indirect-buffer "*edit-word*" t)
    (narrow-to-region (point) (save-excursion
                                (forward-word n)
                                (point)))
    (overwrite-mode 1)
    (local-set-key (kbd "C-c C-c")
                   (lambda ()
                     (interactive)
                     (widen)
                     (let ((end-of-edit (point)))
                       (kill-buffer)
                       (set-window-configuration old-window-configuration)
                       (goto-char end-of-edit))))))

Invoke Mx change-word (or bind it to a key you like), edit the word, and type Cc Cc when you're done.调用Mx 更改单词(或将其绑定到您喜欢的键),编辑单词,并在完成后键入Cc Cc If you want to edit the next n words, give it a prefix argument (eg M-3 Mx change-word to change the next three words).如果要编辑接下来的n单词,请给它一个前缀参数(例如M-3 Mx change-word以更改接下来的三个单词)。 It's not exactly the same -- You'll edit in another buffer -- but it comes close.它不完全相同——您将在另一个缓冲区中编辑——但它很接近。 Try and see if you like it.试试看你喜不喜欢。 It is not a very elaborate solution.这不是一个非常复杂的解决方案。 Maybe the best, and most emacs-style approach, would be to have something akin to isearch-mode, that highlights the changed region and so on.也许最好的,也是最具 emacs 风格的方法是使用类似于 isearch-mode 的东西,突出显示更改的区域等等。 Note that you'll have to (require 'cl) because of lexical-let .请注意,由于lexical-let ,您必须(require 'cl)

Another possibility would be something like this:另一种可能性是这样的:

(defun change-word ()
  (interactive)
  (dotimes (i (- (save-excursion (end-of-thing 'word)) (point)))
    (insert (read-char))
    (delete-char 1)))

But this example is just a very crude hack -- You won't even be able to navigate while editing your word.但是这个例子只是一个非常粗略的技巧——你甚至无法在编辑你的单词时导航。 Maybe one could write something modifying the command-loop behavior, but I didn't look into that.也许有人可以写一些修改命令循环行为的东西,但我没有研究过。

I don't know about any built-in functionality that does exactly what you seem to want, but of course there are Viper and other Vi emulation modes built in.我不知道有什么内置功能可以完全满足您的需求,但当然还有 Viper 和其他内置的 Vi 仿真模式。

With the cursor before the letter t in "two," press M-@ or CM-Space to select the word "two," and then type XXX to replace it.用 cursor 在“二”中的字母t之前按M-@CM-Space到 select 单词“二”,然后键入XXX替换它。 In my Emacs, M-@ is bound to mark-word , and CM-Space is bound to mark-sexp .在我的 Emacs 中, M-@绑定到mark-wordCM-Space绑定到mark-sexp I find the latter easier to type, but the former is more likely to work as intended across various modes.我发现后者更容易输入,但前者更有可能在各种模式下按预期工作。

The nice thing about these commands is that repeated executions (via the aforementioned key sequences) extend the selection incrementally.这些命令的好处是重复执行(通过前面提到的键序列)逐渐扩展选择。 In your case, the first press of M-@ highlights "two":在您的情况下,第一次按M-@突出显示“两个”:

This is one |这是一个| two three.二三

Pressing M-@ again highlights "three" as well:再次按M-@也会突出显示“三”:

This is one |这是一个| two three .二三

On your keyboard, the meta key (denoted by "M" in these examples) may be bound to the Alt key.在您的键盘上,元键(在这些示例中用“M”表示)可能绑定到 Alt 键。

The following SO answer deals specifically with reproducing vim's word marking behavior. 以下 SO 答案专门处理重现 vim 的文字标记行为。 Then it's just a matter of killing the region ( Cw ) before typing.然后只需在键入之前杀死该区域( Cw )即可。

For the over all document editing in multiple cases like this, you might want to use Mx delete-selection-mode.对于像这样的多种情况下的全部文档编辑,您可能需要使用 Mx delete-selection-mode。 You can just select the 2nd word and start typing.您可以只输入第二个字 select 并开始输入。 Use C-M-SPACE (mark-sexp) for selection.使用C-M-SPACE (mark-sexp) 进行选择。

From your description, it sounds like M-backspace would delete the 'two' and leave the cursor ready to type in the replacement.根据您的描述,听起来M-backspace会删除“两个”并让 cursor 准备好输入替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM