微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Emacs相当于Vim的dd,o,O

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

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

我发现了类似的做法:

C-a C-k C-k

虽然C-a将光标移动到行的开头,但第一个C-k会杀死文本,第二个则会杀死换行符。唯一的问题是,这不是在空线上工作,我只需要键入C-k,这是非常不方便的,因为我必须对同一个任务使用不同的命令:杀死一条线。

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

那么,C-C C-O几乎像O,只是身份丢失了。 C-e C-o在当前下方创建一个空行,但不会移动光标。

有什么更好的解决方案我的问题,还是我必须学习Lisp和定义新的命令来满足我的需要?

对于o和o,这里是几年前写的几个函数
(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绑定到M-insert,如下所示:

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

对于dd,如果想要杀死的行将其置于kill ring上,可以使用包含kill-line的此函数

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

为了完整,它接受一个前缀参数并将其应用于kill-line,这样它可以比“current”行多得多。

您也可以查看viper-mode的源,看看它如何实现等效的dd,o和o命令。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐