How can I delete the current line in Emacs?

后端 未结 5 1007
别那么骄傲
别那么骄傲 2021-01-29 18:42

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.

相关标签:
5条回答
  • 2021-01-29 19:08

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

    0 讨论(0)
  • 2021-01-29 19:11

    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)))))
    
    0 讨论(0)
  • 2021-01-29 19:13

    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))
    
    0 讨论(0)
  • 2021-01-29 19:13

    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

    0 讨论(0)
  • 2021-01-29 19:19
    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.
    
    0 讨论(0)
提交回复
热议问题