ChatGPT解决这个技术问题 Extra ChatGPT

如何删除 Emacs 中的当前行?

vi的dd的emacs等价物是什么?我想删除当前行。试过 CTRL + k 但它只从当前位置删除。


m
msrd0
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.

这行得通。有没有办法使用一个命令来做到这一点?我是否必须创建自定义键绑定才能执行此操作?
不知道C-x z,这真的很酷。顺便说一句,答案很好且准确。
还有 C-k C-k,它有点像 vim 中的 d$ S-j,但这可以很好地删除向前的行。
为什么 C-cd 中没有空格?
@RenéG:空间是可选的。
V
Vladimir Panteleev

如果您不想终止该行(这会将其放入操作系统剪贴板并终止环),而只需将其删除:

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

d
dardisco

另一种删除线而不将其放入杀伤环的方法:

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

a
azzamsa

而不是使用单独的键来删除行,或者必须调用前缀参数。您可以使用 crux-smart-kill-line 它将“杀死到行尾并在下一次调用时杀死整行”。但是,如果您更喜欢 delete 而不是 kill,则可以使用下面的代码。

对于点到字符串操作(杀死/删除),我建议使用 zop-to-char

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

source


M
Micah Elliott

从线上的任何点删除(杀死)整行而不选择任何内容的最快/最简单的方法是:

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

(请注意,CS-backspace 可能在终端 Emacs 中不起作用。)


W
Winny

安装软件包 whole-line-or-region,然后运行 (whole-line-or-region-global-mode)。如果没有区域(选择)处于活动状态,这将使 C-w 杀死整行。

请参阅包的 GitHub 页面 https://github.com/purcell/whole-line-or-region