1

I've only just started with Emacs (about 6 weeks now) so I'm not really familiar with its coding language. I was wondering if it was possible to write, for Emacs, the equivalent of a Python macro I have in my Sublime Text setup.

In Sublime Text, I have a Python macro which can transform selected lines of text. If I select the lines

foo
bar
baz

and press my chosen shortcut C-l C-s the text is transformed to ['foo', 'bar', 'baz'].

I was wondering how to could do this for Emacs.

  • Yes, of course you can! You program emacs with elisp, which as a Lisp dialect is a fully-fledged programming language. You can perform this and much more complicated text processing tasks with emacs. If you can describe it precisely, it can be programmed. Did you actually want a function to do this particular task? (On a separate note, your tag [macros] is wrong; here, it refers to a particular language feature of elisp.) – Harald Hanche-Olsen Feb 24 '16 at 21:48
  • A macro to do this particular task would be very useful, thank you. One of the reasons I'm trying to move to emacs from Sublime Text is that it's programmable and also open source (so I don't have to worry about the developer losing interest). – sigint Feb 24 '16 at 21:51
  • Um, I didn't offer to write this function (not macro). Perhaps someone else will. A brief note, however: If this is to be used for making lists of strings in some programming language, as it looks like, you should decide whether and how it should deal with any single quotes already present in the selected text. I.e., if one of the lines contains “fo'c'sle”, should that be rendered as 'fo''c''sle', or 'fo\'c\'sle' or something entirely different? – Harald Hanche-Olsen Feb 24 '16 at 22:03
  • Your response was still very useful, I was looking for emacs macros not realising that macros meant something else in emacs. I'm now googling elisp functions and found promising answers like stackoverflow.com/questions/605846/… I'll do some searching based on what you've said. Thank you. – sigint Feb 24 '16 at 22:11
  • 1
    Note that Emacs has two distinct kinds of things called "macros". Firstly the Emacs Lisp language provides macros (see C-h i g (elisp) Macros). Secondly there is an editing feature referred to as Keyboard Macros (see C-h i g (emacs) Keyboard Macros). I don't think you actually meant either of these things, so you should definitely avoid the word "macro" entirely in order to avoid confusion. – phils Feb 25 '16 at 0:32
5

This method will take the selected lines, wrap them in single quotes, and put them inside a list.

(defun lines-to-list (point mark)
  (interactive "r")
  (let ((text (buffer-substring point mark)))
    (delete-region point mark)
    (insert "[")
    (insert (mapconcat (lambda (line) (format "'%s'" line))
                       (split-string text "\s*\n\s*")
                       ", "))
    (insert "]")))

You can, bind them to a key shortcut like this:

(define-key python-mode-map (kbd "C-c C-l") #'lines-to-list)

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.