如何在Emacs中删除当前行?


Answers:


212
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.

这可行。有一种方法可以使用一个命令来执行此操作吗?为此,我是否需要创建自定义键绑定?
Manoj Govindan

14
不知道C-x z,那真的很酷。顺便说一句,很好而精确的答案。
slu 2010年

2
还有C-k C-k一点有点像d$ S-jvim一样,但这可以很好地删除行。
Theo Belaire

为什么里面没有空间C-cd
Elliot Gorokhovsky

@RenéG:该空间是可选的。
unutbu 2015年

11

如果您不想杀死该行(它将把它放入操作系统剪贴板并杀死响铃),只需删除它:

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

3

删除该行而不将其放入kill环的另一种方法:

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

1

在不选择任何内容的情况下,从行中的任何一点删除(杀死)整行的最快/最简单的方法是:

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退格键可能无法在终端Emacs中使用。)


1

不必使用单独的键来删除行,也不必调用前缀参数。您可以使用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))))

资源

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.