How can I delete the current line in Emacs?

Viewed 60000

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.

6 Answers

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

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

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

Related