I am new to Emacs. I am reading a book on Python. The book has many code examples. What I would like to do is to

convert this:

 61. def getRandomWord(wordList):
 62.     # This function returns a random string from the passed list of strings.
 63.     wordIndex = random.randint(0, len(wordList) - 1)
 64.     return wordList[wordIndex]

in to this:

def getRandomWord(wordList):
    # This function returns a random string from the passed list of strings.
    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]

removing one space + the number + one space that appears at the beginning of each line. I'm sure there are multiple ways to accomplish this. For example, a regex search, multiple cursors, or a command that removes the first 5 chars from each line of a text.


If you were going through a text with a lot of code examples that you wanted to copy/paste into Emacs, what method would you use to remove the line numbers?


2 Answers 2


I would most likely use rectangle selection in this case.

Put point at the top-left position, then hit C-x <SPC> (rectangle-mark-mode) to start selection. Move point to the lower-right corner of the rectangle that contains the stuff you want to remove, then just kill the region with C-w.

That said, defining a macro or using a regexp is a better solution if you need to repeat this same thing many times (so +1 to @t-verron's answer).

  • 1
    I would most likely use rectangles for this regardless -- albeit more likely to start at the d of def and then type C-SPC M-} C-x r k -- and I would just make a macro which did that if I thought I needed to repeat it many times. Unless the horizontal alignment of the original text is broken by a change from (say) line 99 to line 100, I see this as the most efficient approach.
    – phils
    Mar 24, 2017 at 23:21
  • I think you need a C-a in that sequence? But yeah, that way makes the navigation more straightforward.
    – glucas
    Mar 24, 2017 at 23:35

I'd use either a keyboard macro or a replace-regexp.

Keyboard macro: f3 C-a M-d C-d C-d C-n f4 then press f4 to delete the line number for each line.

This records a macro which: puts the cursor to the beginning of line, deletes a word (the number) and 2 characters (the dot and the space), then moves to the next line. [Deleting 5 characters only works for numbers between 10 and 99]

If you paste another code block later and you didn't record another macro in between, you can just skip to pressing f4.

Replace-regexp: call query-replace-regexp (C-M-%), with ^[0-9]+\. as "from" and the empty string as "to".

You can recall the strings from the input history if you need to execute the same replacement again later.

Also, either solution can easily be turned into an interactive function if you feel the need to: for the macro, use kmacro-name-last-macro, and for replace-regexp, wrap it into an interactive defun, taking good care to protect the backslashes:

(defun tv/delete-heading-newline ()
    (query-replace-regexp "^[0-9]+\\. " ""))

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