【问题标题】:How to add a new line without breaking the current line?如何在不破坏当前行的情况下添加新行?
【发布时间】:2011-08-19 09:34:53
【问题描述】:

在 vim 中,我可以在命令模式下通过键入 'o' 来执行此操作,这将在光标下方添加一个新行,并进入插入模式。

emacs 中是否有等价物?

【问题讨论】:

    标签: emacs


    【解决方案1】:

    其他人建议的命令Co open-line与vi中的o不太一样,因为它拆分当前行并让光标留在当前行。

    你得到与 vi 的 o 完全相同的效果,只需两个笔划:Ce RET,将光标移动到当前的末尾行,然后插入一个新行,将光标留在该行的开头。

    您可以将该序列绑定到它自己的键上(可能会覆盖 C-o 的现有定义),但我怀疑这是否值得麻烦。

    (顺便说一句,对称序列Ca RET给你vi的大写O的效果,在之前插入一行 em> 当前行。)

    【讨论】:

    • 我只是偶尔在 vi 中发现了这个很棒的键,我确信绑定到 C-o 是值得的。经常需要它,我想知道为什么我以前没有猜到这样做。
    • 更好的命令是C-e C-o,因为如果启用了自动填充模式,这将避免换行。
    • 更好的是:M-m C-o 可以保留缩进。
    【解决方案2】:

    你的问题解决了吗?

    我刚刚解决了这个问题。随意使用此代码:) 您可以在global-set-key绑定到您喜欢的每个键,也可以将newline-and-indent 替换为newline 以防您不喜欢新行缩进。

    ;; newline-without-break-of-line
    (defun newline-without-break-of-line ()
      "1. move to end of the line.
      2. insert newline with index"
    
      (interactive)
      (let ((oldpos (point)))
        (end-of-line)
        (newline-and-indent)))
    
    (global-set-key (kbd "<C-return>") 'newline-without-break-of-line)
    

    【讨论】:

    • 这就是 emacs 的优点:你可以通过省略它来实现你想要的一切。没有限制也没有约束。您可以制作适合您的 emacs,因为它是高度可定制的!
    【解决方案3】:

    我用的是prelude,S-RET相当于vi的oCS-RET相当于vi的O .

    【讨论】:

      【解决方案4】:

      我正在使用emacs 25,我有这样的东西:

      ;; Insert new line below current line
      ;; and move cursor to new line
      ;; it will also indent newline
      (global-set-key (kbd "<C-return>") (lambda ()
                         (interactive)
                         (end-of-line)
                         (newline-and-indent)))
      ;; Insert new line above current line
      ;; and move cursor to previous line (newly inserted line)
      ;; it will also indent newline
      ;; TODO: right now I am unable to goto previous line, FIXIT
      (global-set-key (kbd "<C-S-return>") (lambda ()
                             (interactive)
                             (beginning-of-line)
                             (newline-and-indent)
                             (previous-line)))
      

      希望它会有所帮助:)

      【讨论】:

        【解决方案5】:

        我使用以下键绑定使其与 vim 的 o 和 O 类似:

        <pre>
        ;; vi-like line insertion
        (global-set-key (kbd "C-o") (lambda () (interactive)(beginning-of-line)(open-line 1)))
        (global-set-key (kbd "M-o") (lambda () (interactive)(end-of-line)(newline)))
        </pre>
        

        【讨论】:

          【解决方案6】:

          我正在使用 Emacs 24。将行插入“.emacs”文件。

          ;; Move cursor to end of current line
          ;; Insert new line below current line
          ;; it will also indent newline
          (global-set-key (kbd "<C-return>") (lambda ()
                             (interactive)
                             (end-of-line)
                             (newline-and-indent)))
          
          ;; Move cursor to previous line 
          ;; Go to end of the line
          ;; Insert new line below current line (So it actually insert new line above with indentation)
          ;; it will also indent newline
          (global-set-key (kbd "<C-S-return>") (lambda ()
                                 (interactive)
                                 (previous-line)
                                 (end-of-line)
                                 (newline-and-indent)
                                 ))
          
          • &lt;C + Return&gt; 表示 &lt;Ctrl + Enter&gt; 代表下面的新行

          • &lt;C + S + Return&gt; 表示 &lt;Ctrl + Shift + Enter&gt; 代表上面的新行

            两者都会缩进。我希望它会起作用。

          【讨论】:

            【解决方案7】:

            C-o 将运行open-line,它将在光标后插入一个空行。默认情况下,emacs 已经处于“插入模式”,除非您在只读缓冲区中。

            【讨论】:

              【解决方案8】:

              此配置可能会有所帮助:

              (defun newline-without-break-of-line ()
                "1. move to end of the line.
              2. open new line and move to new line"
                (interactive)
                (end-of-line)
                (open-line 1)
                (right-char))
              (global-set-key (kbd "<M-return>") 'newline-without-break-of-line)
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-12-29
                • 2011-11-26
                • 1970-01-01
                • 2015-05-22
                • 2013-04-12
                • 2014-01-08
                • 2010-09-15
                • 1970-01-01
                相关资源
                最近更新 更多