Emacs 相当于 Vim 的 dd,o,O

发布于 2024-08-20 06:35:08 字数 617 浏览 1 评论 0原文

我目前正在使用 emacs,并对大多数概念感到满意。但我真的很喜欢三个 vim 命令的便利性:dd、o、O 希望你能告诉我如何在 emacs 中镜像它们:)

dd - 删除整行,包括换行符,无论光标在哪里。

我发现了类似的方法:

Ca CK CK

Ca 将光标移动到行首时,第一个 Ck 会终止文本,第二个 Ck 会终止换行符。唯一的问题是,这不适用于空行,我只需要输入 Ck ,这非常不方便,因为我必须使用不同的命令来完成同一任务:杀死一行。

o / O - 在光标下方/上方创建一个新的空行,并将光标移动到新行,正确缩进

好吧,Ca Co 几乎类似于 O,只是缺少标识。 Ce Co 在当前行下方创建一个空行,但不移动光标。

对于我的问题是否有更好的解决方案,或者我是否必须学习 Lisp 并定义新命令来满足我的需求?

I am currently playing around with emacs and happy with most of the concepts. But I really adored the convenience of the three vim commands: dd,o,O
Hopefully you can tell me how to mirror them in emacs :)

dd - deletes whole line, including newline, no matter where the cursor is.

I found something similar to do the trick:

C-a C-k C-k

While C-a moves the cursor to the beginning of the line, the first C-k kills the text, the second one kills the newline. The only problem is that this is not working on empty lines where I only need to type C-k which is quite inconvenient as I have to use different commands for the same task: killing a line.

o / O - creates a new empty line below / above cursor and moves cursor to the new line, indented correctly

Well, C-a C-o is nearly like O, just the idention is missing. C-e C-o creates an empty line below the current but does not move the cursor.

Are there any better solutions to my problems or do I have to learn Lisp and define new commands to fulfill my needs?

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

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

发布评论

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

评论(10

意中人 2024-08-27 06:35:08

对于 oO,以下是我多年前编写的一些函数:

(defun vi-open-line-above ()
  "Insert a newline above the current line and put point at beginning."
  (interactive)
  (unless (bolp)
    (beginning-of-line))
  (newline)
  (forward-line -1)
  (indent-according-to-mode))

(defun vi-open-line-below ()
  "Insert a newline below the current line and put point at beginning."
  (interactive)
  (unless (eolp)
    (end-of-line))
  (newline-and-indent))

(defun vi-open-line (&optional abovep)
  "Insert a newline below the current line and put point at beginning.
With a prefix argument, insert a newline above the current line."
  (interactive "P")
  (if abovep
      (vi-open-line-above)
    (vi-open-line-below)))

您可以将 vi-open-line 绑定到,例如 < em>M-insert如下:

(define-key global-map [(meta insert)] 'vi-open-line)

对于dd,如果你想让被杀死的行进入kill环,你可以使用这个包装kill-line<的函数/code>:

(defun kill-current-line (&optional n)
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (let ((kill-whole-line t))
      (kill-line n))))

为了完整性,它接受一个前缀参数并将其应用于kill-line,这样它就可以杀死比“当前”行更多的内容。

您还可以查看 viper-mode< 的源代码/code>查看它如何实现等效的 ddoO 命令。

For o and O, here are a few functions I wrote many years ago:

(defun vi-open-line-above ()
  "Insert a newline above the current line and put point at beginning."
  (interactive)
  (unless (bolp)
    (beginning-of-line))
  (newline)
  (forward-line -1)
  (indent-according-to-mode))

(defun vi-open-line-below ()
  "Insert a newline below the current line and put point at beginning."
  (interactive)
  (unless (eolp)
    (end-of-line))
  (newline-and-indent))

(defun vi-open-line (&optional abovep)
  "Insert a newline below the current line and put point at beginning.
With a prefix argument, insert a newline above the current line."
  (interactive "P")
  (if abovep
      (vi-open-line-above)
    (vi-open-line-below)))

You can bind vi-open-line to, say, M-insert as follows:

(define-key global-map [(meta insert)] 'vi-open-line)

For dd, if you want the killed line to make it onto the kill ring, you can use this function that wraps kill-line:

(defun kill-current-line (&optional n)
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (let ((kill-whole-line t))
      (kill-line n))))

For completeness, it accepts a prefix argument and applies it to kill-line, so that it can kill much more than the "current" line.

You might also look at the source for viper-mode to see how it implements the equivalent dd, o, and O commands.

淤浪 2024-08-27 06:35:08

对于 dd,请使用“kill-whole-line”,在最新版本的 Emacs 中默认绑定到“CS-backspace”。

我应该补充一点,我自己更频繁地使用 whole-line-or-region.el ,因为 < code>Cw 比 CS-backspace 更容易输入。

For dd, use "kill-whole-line", which is bound to "C-S-backspace" by default in recent versions of Emacs.

I should add that I myself use whole-line-or-region.el more often, since C-w is easier to type than C-S-backspace.

山人契 2024-08-27 06:35:08
C+e C+j

根据 emacs 手册文档。这会给你一个新的行和缩进。

C+e C+j

According to the emacs manual docs. That gets you a new line and indentation.

依 靠 2024-08-27 06:35:08

您可以创建一个 宏< /a> 和 将其绑定到按键序列。还不需要学习任何 emacslisp。

You could create a macro and bind it to a key sequence. No need to learn any emacslisp yet.

随梦而飞# 2024-08-27 06:35:08

我知道,这个回答并不直截了当,但是就像一个 vim 用户一样,我发现 Spacemacs 是从 vim 迁移到 emacs 时功能最强大的 emacs 入门包。您可以将其配置为类似 vim、类似 emacs 或混合。

http://spacemacs.org/

尝试一下。

I know, this response is not straight to the point, however like a vim user, I found that Spacemacs is the most functional emacs starter pack to move from vim to emacs. You can configure it to be vim like, emacs like or hybrid.

http://spacemacs.org/

Give it a try.

回忆那么伤 2024-08-27 06:35:08

经过几次搜索和实验,我根据其他答案得出的结论是替代方案如下:

  • Vim 上的 dd 命令:Emacs 上的 CS-backspace
  • < Vim 上的 code>o 命令:Emacs 上的 Ce Cj Vim
  • 上的 O 命令:Emacs 上的 Ca Cj Cp

是的,emacs 有时有一些“富有表现力”的命令组合可以做几件事,但有时它们确实很有意义!

如果你想一想,emacs 有时也会简化 vim 无法做到的事情。当您想要选择文件中的整个文本时,您可以在 Vim 上执行 ggVG,而在 Emacs 上只需执行 Cx h

After a couple of searching and experimenting, I came to a conclusion based on the other answers that the alternatives are the following:

  • dd command on Vim: C-S-backspace on Emacs
  • o command on Vim: C-e C-j on Emacs
  • O command on Vim: C-a C-j C-p on Emacs

Yes, emacs sometimes have some "expressive" command combinations to do a couple of things, but they do make a lot of sense sometimes!

And if you think about it, emacs sometimes also simplifies things when vim doesn't. When you want to select the whole text in a file, you do ggVG on Vim while on Emacs is simply C-x h!

无声无音无过去 2024-08-27 06:35:08

以下是我如何解决 Emacs 缺乏类似 vi 的“O”命令的问题:

(defadvice open-line (around vi-style-open-line activate)
  "Make open-line behave more like vi."
  (beginning-of-line)
  ad-do-it
  (indent-according-to-mode))

有了这个,我就不再真正觉得需要 vi 的“o”命令的相应版本。 Cn Co 就成功了。

至于“dd”命令,一开始有点不舒服,但我最终接受了 Emacs 的处理方式。无论如何,当我想一次删除几行时(这种情况经常发生),我只需使用区域(Ca C-SPC,转到我要删除的文本的另一端,Cw)即可。或者,如果我可以看到我想要删除的行数,我会这样做。 M-9 Ck 一次删除九行。

Here's how I addressed the issue of Emacs's lack of a vi-like "O" command:

(defadvice open-line (around vi-style-open-line activate)
  "Make open-line behave more like vi."
  (beginning-of-line)
  ad-do-it
  (indent-according-to-mode))

With this in place, I've never really felt the need for a corresponding version of vi's "o" command. C-n C-o does the trick.

As for the "dd" command, that grated a little at first, but I eventually came around to Emacs's way of doing things. Anyway, when I want to delete several lines at once, which is often the case, I just do it using the region (C-a C-SPC, go to the other end of the text I want to delete, C-w). Or if I can eyeball the number of lines I want to delete, I'll do eg. M-9 C-k to delete nine lines at once.

廻憶裏菂餘溫 2024-08-27 06:35:08

Just use Viper-mode, Vimpulse or Vim Mode, Emacs keybindings are just not as ergonomic.

平定天下 2024-08-27 06:35:08

所有这些都很棒。这里还有一些。

https://stable.melpa.org/#/crux
https://github.com/bbatsov/crux

Crux 不会映射任何留给您的按键绑定决定按键绑定,但以下是您可以根据建议进行按键绑定或根据需要进行更改的相关功能。

Function:                   Keymap:     Description:
---------------------------------------------------------
crux-kill-whole-line        Super-k     Kill whole line
crux-smart-open-line-above  Super-O     Insert an empty line above & indent
crux-smart-open-line        Super-o     Insert an empty line and indent 

您当然可以使用其他东西,例如 Cc o / Mc o 或任何其他对您个人有意义的键绑定。请参阅 crux github 以获取说明。

另一种方法是考虑 Doom Emacs,它默认安装并打开了邪恶模式。那么您熟悉的 99.999% ViM 键绑定的工作方式大致相同。您还可以在 Vanilla Emacs 上手动安装 Evil-Mode,甚至可以根据需要使用键绑定打开和关闭它,或者利用一些小型 eLisp 将 Evil-Mode 挂钩到特定的缓冲区/文件类型。

我已经是 Vi / ViM / Neovim 的长期用户了,我是从 Doom 开始的。然而,最近,我强迫自己按照预期的方式学习使用 Emacs,而且我学到了很多东西。键和弦和默认绑定并不像我想象的那么糟糕。我很快就学会了肌肉记忆。我只是启动 Emacs,除了选择一个合适的主题、字体、禁用滚动条、工具栏、菜单栏以及添加哪个键模式和宽敞填充模式之外。我没有做太多改变,只是安装了一些其他软件包。

All of these are great. Here's a couple more.

https://stable.melpa.org/#/crux
https://github.com/bbatsov/crux

Crux doesn't map any keybinds it leaves you to decide the keybindings but here are the relevant functions you can keybind as suggested or change as you wish.

Function:                   Keymap:     Description:
---------------------------------------------------------
crux-kill-whole-line        Super-k     Kill whole line
crux-smart-open-line-above  Super-O     Insert an empty line above & indent
crux-smart-open-line        Super-o     Insert an empty line and indent 

You could of course use something else such as C-c o / M-c o or whatever other keybinding makes sense to you personally. See the crux github for the instructions.

Alternative is to consider Doom Emacs which has Evil Mode installed and turned on by default. Then 99.999% of the ViM Keybindings you are familiar with work much the same way. You can also install Evil-Mode manually on Vanilla Emacs and even turn it on and off as needed with a keybinding or utilize some minor eLisp to hook Evil-Mode to specific buffer / file-types.

I've been a long time Vi / ViM / Neovim user and I started with Doom. However, lately, I am forcing myself to learn to use Emacs the way it was intended and I am learning quite a bit more. The key-chords and default bindings are not as terrible as I thought. I am learning the muscle memory rather quickly. I just start up Emacs and aside from picking a decent theme, font(s), and disabling the scrollbar, toolbar, menu bar and adding which-key-mode and spacious-padding-mode. I've not changed much else but install some other packages.

梦开始←不甜 2024-08-27 06:35:08

正如其他人所说的 CS-backspace 杀死当前行

对于上面的打开行,使用 Mm Co 保持缩进(Mm 让你行的开头缩进之后)

对于下面打开的行,我很惊讶没有人建议按enter键打开下面的一行并放入其中,因此,请在移动到当前行的末尾 Ce Enter 后执行此操作。与 Cj 不同,这也可能为您提供正确的缩进

As others have said C-S-backspace to kill the current line

For open line above, use M-m C-o to keep the indentation (M-m puts you to the start of the line after indentation)

For open line below, I'm surprised no one has suggested to press the enter key to open a line below and drop into it, so do that after moving to the end of the current line C-e enter. This is likely to also give you the right indentation, unlike C-j

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