There are many ways to do this.
1. Using built-in emacs stuff
Switch to a programming mode where aligning at the equal sign makes sense, e.g.: M-x python-mode
Place point right at the end of the line with the last =
.
Call M-x align
.
Call copy-rectangle-as-kill
on the rectangle before the =
.
Undo the alignment with C-x u
2. Using the versatile matches
function
The following function matches
is quite usefull for collecting strings matching some regular expression. Please also read the doc-string of that function.
- Install the function in your init files. (You only need this once to use it later on.)
- Select the region with the strings you want to collect. If region is not active then the region from
(point-min)
to (point-max)
is used.
- Input M-x
matches
RET. You can also bind matches
to some key if you like.
- Input your regular expression. In your case that can be
^[^= ]+
. Press two times RET. (The first time you confirm the default 0
for the number of the sub-expression you want to collect from the match. Thereby 0
means the full match.)
- Now you can yank a string with all matches where you want to have it.
I've added the protection against matching empty strings before pasting the function here. Hopefully, that did not break anything. Just leave a comment if you encounter problems. I will fix them.
(defcustom matches-default-separator " "
"Default separator string for `matches'."
:group 'matching
:type 'string)
(defvar matches-separator-history nil
"History of the separators for `matches'.")
(defun matches (re &optional n b e unique)
"Return a list of all occurences of re within region from B to E.
If B and E are `nil' then search from point to end of buffer.
If called interactively the list goes to the kill-ring.
If UNIQUE is non-nil or if `matches' is called with interactively with prefix arg
then collect each matching string only once.
On interactive calls also the separator is asked for."
(interactive "sRegular expression:\nMNumber of subexpression (default: 0):")
(let (sep)
(if (stringp n) (setq n (string-to-number n)))
(when (called-interactively-p 'any)
(setq unique current-prefix-arg)
(setq sep (read-string (format "Separator (default: \"%s\"):" matches-default-separator) nil 'matches-separator-history matches-default-separator))
(if (use-region-p)
(progn
(setq b (region-beginning))
(setq e (region-end)))))
(let (ret)
(save-excursion
(unless n (setq n 0))
(unless b (setq b (point)))
(unless e (setq e (point-max)))
(goto-char b)
(condition-case err
(while (search-forward-regexp re e 'noErr)
(let ((str (match-string-no-properties n)))
(unless (and unique
(assoc-string str ret))
(setq ret (cons str ret)))
(if (= (length str) 0) ;; protection against inappropriate re
(forward-char))))
(end-of-buffer))
(setq ret (nreverse ret))
(if (called-interactively-p 'any)
(progn
(message "Matches found: %s" ret)
(kill-new (mapconcat 'identity ret sep))))
ret))))