I want to delete lines with number 03,08,13,18,...., 3+5n.
How can I do that? Thank you very much.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityThe Swiss army knife of Emacs is called keyboard macros. See the Emacs Wiki and the manual.
Enter the macro mode, perform the commands of the macro (note: the commands are not only recorded, they are also executed).
Steps:
C-x (
M-x kill-whole-line
C-x )
In case of any error during this process, you have to cancel the macro recording (C-g
), undo until the file is back to the original state (or reload), then create the macro again.
You should now be on line 8.
To repeat the sequence of commands, do C-x e
. If you don't do anything else and want to repeat again the macro execution, just enter e
, after C-x e
was executed.
To repeat the macro 3 times, do C-u 3 C-x e
, etc. To repeat the macro until the end of the file is reached (or more generally until an error occurs), do C-u 0 C-x e
.
The following function (briefly tested) will allow you to delete lines with a starting value and a step value:
(defun delete-some-lines (&optional start step)
"Delete lines starting with START at STEP intervals."
(interactive)
(let ((lines (list (or start
(string-to-number (read-string "Starting line: ")))))
(step (or step (string-to-number (read-string "Skipping: "))))
(last (line-number-at-pos (point-max))))
(while (<= (+ step (car lines)) last)
(push (+ step (car lines)) lines))
(save-excursion
(cl-dolist (line lines)
(goto-line line)
(delete-region (point-at-bol)
(min (1+ (point-at-eol)) (point-max)))))))
Note that it doesn't do any error checking.
If the user wishes to also delete the \n
at the end of the line, then just change (line-end-position)
to (1+ (line-end-position))
.
(defun delete-line-number (line)
"Doc-string."
(save-excursion
(goto-line line)
(delete-region (line-beginning-position) (line-end-position))))
(defun delete-3+5n-lines ()
"Delete the 3+5n linum lines in current buffer."
(interactive)
(let* ((lines (split-string (buffer-string) "\n"))
(max-linum (length lines))
(3+5n-linums
(cl-loop for x from 0
for y = (+ 3 (* 5 x))
until (> y max-linum)
collect y)))
(erase-buffer)
(insert
(mapconcat
#'identity
(cl-loop for x from 1 to max-linum
unless (memq x 3+5n-linums)
collect (elt lines (1- x)))
"\n"))))
erase-buffer
-- Are you serious? This approach looks like a machete instead of a scalpel! What if there are overlays? What if there are text properties? What if . . . My comments underneath the question of the original poster basically give the ingredients of what should be a valid answer . . .
forward-line
andline-number-at-pos
anddelete-region
-- withline-beginning-position
andline-end-position
.(format-mode-line "%l")
: emacs.stackexchange.com/a/3822/2287 What spawned that particular thread, was the need to do 50 calculations of line numbers per command loop, so I needed something faster.