How to delete all the lines in one go in emacs?
Asked Answered
H

3

8

I want to delete all the lines below a particular line of emacs ? Is there any shortcut key which can delete all the lines below a particular line?

Hedonics answered 26/8, 2013 at 4:34 Comment(1)
You are kind of missing the point of using a programmable, extensible editor if you want to restrict yourself to predefined key bindings.Theism
J
12

C-u 9999999 C-k should do the trick.

If you are not at the beginning of the first line you want to kill, then use C-a C-u 9999999 C-k.

(The 9999999 can be any number larger than the number of lines you want to kill.)

An alternative to using C-u 999999 is to hold down the Control key and then hold down 9, so you get, in effect: C-9 C-9 C-9 C-9 C-9 C-9 C-9 C-k.

Josie answered 26/8, 2013 at 4:43 Comment(3)
Try again. Unless you have something in your init file (~/.emacs) that changes things it will work. To be clear, C-a means press and hold Control while hitting a, and so on.Josie
actually the number 1000 was being displayed as 1 0 0 0 so i thought it was working for single digits but that was not the case and it deleted 1000 linesHedonics
Using 9s instead of 1s and 0s means fewer keystrokes. ;-)Josie
C
8

Just add this function to your ~/.emacs:

(defun kill-to-end-of-buffer() "Deletes all lines after the current line"
  (interactive)
  (progn
    (forward-line 1)
    (delete-region (point) (point-max))))


;; Change this to your preferred keybinding
(global-set-key "\C-\M-k" 'kill-to-end-of-buffer)
Cowboy answered 26/8, 2013 at 5:3 Comment(1)
Incredibly convenient. Thank you!Kiwanis
S
0

To delete all the lines in one go you can use C-x r t space. Makes a string rectangle! Check this link for more info on how to specialize this for your needs.

Springe answered 23/4 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.