5

Say we have a file with several cursors. Not all lines have cursors, but every cursor is on a different line.

How can I kill all the lines without cursors? I couldn't find a single mc command for this, but maybe there is a still a way to do it with the other commands?

  • One approach is to append lines with mc to a register, clear the whole buffer and paste in that appended register. – Kaushal Modi Apr 21 '15 at 20:58
3

The following function creates messages only, instead of actually deleting anything. If this approach seems appealing, then (message "No real or fake cursor here: %s" line) can be substituted with (kill-region pbol peol) to kill the line and place it into the kill-ring. If the user wishes to also remove the \n at the end of the line, then use (kill-region pbol (1+ peol)). The section of code (message "MC exists: %s" line) can be replaced with nil so that the function is silent in its operation. This function should only run one time, so it is added to the list of mc--default-cmds-to-run-once.

(defun mc/kill-all-lines-without-cursors ()
"Kill all lines that do not have fake or real cursors."
(interactive)
  (let* (
      mc
      line
      pbol
      peol
      (starting-line (line-number-at-pos))
      (my-list
        (mapcar
          (lambda (x)
            (cons (mc/cursor-beg x) (mc/cursor-end x)))
          (mc/all-fake-cursors))))
    (save-excursion
      (goto-char (point-max))
      (catch 'done
        (while t
          (setq line (line-number-at-pos))
          (setq mc nil)
          (setq pbol (point-at-bol))
          (setq peol (point-at-eol))
          (catch 'found
            (mapcar
              (lambda (x)
                (when
                  (or
                    (and
                      (>= (car x) pbol)
                      (<= (car x) peol))
                    (and
                      (>= (cdr x) pbol)
                      (<= (cdr x) peol)))
                  (setq mc t)
                  (throw 'found (message "MC exists:  %s" line))))
              my-list))
          (unless (or mc (= line starting-line))
            (message "No real or fake cursor here:  %s" line))
          (when (= 1 line) (throw 'done nil))
          (re-search-backward "\n" nil t))))))

(add-to-list 'mc--default-cmds-to-run-once 'mc/kill-all-lines-without-cursors)
  • One closely related question with an answer that would be a potential solution to the OP is the following: Say you want to kill the region selected by all cursors and yank it all on a new, empty buffer. How do you do that with multiple-cursors? Note that a simple paste on the new buffer does not work, since it only seems to yank the region from the first cursor. If we had a way to solve this particular and more natural problem, we could use it to solve the OP problem: The new buffer would only retain the selected text, so it would be equivalent to "keeping only the lines with the cursors". – Amelio Vazquez-Reina Apr 23 '15 at 0:03
  • 1
    I believe the answer above is indeed a complete answer to the initial question posed by the original poster, with the only difference being that messages are used instead of kill-region. The instructions in the answer provide for a method to replace the messages with kill-region. The comment above this one poses a new question, that should (in my opinion) be asked in new separate thread. – lawlist Apr 23 '15 at 0:16
  • As noted/suggested, I asked that question here. – Amelio Vazquez-Reina Apr 23 '15 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.