如何在不破坏当前行的情况下添加新行?

发布于 2024-11-05 09:48:55 字数 82 浏览 0 评论 0原文

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

emacs 中有类似的吗?

In vim, I can do this in command mode by typing 'o', which will add a new line below the cursor, and enter the insert mode.

Is there an equivalent in emacs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

娇纵 2024-11-12 09:48:55

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

您可以通过两笔画得到与 vi 的 o 完全相同的效果:Ce RET,它将光标移动到当前行的末尾,然后插入一个新行,这会将光标保留在该行的开头。

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

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

The command C-o open-line that others have suggested is not quite the same as o in vi, because it splits the current line and lets the cursor remain in the current line.

You get the exact same effect as vi's o with two strokes: C-e RET, which moves the cursor to the end of the current line and then inserts a new line, which leaves the cursor at the beginning of that line.

You could bind that sequence to a key of its own (perhaps overriding the existing definition of C-o), but I doubt if it's worth the trouble.

(Incidentally, the symmetric sequence C-a RET gives you the effect of vi's capital O, inserting a line before the current line.)

无人接听 2024-11-12 09:48:55

你的问题解决了吗?

我刚刚解决了这个问题。请随意使用此代码:)
您可以在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)

have you solved your problem?

I just solved this problem. Feel free to use this code :)
You can bind to every key you like in the global-set-key,also to replace newline-and-indent with newline in case you don't like the new line to be indented.

;; 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)
撩动你心 2024-11-12 09:48:55

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

I am using prelude, and S-RET is equivalent to vi's o and C-S-RET is equivalent to vi's O.

无风消散 2024-11-12 09:48:55

我正在使用 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)))

希望它会有所帮助:)

I am using emacs 25 and I have something like this:

;; 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)))

Hope it will help :)

寄离 2024-11-12 09:48:55

我正在使用 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)
                       ))
  • 表示 下面的新行< /em>

  • 表示 < /strong> 用于新行上面

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

I am using Emacs 24.Insert the line to ".emacs" file.

;; 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)
                       ))
  • <C + Return> that means <Ctrl + Enter> for new line below

and

  • <C + S + Return> that means <Ctrl + Shift + Enter> for new line above.

    Both will indent also. I hope it will work.

你在我安 2024-11-12 09:48:55

我使用以下键绑定使其工作方式类似于 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>

I use following key bindings make it work similar to vim's o and 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>
寂寞清仓 2024-11-12 09:48:55

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

C-o will run open-line which will insert a blank line after the cursor. By default emacs is already in "insert mode" unless you are in a read-only buffer.

笨死的猪 2024-11-12 09:48:55

此配置可以帮助:

(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)

This configuration could help:

(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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文