如何在Emacs中替换一个换行符?

时间:2023-01-16 19:31:28

I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs.

我在试着换一个角色——说;-在Emacs中使用替换字符串和/或替换regexp的新行。

I have tried the following commands:

我试过以下命令:

  • M-x replace-string RET ; RET \n

    m x replace-string RET;RET \ n

    This will replace ; with 2 characters: \n.

    这将取代;2字符:\ n。

  • M-x replace-regex RET ; RET \n

    m x replace-regex RET;RET \ n

    This results in the following error (shown in the minibuffer):

    这将导致以下错误(在minibuffer中显示):

    Invalid use of `\' in replacement text.

    在替换文本中使用“\”无效。

What's wrong with using replace-string for this task? Is there any other way to do it?

为这个任务使用replace-string有什么问题吗?还有别的办法吗?

Thanks.

谢谢。

5 个解决方案

#1


346  

M-x replace-string RET ; RET C-q C-j.

m x replace-string RET;RET C-q C-j。

  • C-q for quoted-insert,

    C-q escape,

  • C-j is a newline.

    C-j是一个换行符。

Cheers!

干杯!

#2


53  

There are four ways I've found to put a newline into the minibuffer.

我发现有四种方法将一条新行放入到minibuffer中。

  1. C-o

    切断

  2. C-q C-j

    C-q C-j

  3. C-q 12 (12 is the octal value of newline)

    c - q12(12是换行的八进制值)

  4. C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y

    C-x到主窗口,用C-k杀死一条换行,然后c - xo返回到minibuffer,用C-y把它拽出来。

#3


22  

Don't forget that you can always cut and paste into the minibuffer.

别忘了,你可以随时剪切和粘贴到minibuffer中。

So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.

因此,您可以从缓冲区中复制一个换行符(或任何字符串),然后在提示替换文本时将其删除。

#4


7  

More explicitly:

更明确:

To replace the semi colon character (;) with a newline, follow these exact steps.

要用换行符替换semi冒号(;),请遵循以下步骤。

  1. locate cursor at upper left of buffer containing text you want to change
  2. 将光标定位在包含要更改的文本的缓冲区的左上角。
  3. Type m-x replace-string and hit RETURN
  4. 键入m-x替换字符串并单击RETURN。
  5. the mini-buffer will display something like this: Replace string (default ^ -> ):
  6. 微缓存将显示这样的:替换字符串(默认^ - >):
  7. Type in the character you want to replace. In this case, ; and hit RETURN
  8. 输入要替换的字符。在这种情况下,;点击返回
  9. the mini-buffer will display something like this: string ; with:
  10. 小缓冲区将显示如下内容:字符串;:
  11. Now execute C-q C-j
  12. 现在执行C-q C-j
  13. All instances of semi-colon will be replaced a newline (from the cursor location to the end of the buffer will now appear)
  14. 所有的分号实例都将被替换为一条新行(从光标位置到缓冲区的末尾将出现)

Bit more to it than the original explanation says.

比原来的解释说得多一点。

#5


2  

Switch to text-mode

切换到文本模式

M-x text-mode

Highlight block to indent

强调块缩进

Indent

缩进

C+M \

Switch back to whatever mode..

切换回任何模式。

#1


346  

M-x replace-string RET ; RET C-q C-j.

m x replace-string RET;RET C-q C-j。

  • C-q for quoted-insert,

    C-q escape,

  • C-j is a newline.

    C-j是一个换行符。

Cheers!

干杯!

#2


53  

There are four ways I've found to put a newline into the minibuffer.

我发现有四种方法将一条新行放入到minibuffer中。

  1. C-o

    切断

  2. C-q C-j

    C-q C-j

  3. C-q 12 (12 is the octal value of newline)

    c - q12(12是换行的八进制值)

  4. C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y

    C-x到主窗口,用C-k杀死一条换行,然后c - xo返回到minibuffer,用C-y把它拽出来。

#3


22  

Don't forget that you can always cut and paste into the minibuffer.

别忘了,你可以随时剪切和粘贴到minibuffer中。

So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.

因此,您可以从缓冲区中复制一个换行符(或任何字符串),然后在提示替换文本时将其删除。

#4


7  

More explicitly:

更明确:

To replace the semi colon character (;) with a newline, follow these exact steps.

要用换行符替换semi冒号(;),请遵循以下步骤。

  1. locate cursor at upper left of buffer containing text you want to change
  2. 将光标定位在包含要更改的文本的缓冲区的左上角。
  3. Type m-x replace-string and hit RETURN
  4. 键入m-x替换字符串并单击RETURN。
  5. the mini-buffer will display something like this: Replace string (default ^ -> ):
  6. 微缓存将显示这样的:替换字符串(默认^ - >):
  7. Type in the character you want to replace. In this case, ; and hit RETURN
  8. 输入要替换的字符。在这种情况下,;点击返回
  9. the mini-buffer will display something like this: string ; with:
  10. 小缓冲区将显示如下内容:字符串;:
  11. Now execute C-q C-j
  12. 现在执行C-q C-j
  13. All instances of semi-colon will be replaced a newline (from the cursor location to the end of the buffer will now appear)
  14. 所有的分号实例都将被替换为一条新行(从光标位置到缓冲区的末尾将出现)

Bit more to it than the original explanation says.

比原来的解释说得多一点。

#5


2  

Switch to text-mode

切换到文本模式

M-x text-mode

Highlight block to indent

强调块缩进

Indent

缩进

C+M \

Switch back to whatever mode..

切换回任何模式。