ChatGPT解决这个技术问题 Extra ChatGPT

How can I delete the current line in Emacs?

What is the emacs equivalent of vi's dd? I want to delete the current line. Tried CTRL + k but it only deletes from current position.


m
msrd0
C-a # Go to beginning of line
C-k # Kill line from current point

There is also

C-S-backspace   # Ctrl-Shift-Backspace

which invokes M-x kill-whole-line.

If you'd like to set a different global key binding, you'd put this in ~/.emacs:

(global-set-key "\C-cd" 'kill-whole-line)     # Sets `C-c d` to `M-x kill-whole-line`

If you want to delete a number of whole lines, you can prefix the command with a number:

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

Sometimes I also find C-x z helpful:

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.

This works. Is there a way to do this using one command? Will I have to create a custom key binding to do this?
Didn't know C-x z, that's really cool. And nice and precise answer btw.
There's also C-k C-k which kinda works like a d$ S-j in vim, but this can delete lines forward fairly nicely.
Why isn't there a space in C-cd?
@RenéG: The space is optional.
V
Vladimir Panteleev

In case you don't want to kill the line (which would put it into the OS clipboard and kill ring) but simply delete it:

(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

Another method to delete the line without placing it into the kill ring:

(defun delete-current-line ()
  "Deletes the current line"
  (interactive)
  (delete-region
   (line-beginning-position)
   (line-end-position)))

This will leave the point at the beginning of a blank line. To get rid of this also, you may wish to add something like (delete-blank-lines) to the end of the function, as in this example, which is perhaps a little less intuitive:

(defun delete-current-line ()
 "Deletes the current line"
 (interactive)
 (forward-line 0)
 (delete-char (- (line-end-position) (point)))
 (delete-blank-lines))

a
azzamsa

Rather than having separate key to delete line, or having to invoke prefix-argument. You can use crux-smart-kill-line which will "kill to the end of the line and kill whole line on the next call". But if you prefer delete instead of kill, you can use the code below.

For point-to-string operation (kill/delete) I recommend to use 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

The fastest/simplest way to delete (kill) a full line, from any point on the line, without selecting anything, is:

C-w  ; kill-region

It is versatile in deleting whatever is selected, or a line by default if nothing is selected.

Given the question, you're probably also interested in replicating Vim's "yank", yy (though in Emacs parlance a "yank" is confusingly Vim's "put", p). This is:

M-w  ; kill-ring-save

Nice and congruent, and pretty easy to remember. Even slightly similar to Vim's i_CTRL-W.

Once you've put something in the kill ring with either of the above, you'll likely want to "yank" (paste) it:

M-y  ; yank-pop

(Note that C-S-backspace may not work in terminal Emacs.)


W
Winny

Install package whole-line-or-region and then run (whole-line-or-region-global-mode). This will make C-w kill the whole line if no region (selection) is active.

See the Package's GitHub page https://github.com/purcell/whole-line-or-region