1

Suppose I have some code written like:

a_to_b = self.tversky(a, b)
b_to_c = self.tversky(b, c)
d_to_e = self.tversky(d, e)
e_to_f = self.tversky(e, f)
g_to_h = self.tversky(g, h)

I would like to select as a rectangle the variable names x_to_y and then paste it into array as so:

return [a_to_b, b_to_c, d_to_e, e_to_f, g_to_h]

I don't necessarily need the commas in one command, but basically selecting a few columns and then applying M-S ^ to each line. So I can get them all next to each other.

  • What is M-S ^, and what did you intend by applying it to each line? – Drew Mar 16 at 3:50
1
  1. Select the rectangle (e.g. use C-x SPC).

  2. Use this command (M-x foo) to copy the variables to the kill-ring, as the string "a_to_b, b_to_c, d_to_e, e_to_f, g_to_h":

(defun foo (beg end)
  (interactive "r")
  (let ((vars  (mapconcat #'identity
                          (extract-rectangle (region-beginning) (region-end))
                          ", ")))
    (kill-new vars)))
  1. Move to where you want the result.

  2. Use this command (M-x bar) to insert the result at point.

(defun bar ()
  (interactive)
  (insert "return [")
  (yank)
  (insert "]\n"))
| improve this answer | |
  • Thanks, M-S ^ is delete-indentation. When I posted the question, I had an OS shortcut overiding that command and couldn't C-h k for it. – dylanjm Mar 16 at 17:07
  • Hi @dylanjm, that would normally be written M-^ (or S-M-6, but that ties you to a US or UK keyboard). – Phil Hudson Mar 18 at 11:20

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.