Emacs中与vi的 dd 类似的命令是什么?我想要删除当前行。尝试过 CTRL + k 但它只会从光标位置开始删除。
C-a # Go to beginning of line
C-k # Kill line from current point
还有一种方法
C-S-backspace # Ctrl-Shift-Backspace
调用M-x kill-whole-line
命令。
如果您想设置不同的全局键绑定,则将此放置在~/.emacs中:
(global-set-key "\C-cd" 'kill-whole-line) # Sets `C-c d` to `M-x kill-whole-line`
如果您想删除多行文本内容,可以在命令前加上一个数字:C-u 5 C-S-backspace # deletes 5 whole lines
M-5 C-S-backspace # deletes 5 whole lines
C-u C-S-backspace # delete 4 whole lines. C-u without a number defaults to 4
C-u -5 C-S-backspace # deletes previous 5 whole lines
M--5 C-S-backspace # deletes previous 5 whole lines
有时我也发现C-x z
很有用:
C-S-backspace # delete 1 whole line
C-x z # repeat last command
z # repeat last command again.
# Press z as many times as you wish.
# Any other key acts normally, and ends the repeat command.
如果你不想杀掉这一行(这将把它放入操作系统的剪贴板和删除环中),而只是想删除它:
(defun delete-current-line ()
"Delete (not kill) the current line."
(interactive)
(save-excursion
(delete-region
(progn (forward-visible-line 0) (point))
(progn (forward-visible-line 1) (point)))))
(defun delete-current-line ()
"Deletes the current line"
(interactive)
(delete-region
(line-beginning-position)
(line-end-position)))
(delete-blank-lines)
的内容,例如下面这个例子,也许不太直观:(defun delete-current-line ()
"Deletes the current line"
(interactive)
(forward-line 0)
(delete-char (- (line-end-position) (point)))
(delete-blank-lines))
delete
而不是kill
,则可以使用下面的代码。(defun aza-delete-line ()
"Delete from current position to end of line without pushing to `kill-ring'."
(interactive)
(delete-region (point) (line-end-position)))
(defun aza-delete-whole-line ()
"Delete whole line without pushing to kill-ring."
(interactive)
(delete-region (line-beginning-position) (line-end-position)))
(defun crux-smart-delete-line ()
"Kill to the end of the line and kill whole line on the next call."
(interactive)
(let ((orig-point (point)))
(move-end-of-line 1)
(if (= orig-point (point))
(aza-delete-whole-line)
(goto-char orig-point)
(aza-delete-line))))
安装whole-line-or-region
软件包,然后运行(whole-line-or-region-global-mode)
。这将使C-w
在没有激活区域(选择)的情况下删除整行。
查看软件包的GitHub页面https://github.com/purcell/whole-line-or-region
最快且最简单的删除(杀死)整行的方式,无需选择任何内容,从任意位置开始删除一整行:
C-w ; kill-region
它可以灵活地删除所选内容,或者默认情况下删除一行(如果没有选择任何内容)。
考虑到问题,您可能还想复制Vim的“yank”,yy
(尽管在Emacs术语中,“yank”令人困惑地是Vim的“put”,p
)。这是:
M-w ; kill-ring-save
既美观又协调,而且很容易记忆。甚至略微类似于Vim的i_CTRL-W
。
一旦你使用上述任何一种方法将某些内容放入了剪贴板,你可能想要“粘贴”(即黏贴)它:
M-y ; yank-pop
(请注意,在终端Emacs中,C-S-backspace可能无法正常工作。)
C-x z
,真的很酷。顺便说一句,你的回答非常好而且准确。 - sluC-k C-k
,它有点像vim中的d$ S-j
,但是它可以很好地向前删除行。 - Theo BelaireC-cd
中没有空格? - Elliot Gorokhovskysmart-kill-whole-line
命令来处理缩进:http://emacsredux.com/blog/2013/04/09/kill-whole-line/ - m33lky