How can I delete the current line in Emacs?

Emacs

Emacs Problem Overview


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.

Emacs Solutions


Solution 1 - Emacs

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.

Solution 2 - Emacs

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

Solution 3 - Emacs

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

Solution 4 - Emacs

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

Solution 5 - Emacs

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

Solution 6 - Emacs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManoj GovindanView Question on Stackoverflow
Solution 1 - EmacsunutbuView Answer on Stackoverflow
Solution 2 - EmacsVladimir PanteleevView Answer on Stackoverflow
Solution 3 - EmacsdardiscoView Answer on Stackoverflow
Solution 4 - EmacsazzamsaView Answer on Stackoverflow
Solution 5 - EmacsMicah ElliottView Answer on Stackoverflow
Solution 6 - EmacsWinnyView Answer on Stackoverflow